-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 2.22 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
73
var t = require('babel-types');
var chalk = require('chalk');
var moduleName = 'zent';
// 此处可以改成通用配置
function replaceRules(name) {
return moduleName +'/' + name.toLowerCase();
};
// import below is not allowed
function log (value) {
throw new Error(
chalk.yellow(
"\n" +
"====================================" + "\n" +
"import * as Zent from 'zent'" + "\n" +
"import XX from 'zent-xx'" + "\n" +
"const zent = require('zent')" + "\n" +
"以上三种import方式废弃,请使用 import { XX } from 'zent'" + "\n" +
"=========================================" + "\n" )
);
}
module.exports = function() {
return {
visitor: {
CallExpression(path, state) {
var node = path.node;
var args = node.arguments || [];
if (node.callee.name === "require" && args.length === 1 && t.isStringLiteral(args[0])) {
if (args[0].value === moduleName) {
log();
}
}
},
ImportDeclaration(path, state) {
var source = path.node.source;
var fullImports = path.node.specifiers.filter(function(specifier) { return specifier.type !== 'ImportSpecifier' });
var importSpecifiers = path.node.specifiers.filter(function(specifier) { return specifier.type === 'ImportSpecifier' });
if (fullImports.length > 0) {
var reg = /(zent$|zent-)/ig;
if (reg.test(source.value)) {
log();
}
}
var newImportDeclarations = [];
if (importSpecifiers.length > 0) {
importSpecifiers.forEach(function(importSpecifier) {
var importedName = importSpecifier.imported.name;
if (source.value === moduleName) {
var newImportedName = replaceRules(importedName);
var newImportDeclaration = t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(importedName))],
t.stringLiteral(newImportedName)
);
newImportDeclarations.push(newImportDeclaration);
}
})
}
if (newImportDeclarations.length > 0) {
path.replaceWithMultiple(newImportDeclarations);
}
}
}
}
}