Options
All
  • Public
  • Public/Protected
  • All
Menu

Module es-stats

es-stats

Find out import, exports and root declarations' dependency relationships of an ES module file.

Quick start

Intalling via npm:

npm i es-stats

Extract imports, exports, and root declarations' relationship with default settings:

const esStats = require('es-stats');
const fs = require('fs');

const { imports, exports, relations } = esStats(
  fs.readFileSync('myfile.js', 'utf-8')
);
console.log('Imports:', imports);
console.log('Exports:', exports);
console.log('Relations:', relations);

To support things like JSX, flow, dynamic imports, etc.. You will need to enable @babel/parser plugins:

const { imports, exports, relations } = esStats(
  fs.readFileSync('myfile.js', 'utf-8'),
  {
    plugins: ['jsx', 'dynamicImport'],
  }
);

Following plugins are enabled on .js|.jsx|.ts|.tsx files by default:

  • dynamicImport
  • classProperties
  • flowComments
  • objectRestSpread
  • functionBind
  • jsx
  • flow (.js and .jsx only)
  • typescript (.ts and .tsx only)

All the options in the 2nd parameter will be passed to @babel/parser directly. @babel/parser options can be found here.

Index

References

createExportVisitors

Re-exports createExportVisitors

createImportVisitors

Renames and re-exports createExportVisitors

createRootRelationVisitors

Re-exports createRootRelationVisitors

default

Renames and re-exports fileStats

extractStats

Re-exports extractStats

getPrivateMemberNames

Renames and re-exports getDeclarationNames

mergeVisitors

Re-exports mergeVisitors

Variables

Const MODULE_ALL

MODULE_ALL: "*" = "*"

Const MODULE_DEFAULT

MODULE_DEFAULT: "default" = "default"

Functions

createExportVisitors

  • createExportVisitors(exports?: Exports): Visitor
  • Create a Babel visitor that will find out all the exports and save them into an object ref.

    Parameters

    • Default value exports: Exports = { members: [] }

      The object ref to save the exports result.

    Returns Visitor

createExportVisitors

  • createExportVisitors(imports?: Import[]): Visitor
  • Create a Babel visitor that will find out all the imports and save them into an object ref.

    Parameters

    • Default value imports: Import[] = []

      The object ref to save the imports result.

    Returns Visitor

createRootRelationVisitors

  • createRootRelationVisitors(relations?: MemberRelation): Visitor
  • Create a Babel visitor that will find out the dependency relationships between root declarations, and save to an object ref.

    Parameters

    • Default value relations: MemberRelation = {}

      The object ref to save the relationships

    Returns Visitor

extractStats

  • extractStats(ast: File): { exports: Exports; imports: Import[]; relations: MemberRelation }
  • Extract imports, exports, and root declarations relations from an AST

    • Example: ``` const fs = require('fs'); const { parse } = require('@babel/parse');

    extractStats( parse( fs.readFileSync('esfile.js', 'utf-8'), { sourceType: 'module' plugins: ['jsx'] } ) );

    Parameters

    • ast: File

      File AST object

    Returns { exports: Exports; imports: Import[]; relations: MemberRelation }

    • exports: Exports
    • imports: Import[]
    • relations: MemberRelation

fileStats

  • fileStats(file: string, parserOptions?: ParserOptions): ReturnType<typeof extractStats>
  • Get ES file imports, exports, and root declaration definitions. Example:

    fileStats(
     fs.readFileSync('esfile.js', 'utf-8'),
     {
       plugins: ['jsx']
     }
    );

    Parameters

    • file: string

      File content

    • Optional parserOptions: ParserOptions

      Options supported by @babel/parser@^7.7.5

    Returns ReturnType<typeof extractStats>

getDeclarationNames

  • getDeclarationNames(node: Node): Array<MemberRef> | null
  • Extract declaration names and its alias from AST declaration nodes. This function only handles VariableDeclaration, FunctionDeclaration, ClassDeclaration.

    Parameters

    • node: Node

      AST node object

    Returns Array<MemberRef> | null

    A list of objects contain declaration name and alias

mergeVisitors

  • mergeVisitors(...visitors: Visitor[]): Visitor
  • Merge multiple @babel/traverse visitor objects into one. Example:

    mergeVisitors(
     {
       Identifier(node) {
         console.log(1);
       }
     },
     {
       Identifier(node) {
         console.log(2);
       }
     }
    );

    Parameters

    • Rest ...visitors: Visitor[]

      @babel/traverse visitors

    Returns Visitor

Generated using TypeDoc