@vyr/pack-var-rename-plugin 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +74 -0
- package/package.json +1 -0
package/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const types = require('@babel/types');
|
|
2
|
+
const { parse } = require('@babel/parser')
|
|
3
|
+
const { default: traverse } = require('@babel/traverse')
|
|
4
|
+
const { default: generate } = require('@babel/generator')
|
|
5
|
+
|
|
6
|
+
module.exports = (packages, outputFile) => {
|
|
7
|
+
const varRename = (ast, depName) => {
|
|
8
|
+
// 记录已改过的作用域
|
|
9
|
+
const scopeRecord = {};
|
|
10
|
+
// 替换作用域内变量
|
|
11
|
+
function replaceScopeVarsName(path, name, newName) {
|
|
12
|
+
// 防止重复修改
|
|
13
|
+
if (scopeRecord[path.scope.uid]) return;
|
|
14
|
+
const item = path.scope.bindings[name]
|
|
15
|
+
// // 变量名应该唯一, 这里用随机字符凑合一下
|
|
16
|
+
// const newName = randomString()
|
|
17
|
+
// 替换变量名
|
|
18
|
+
item.identifier.name = newName
|
|
19
|
+
// 替换引用
|
|
20
|
+
item.referencePaths.forEach(function (refItem) {
|
|
21
|
+
refItem.node.name = newName;
|
|
22
|
+
})
|
|
23
|
+
scopeRecord[path.scope.uid] = true
|
|
24
|
+
}
|
|
25
|
+
const rebinds = []
|
|
26
|
+
traverse(ast, {
|
|
27
|
+
VariableDeclaration(path) {
|
|
28
|
+
path.node.declarations.forEach(declarator => {
|
|
29
|
+
// 检查该声明是否是通过 require 引入 {depName}
|
|
30
|
+
if (
|
|
31
|
+
declarator.init &&
|
|
32
|
+
declarator.init.type === 'CallExpression' &&
|
|
33
|
+
declarator.init.callee.name === 'require' &&
|
|
34
|
+
declarator.init.arguments.length === 1 &&
|
|
35
|
+
declarator.init.arguments[0].value === depName
|
|
36
|
+
) {
|
|
37
|
+
const replaceName = `__vyr_${declarator.id.name}`
|
|
38
|
+
replaceScopeVarsName(path, declarator.id.name, replaceName)
|
|
39
|
+
const newName = `'__${depName.toUpperCase().replace(/-/g, "_")}'`
|
|
40
|
+
rebinds.push({ replaceName, member: types.memberExpression(types.identifier('global'), types.identifier(newName), true) })
|
|
41
|
+
path.remove()
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
traverse(ast, {
|
|
48
|
+
Identifier(path) {
|
|
49
|
+
for (const rebind of rebinds) {
|
|
50
|
+
if (rebind.replaceName !== path.node.name) continue
|
|
51
|
+
path.replaceWith(rebind.member)
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const generateBundle = (options, bundle, isWrite) => {
|
|
59
|
+
const file = bundle[outputFile]
|
|
60
|
+
|
|
61
|
+
const ast = parse(file.code)
|
|
62
|
+
|
|
63
|
+
for (const _package of packages) varRename(ast, _package)
|
|
64
|
+
|
|
65
|
+
const { code } = generate(ast, { jsescOption: { minimal: true } })
|
|
66
|
+
|
|
67
|
+
file.code = code
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
name: 'varRename',
|
|
72
|
+
generateBundle,
|
|
73
|
+
}
|
|
74
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@vyr/pack-var-rename-plugin","version":"0.0.4","description":"","main":"./index.js","author":"","license":"ISC"}
|