-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathPlugin.js
More file actions
413 lines (364 loc) · 13.7 KB
/
Copy pathPlugin.js
File metadata and controls
413 lines (364 loc) · 13.7 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { join } from 'path';
import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-imports';
/**
* 驼峰 -> 分隔符
* @param {*} _str 驼峰
* @param {*} symbol 分隔符
* @returns
*/
function transCamel(_str, symbol) {
// e.g. QRCode
// First match: QR
// Second match: Code
const cells = _str.match(/([A-Z]+(?=[A-Z]|$))|([A-Z]?[^A-Z]+)/g) || [];
return cells.map(c => c.toLowerCase()).join(symbol);
}
function winPath(path) {
return path.replace(/\\/g, '/');
}
function normalizeCustomName(originCustomName) {
// If set to a string, treat it as a JavaScript source file path.
if (typeof originCustomName === 'string') {
// eslint-disable-next-line import/no-dynamic-require
const customNameExports = require(originCustomName);
return typeof customNameExports === 'function' ? customNameExports : customNameExports.default;
}
return originCustomName;
}
export default class Plugin {
constructor(param) {
const {
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
alias,
transferNameOn,
types,
index = 0,
} = param;
if (!types) {
throw new Error('types不存在', { types });
}
/**
* 目标包名,如antd
*/
this.libraryName = libraryName;
/**
* 包里到处模块的文件夹,默认是lib
*/
this.libraryDirectory = typeof libraryDirectory === 'undefined' ? 'lib' : libraryDirectory;
/**
* 驼峰是否转化为'-'分隔符,默认是true
*/
this.camel2DashComponentName =
typeof camel2DashComponentName === 'undefined' ? true : camel2DashComponentName;
/**
* 驼峰是否转化为'_'分隔符
*/
this.camel2UnderlineComponentName = camel2UnderlineComponentName;
/**
* 是否导出当前组件的样式文件,不用管
*/
this.style = style || false;
/**
* 导出组件的样式文件的路径,不用太管
*/
this.styleLibraryDirectory = styleLibraryDirectory;
/**
* 根据导入的样式动态地决定该模块的引入路径,见 https://github.com/umijs/babel-plugin-import#customName,不用管
*/
this.customStyleName = normalizeCustomName(customStyleName);
this.fileName = fileName || '';
/**
* 根据导入的模块动态地决定该模块的引入路径,见 https://github.com/umijs/babel-plugin-import#customName
*/
this.customName = normalizeCustomName(customName);
/**
* 要处理的npm包没有默认导出时设为false, 若没配置则默认是 true
*/
this.transformToDefaultImport =
typeof transformToDefaultImport === 'undefined' ? true : transformToDefaultImport;
/**
* babel的types工具包
*/
this.types = types;
/**
* 每个插件实例的独立标识符
*/
this.pluginStateKey = `importPluginState${index}`;
/**
* The alias for the library, if provided
*/
this.alias = typeof alias === 'undefined' ? undefined : alias;
/**
* 是否根据导入的模块名转换导出文件名称,默认=true
*/
this.transferNameOn = typeof transferNameOn === 'undefined' ? true : transferNameOn;
}
/**
* 获取或初始化插件的状态对象,存储在 Babel 的 state 中。
* @param {*} state
* @returns
*/
getPluginState(state) {
if (!state[this.pluginStateKey]) {
state[this.pluginStateKey] = {}; // eslint-disable-line
}
return state[this.pluginStateKey];
}
ProgramEnter(path, state) {
const pluginState = this.getPluginState(state);
// 模块名称到本地别名的映射
pluginState.specified = Object.create(null);
// 默认导入模块的字典
pluginState.libraryObjs = Object.create(null);
// 指定导入的模块到解析后的导入路径的映射
pluginState.selectedMethods = Object.create(null);
pluginState.pathsToRemove = [];
}
ProgramExit(path, state) {
this.getPluginState(state).pathsToRemove.forEach(p => !p.removed && p.remove());
}
// ============================ 工具函数 ============================ //
/**
* 生成指定导入模块与之解析后的导入路径,以键值的形式储存在插件状态的selectedMethod下,并返回导入语句的identifier节点
* @param {*} methodName - 引入的模块
* @param {*} file - 当前正在处理的文件
* @param {*} pluginState - 插件状态
* @returns
*/
importMethod(methodName, file, pluginState) {
if (!pluginState.selectedMethods[methodName]) {
const { style, libraryDirectory, alias } = this;
let { transferNameOn } = this;
transferNameOn = !alias && transferNameOn;
let transformedMethodName = '';
if (transferNameOn && methodName !== 'default') {
transformedMethodName = this.camel2UnderlineComponentName
? transCamel(methodName, '_')
: this.camel2DashComponentName
? transCamel(methodName, '-')
: methodName;
}
const path = winPath(
this.customName
? this.customName(methodName, file)
: join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName),
);
pluginState.selectedMethods[methodName] =
methodName === 'default' || this.transformToDefaultImport
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName, file));
addSideEffect(file.path, `${stylePath}`);
} else if (this.styleLibraryDirectory) {
const stylePath = winPath(
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName),
);
addSideEffect(file.path, `${stylePath}`);
} else if (style === true) {
addSideEffect(file.path, `${path}/style`);
} else if (style === 'css') {
addSideEffect(file.path, `${path}/style/css`);
} else if (typeof style === 'function') {
const stylePath = style(path, file);
if (stylePath) {
addSideEffect(file.path, stylePath);
}
}
}
return { ...pluginState.selectedMethods[methodName] };
}
buildExpressionHandler(node, props, path, state) {
const file = (path && path.hub && path.hub.file) || (state && state.file);
const { types } = this;
const pluginState = this.getPluginState(state);
props.forEach(prop => {
if (!types.isIdentifier(node[prop])) return;
if (
pluginState.specified[node[prop].name] &&
types.isImportSpecifier(path.scope.getBinding(node[prop].name).path)
) {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
}
});
}
buildDeclaratorHandler(node, prop, path, state) {
const file = (path && path.hub && path.hub.file) || (state && state.file);
const { types } = this;
const pluginState = this.getPluginState(state);
const checkScope = targetNode =>
pluginState.specified[targetNode.name] && // eslint-disable-line
path.scope.hasBinding(targetNode.name) && // eslint-disable-line
path.scope.getBinding(targetNode.name).path.type === 'ImportSpecifier'; // eslint-disable-line
if (types.isIdentifier(node[prop]) && checkScope(node[prop])) {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
} else if (types.isSequenceExpression(node[prop])) {
node[prop].expressions.forEach((expressionNode, index) => {
if (types.isIdentifier(expressionNode) && checkScope(expressionNode)) {
node[prop].expressions[index] = this.importMethod(
pluginState.specified[expressionNode.name],
file,
pluginState,
); // eslint-disable-line
}
});
}
}
// ============================ 工具函数 ============================ //
// ============================ 处理节点的函数 ============================ //
/**
* 无需多言
* @param {*} path - 当前节点路径
* @param {*} state - 插件状态
* @returns
*/
ImportDeclaration(path, state) {
const { node } = path;
// path maybe removed by prev instances.
if (!node) return;
const { value } = node.source;
const { libraryName, alias } = this;
const { types } = this;
const pluginState = this.getPluginState(state);
// Check if the import source matches either the libraryName or the alias
if (value === libraryName || (alias && value.startsWith(alias))) {
node.specifiers.forEach(spec => {
if (types.isImportSpecifier(spec)) {
// Named import
pluginState.specified[spec.local.name] = spec.imported.name;
} else if (types.isImportDefaultSpecifier(spec)) {
// Default import
pluginState.specified[spec.local.name] = 'default';
} else {
// Namespace import or other
pluginState.libraryObjs[spec.local.name] = true;
}
});
// If an alias is used, replace it with the actual libraryName
if (alias && value.startsWith(alias)) {
node.source.value = types.StringLiteral(value.replace(alias, libraryName));
}
pluginState.pathsToRemove.push(path);
}
}
CallExpression(path, state) {
const { node } = path;
const file = (path && path.hub && path.hub.file) || (state && state.file);
const { name } = node.callee;
const { types } = this;
const pluginState = this.getPluginState(state);
if (types.isIdentifier(node.callee)) {
if (pluginState.specified[name]) {
// 替换节点的callee为转译后的导入语句
node.callee = this.importMethod(pluginState.specified[name], file, pluginState);
}
}
// 检查函数调用的参数是否也是导入的,是的话重复上述逻辑,多用于嵌套组件
node.arguments = node.arguments.map(arg => {
const { name: argName } = arg;
if (
pluginState.specified[argName] &&
path.scope.hasBinding(argName) &&
path.scope.getBinding(argName).path.type === 'ImportSpecifier'
) {
return this.importMethod(pluginState.specified[argName], file, pluginState);
}
return arg;
});
}
MemberExpression(path, state) {
const { node } = path;
const file = (path && path.hub && path.hub.file) || (state && state.file);
const pluginState = this.getPluginState(state);
// multiple instance check.
if (!node.object || !node.object.name) return;
if (pluginState.libraryObjs[node.object.name]) {
// antd.Button -> _Button
path.replaceWith(this.importMethod(node.property.name, file, pluginState));
} else if (pluginState.specified[node.object.name] && path.scope.hasBinding(node.object.name)) {
const { scope } = path.scope.getBinding(node.object.name);
// global variable in file scope
if (scope.path.parent.type === 'File') {
node.object = this.importMethod(pluginState.specified[node.object.name], file, pluginState);
}
}
}
Property(path, state) {
const { node } = path;
this.buildDeclaratorHandler(node, 'value', path, state);
}
VariableDeclarator(path, state) {
const { node } = path;
this.buildDeclaratorHandler(node, 'init', path, state);
}
ArrayExpression(path, state) {
const { node } = path;
const props = node.elements.map((_, index) => index);
this.buildExpressionHandler(node.elements, props, path, state);
}
LogicalExpression(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['left', 'right'], path, state);
}
ConditionalExpression(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['test', 'consequent', 'alternate'], path, state);
}
IfStatement(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['test'], path, state);
this.buildExpressionHandler(node.test, ['left', 'right'], path, state);
}
ExpressionStatement(path, state) {
const { node } = path;
const { types } = this;
if (types.isAssignmentExpression(node.expression)) {
this.buildExpressionHandler(node.expression, ['right'], path, state);
}
}
ReturnStatement(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['argument'], path, state);
}
ExportDefaultDeclaration(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['declaration'], path, state);
}
BinaryExpression(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['left', 'right'], path, state);
}
NewExpression(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['callee'], path, state);
const argumentsProps = node.arguments.map((_, index) => index);
this.buildExpressionHandler(node.arguments, argumentsProps, path, state);
}
SwitchStatement(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['discriminant'], path, state);
}
SwitchCase(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['test'], path, state);
}
ClassDeclaration(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['superClass'], path, state);
}
SequenceExpression(path, state) {
const { node } = path;
const expressionsProps = node.expressions.map((_, index) => index);
this.buildExpressionHandler(node.expressions, expressionsProps, path, state);
}
// ============================ 处理节点的函数 ============================ //
}