-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel-plugin-module-iterator.js
188 lines (166 loc) · 5.18 KB
/
babel-plugin-module-iterator.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const fs = require('fs');
const path = require('path');
const visitedModules = new Set();
const IMPORT_TYPE = {
deconstruct: 'deconstruct',
default: 'default',
namespace: 'namespace'
}
const EXPORT_TYPE = {
all: 'all',
default: 'default',
named: 'named'
}
class DependencyNode {
constructor(path = '', imports = {}, exports = []) {
this.path = path;
this.imports = imports;
this.exports = exports;
this.subModules = {};
}
}
function resolveBabelSyntaxtPlugins(modulePath) {
const plugins = [];
if (['.tsx', '.jsx'].some(ext => modulePath.endsWith(ext))) {
plugins.push('jsx');
}
if (['.ts', '.tsx'].some(ext => modulePath.endsWith(ext))) {
plugins.push('typescript');
}
return plugins;
}
function isDirectory(filePath) {
try {
return fs.statSync(filePath).isDirectory()
} catch (e) { }
return false;
}
function completeModulePath(modulePath) {
const EXTS = ['.tsx', '.ts', '.jsx', '.js'];
if (modulePath.match(/\.[a-zA-Z]+$/)) {
return modulePath;
}
function tryCompletePath(resolvePath) {
for (let i = 0; i < EXTS.length; i++) {
let tryPath = resolvePath(EXTS[i]);
if (fs.existsSync(tryPath)) {
return tryPath;
}
}
}
function reportModuleNotFoundError(modulePath) {
throw 'module not found: ' + modulePath;
}
if (isDirectory(modulePath)) {
const tryModulePath = tryCompletePath((ext) => path.join(modulePath, 'index' + ext));
if (!tryModulePath) {
reportModuleNotFoundError(modulePath);
} else {
return tryModulePath;
}
} else if (!EXTS.some(ext => modulePath.endsWith(ext))) {
const tryModulePath = tryCompletePath((ext) => modulePath + ext);
if (!tryModulePath) {
reportModuleNotFoundError(modulePath);
} else {
return tryModulePath;
}
}
return modulePath;
}
function moduleResolver(curModulePath, requirePath) {
requirePath = path.resolve(path.dirname(curModulePath), requirePath);
// 过滤掉第三方模块
if (requirePath.includes('node_modules')) {
return '';
}
requirePath = completeModulePath(requirePath);
if (visitedModules.has(requirePath)) {
return '';
} else {
visitedModules.add(requirePath);
}
return requirePath;
}
function traverseJsModule(curModulePath, dependencyGrapthNode, allModules) {
const moduleFileContent = fs.readFileSync(curModulePath, {
encoding: 'utf-8'
});
dependencyGrapthNode.path = curModulePath;
const ast = parser.parse(moduleFileContent, {
sourceType: 'unambiguous',
plugins: resolveBabelSyntaxtPlugins(curModulePath)
});
traverse(ast, {
ImportDeclaration(path) {
const subModulePath = moduleResolver(curModulePath, path.get('source.value').node);
if (!subModulePath) {
return;
}
const specifierPaths = path.get('specifiers');
dependencyGrapthNode.imports[subModulePath] = specifierPaths.map(specifierPath => {
if (specifierPath.isImportSpecifier()) {
return {
type: IMPORT_TYPE.deconstruct,
imported: specifierPath.get('imported').node.name,
local: specifierPath.get('local').node.name
}
} else if (specifierPath.isImportDefaultSpecifier()) {
return {
type: IMPORT_TYPE.default,
local: specifierPath.get('local').node.name
}
} else {
return {
type: IMPORT_TYPE.namespace,
local: specifierPath.get('local').node.name
}
}
});
const subModule = new DependencyNode();
traverseJsModule(subModulePath, subModule, allModules);
dependencyGrapthNode.subModules[subModule.path] = subModule;
},
ExportDeclaration(path) {
if (path.isExportNamedDeclaration()) {
const specifiers = path.get('specifiers');
dependencyGrapthNode.exports = specifiers.map(specifierPath => ({
type: EXPORT_TYPE.named,
exported: specifierPath.get('exported').node.name,
local: specifierPath.get('local').node.name
}));
} else if (path.isExportDefaultDeclaration()) {
let exportName;
const declarationPath = path.get('declaration');
if (declarationPath.isAssignmentExpression()) {
exportName = declarationPath.get('left').toString();
} else {
exportName = declarationPath.toString()
}
dependencyGrapthNode.exports.push({
type: EXPORT_TYPE.default,
exported: exportName
});
} else {
dependencyGrapthNode.exports.push({
type: EXPORT_TYPE.all,
exported: path.get('exported').node.name,
source: path.get('source').node.value
});
}
}
});
allModules[curModulePath] = dependencyGrapthNode;
}
function traverseModule(curModulePath) {
const dependencyGraph = {
root: new DependencyNode(),
allModules: {}
};
traverseJsModule(curModulePath, dependencyGraph.root, dependencyGraph.allModules);
return dependencyGraph;
}
const dependencyGraph = traverseModule(path.resolve(__dirname, '../src/test-module/index.js'))
console.log(JSON.stringify(dependencyGraph, null, 4));