forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-require-statements.js
More file actions
41 lines (37 loc) · 1.15 KB
/
get-require-statements.js
File metadata and controls
41 lines (37 loc) · 1.15 KB
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
export default function getRequireStatements(metadata) {
if (!metadata) return [];
const res = {};
if (metadata.strings) {
metadata.strings.forEach(s => {
res[s.value] = 'direct';
});
}
if (metadata.expressions) {
metadata.expressions.forEach(s => {
if (s.type === 'BinaryExpression') {
res[s.left.value] = 'glob';
} else if (s.type === 'TemplateLiteral') {
res[s.quasis[0].value.raw] = 'glob';
} else if (
s.type === 'CallExpression' &&
s.callee.type === 'MemberExpression' &&
s.callee.property.type === 'Identifier' &&
s.callee.property.name === 'concat' &&
s.arguments &&
s.arguments[0] &&
s.arguments[0].left &&
s.arguments[0].left.type === 'BinaryExpression' &&
s.arguments[0].left.left.type === 'StringLiteral'
) {
// require("".concat('./assets/' + ... + '.js');
res[s.arguments[0].left.left.value] = 'glob';
}
});
}
const total = [];
const paths = Object.keys(res);
for (let i = 0; i < paths.length; i += 1) {
total.push({ path: paths[i], type: res[paths[i]] });
}
return total;
}