vue-i18n-extract-plugin 1.0.78 → 1.0.79
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/lib/visitors.js +48 -12
- package/package.json +1 -1
package/lib/visitors.js
CHANGED
|
@@ -18,10 +18,8 @@ function isTFunction(node, option) {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
function isVNodeCall(path, nodeName) {
|
|
21
|
-
const node = path.node;
|
|
22
21
|
if (!path.isCallExpression()) return false;
|
|
23
|
-
|
|
24
|
-
const callee = node.callee;
|
|
22
|
+
const callee = path.node.callee;
|
|
25
23
|
return (
|
|
26
24
|
(t.isIdentifier(callee) && callee.name === nodeName) ||
|
|
27
25
|
(t.isMemberExpression(callee) &&
|
|
@@ -56,7 +54,23 @@ function getPropKey(propNode) {
|
|
|
56
54
|
return null;
|
|
57
55
|
}
|
|
58
56
|
|
|
59
|
-
|
|
57
|
+
/**
|
|
58
|
+
* 向 hoisted dynamicProps 数组中追加 propKey(若不存在)
|
|
59
|
+
* @returns 是否真的发生了修改
|
|
60
|
+
*/
|
|
61
|
+
function pushDynamicPropsIfMissing(arr, propKey) {
|
|
62
|
+
const hasKey = arr.elements.some(
|
|
63
|
+
el => t.isStringLiteral(el) && el.value === propKey
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
if (!hasKey) {
|
|
67
|
+
arr.elements.push(t.stringLiteral(propKey));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return !hasKey;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 自动补齐动态属性,如 createVNode(c, {message: dynamicValue}) => createVNode(c, {message: dynamicValue}, null, 8, ['message'])
|
|
60
74
|
function transformDirectiveIfNeeded(path, parentPath) {
|
|
61
75
|
let hasCreateVNode = false;
|
|
62
76
|
// 属性值情况,如 title: "xxx"
|
|
@@ -66,11 +80,7 @@ function transformDirectiveIfNeeded(path, parentPath) {
|
|
|
66
80
|
if (!propKey) return;
|
|
67
81
|
|
|
68
82
|
const vNodeCall = path.findParent(
|
|
69
|
-
p =>
|
|
70
|
-
p.isCallExpression() &&
|
|
71
|
-
(t.isIdentifier(p.node.callee, { name: "_createVNode" }) ||
|
|
72
|
-
(t.isMemberExpression(p.node.callee) &&
|
|
73
|
-
t.isIdentifier(p.node.callee.property, { name: "_createVNode" })))
|
|
83
|
+
p => isVNodeCall(p, "_createVNode") || isVNodeCall(p, "_createElementVNode")
|
|
74
84
|
);
|
|
75
85
|
|
|
76
86
|
if (vNodeCall) {
|
|
@@ -91,13 +101,23 @@ function transformDirectiveIfNeeded(path, parentPath) {
|
|
|
91
101
|
}
|
|
92
102
|
|
|
93
103
|
// 设置 patchFlag = 8
|
|
94
|
-
args[patchFlagIndex]
|
|
104
|
+
if (!args[patchFlagIndex]) {
|
|
105
|
+
args[patchFlagIndex] = t.numericLiteral(8);
|
|
106
|
+
}
|
|
95
107
|
|
|
96
108
|
// 设置 dynamicProps = ["propKey"]
|
|
97
109
|
const existingDynamicProps = args[dynamicPropsIndex];
|
|
98
|
-
|
|
110
|
+
|
|
111
|
+
if (t.isIdentifier(existingDynamicProps)) {
|
|
112
|
+
const arr = hoistedMap.get(existingDynamicProps.name);
|
|
113
|
+
if (arr) {
|
|
114
|
+
pushDynamicPropsIfMissing(arr, propKey);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!existingDynamicProps) {
|
|
99
119
|
args[dynamicPropsIndex] = t.arrayExpression([t.stringLiteral(propKey)]);
|
|
100
|
-
} else {
|
|
120
|
+
} else if (t.isArrayExpression(existingDynamicProps)) {
|
|
101
121
|
const existingKeys = new Set(
|
|
102
122
|
existingDynamicProps.elements
|
|
103
123
|
.filter(el => t.isStringLiteral(el))
|
|
@@ -284,10 +304,26 @@ function isAnyVNodeCall(path) {
|
|
|
284
304
|
return false;
|
|
285
305
|
}
|
|
286
306
|
|
|
307
|
+
const hoistedMap = new Map();
|
|
308
|
+
|
|
287
309
|
function createI18nVisitor(option, i18nMap) {
|
|
288
310
|
const excludedCall = [...option.excludedCall, ...EXCLUDED_CALL];
|
|
289
311
|
|
|
290
312
|
return {
|
|
313
|
+
Program(path) {
|
|
314
|
+
path.traverse({
|
|
315
|
+
VariableDeclarator(p) {
|
|
316
|
+
const { id, init } = p.node;
|
|
317
|
+
if (
|
|
318
|
+
t.isIdentifier(id) &&
|
|
319
|
+
t.isArrayExpression(init) &&
|
|
320
|
+
id.name.startsWith("_hoisted_")
|
|
321
|
+
) {
|
|
322
|
+
hoistedMap.set(id.name, init);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
},
|
|
291
327
|
CallExpression(path) {
|
|
292
328
|
if (!isTFunction(path.node, option)) {
|
|
293
329
|
if (isVNodeCall(path, "_createTextVNode")) {
|