Skip to content

Instantly share code, notes, and snippets.

@satya164
Created April 10, 2017 15:04
Show Gist options
  • Save satya164/25444eefcd7d00c9f422d792dbbdab1b to your computer and use it in GitHub Desktop.
Save satya164/25444eefcd7d00c9f422d792dbbdab1b to your computer and use it in GitHub Desktop.
/* @flow */
import types from 'ast-types';
import { parse } from 'babylon';
const config = {
"sourceType": "module",
"plugins": [
"jsx",
"flow",
"objectRestSpread",
"classProperties",
"asyncGenerators",
],
};
const findModuleDependencies = (code: string): Array<string> => {
const dependencies: Set<string> = new Set();
const ast = parse(code, config);
types.visit(ast, {
visitImportDeclaration(path) {
dependencies.add(path.node.source.value);
this.traverse(path);
},
visitCallExpression(path) {
const { callee, arguments: args } = path.node;
if (callee.name === 'require' && args[0]) {
dependencies.add(args[0].value);
}
this.traverse(path);
},
});
return Array.from(dependencies);
};
export default findModuleDependencies;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment