Created
April 10, 2017 15:04
-
-
Save satya164/25444eefcd7d00c9f422d792dbbdab1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @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