Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* @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
You can’t perform that action at this time.