mr-dep-walk

1.4.0 • Public • Published

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);
  },
});

Readme

Keywords

none

Package Sidebar

Install

npm i mr-dep-walk

Weekly Downloads

10,033

Version

1.4.0

License

ISC

Last publish

Collaborators

  • tmquinn
  • trentmwillis
  • rwjblue
  • stefanpenner