-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
44 lines (36 loc) · 1.13 KB
/
index.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
37
38
39
40
41
42
43
44
'use strict';
const types = require('ast-module-types');
const Walker = require('node-source-walk');
/**
* @param {String|Object} content - A file's string content or its AST
* @return {String[]} The file's dependencies
*/
module.exports = function(content) {
const walker = new Walker();
const dependencies = [];
walker.walk(content, node => {
if (!types.isRequire(node) || !node.arguments || node.arguments.length === 0) {
return;
}
if (types.isPlainRequire(node)) {
const result = extractDependencyFromRequire(node);
if (result) {
dependencies.push(result);
}
} else if (types.isMainScopedRequire(node)) {
dependencies.push(extractDependencyFromMainRequire(node));
}
});
return dependencies;
};
function extractDependencyFromRequire(node) {
if (node.arguments[0].type === 'Literal' || node.arguments[0].type === 'StringLiteral') {
return node.arguments[0].value;
}
if (node.arguments[0].type === 'TemplateLiteral') {
return node.arguments[0].quasis[0].value.raw;
}
}
function extractDependencyFromMainRequire(node) {
return node.arguments[0].value;
}