1.4.0 • Published 6 years ago

mr-dep-walk v1.4.0

Weekly downloads
13,628
License
ISC
Repository
github
Last release
6 years ago

mr-dep-walk

Build Status Build status

This library extracts dependent files from both ES6 module syntax, and AMD module syntax;

Usage

yarn add mr-dep-walk
const {
  depFilesFromFile,
  depsFromFile,
  depsFromSource,
  depsFromAST
} = require('mr-dep-walk');

For depFilesFromFile given an entry file, it will produce a list of all dependent files (recursively):

// file.js
import x from 'y';

// y.js
depFilesFromFile({
  entry: 'file.js',
  /* cwd: optional, */
  /* parse: optional,  */
}); // => 'y.js';

For depsFromFile given a file, it will produce a list of its immediate dependent moduleNames;

// file.js
import x from 'y';

// y.js
depsFromFile({
  entry: 'file.js',
  /* cwd: optional, */
  /* parse: optional,  */
}); // => 'y';

For depsFromSource given the raw source, it will produce a list of its immediate dependent moduleNames;

depsFromSource(`import x from 'y'`/*, options */); // => 'y'

For depsFromAST given the AST, it will produce a list of its immediate dependent moduleNames;

depsFromSource(acorn.parse(`import x from 'y'`, {
  ecmaVersion: 8,
  sourceType: 'module'
})); // => 'y'

Custom Parse Step

By default mr-dep-walk will use:

source => acorn.parse(source, { ecmaVersion: 8, sourceType: 'module'})

But some methods (depFilesFromFile, depsFromFile, depsFromSource) support an alt-parser, example:

depFilesFromFile('some-file.js', {
  entry: 'foo.js',
  parse(source) {
    return customParser(source);
  },
});