forked from gpittarelli/list-imports-exports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (65 loc) · 1.78 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const babylon = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const t = require('@babel/types');
function parse(code, extraPlugins = []) {
// accept either pre-parsed ast or string of code
if (typeof code === 'string') {
code = babylon.parse(code, {
sourceType: 'module',
plugins: [
'asyncGenerators',
'classConstructorCall',
'classProperties',
'nullishCoalescingOperator',
'decorators-legacy',
'doExpressions',
'exportDefaultFrom',
'exportExtensions',
'functionSent',
'functionBind',
'jsx',
'objectRestSpread',
'optionalChaining',
'dynamicImport',
...extraPlugins
]
});
}
var output = {
imports: [],
exports: [],
passthroughs: []
};
traverse(code, {
enter: function(path) {
var node = path.node;
if (path.isExportNamedDeclaration()) {
if (node.source) {
var from = node.source.value;
output.imports.push(from);
node.specifiers.forEach(function (spec) {
var orig = spec.local ? spec.local.name : 'default',
exp = spec.exported.name;
output.exports.push(exp);
output.passthroughs.push([exp, from, orig]);
});
} else {
output.exports = output.exports.concat(
node.specifiers.map(function (spec) {
return spec.exported.name;
})
);
}
} else if (path.isImportDeclaration()) {
var from = node.source.value;
output.imports.push(from);
} else if (path.isExportDefaultDeclaration()) {
output.exports.push('default');
}
}
});
return output;
}
module.exports = {
parse: parse
};