forked from vue-styleguidist/vuepress-plugin-live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetImports.js
36 lines (34 loc) · 789 Bytes
/
getImports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const walkes = require("walkes");
const getAst = require("./getAst");
/**
* Returns a list of all strings used in import statements or require() calls
*/
module.exports = function getImports(code) {
// Parse example source code, but ignore errors
const ast = getAst(code);
if (!ast) {
return [];
}
const imports = [];
walkes(ast, {
// import foo from '
// import 'foo'
ImportDeclaration(node) {
if (node.source) {
imports.push(node.source.value);
}
},
// require('foo')
CallExpression(node) {
if (
node.callee &&
node.callee.name === "require" &&
node.arguments &&
node.arguments[0].value
) {
imports.push(node.arguments[0].value);
}
}
});
return imports;
};