vue-i18n-extract-plugin 1.0.71 → 1.0.73

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.
Files changed (2) hide show
  1. package/lib/visitors.js +52 -13
  2. package/package.json +1 -1
package/lib/visitors.js CHANGED
@@ -153,14 +153,14 @@ function generateJSXElement(name, id, msg) {
153
153
  return t.jsxElement(openingElement, null, [], true);
154
154
  }
155
155
 
156
- function unwrapVueCacheIfNeeded(path) {
156
+ function unwrapVueCacheCallIfNeeded(path) {
157
157
  const assignPath = path.parentPath;
158
- if (!assignPath || !assignPath.isAssignmentExpression({ operator: "=" })) {
158
+ if (!assignPath?.isAssignmentExpression({ operator: "=" })) {
159
159
  return false;
160
160
  }
161
161
 
162
162
  const logicalPath = assignPath.parentPath;
163
- if (!logicalPath || !logicalPath.isLogicalExpression({ operator: "||" })) {
163
+ if (!logicalPath?.isLogicalExpression({ operator: "||" })) {
164
164
  return false;
165
165
  }
166
166
 
@@ -169,8 +169,8 @@ function unwrapVueCacheIfNeeded(path) {
169
169
  // 校验 left: _cache[x]
170
170
  const isCacheMember =
171
171
  t.isMemberExpression(left) &&
172
- (t.isIdentifier(left.object, { name: "_cache" }) ||
173
- (t.isIdentifier(left.object) && left.object.name.startsWith("_cache")));
172
+ t.isIdentifier(left.object) &&
173
+ left.object.name.startsWith("_cache");
174
174
 
175
175
  // 校验 right: (_cache[x] = createTextVNode(...))
176
176
  const isSameAssignment =
@@ -187,6 +187,38 @@ function unwrapVueCacheIfNeeded(path) {
187
187
  return true;
188
188
  }
189
189
 
190
+ function unwrapVueSlotArrayCache(path) {
191
+ // 1. 找 ArrayExpression
192
+ const arrayPath = path.findParent(p => p.isArrayExpression());
193
+ if (!arrayPath) return false;
194
+
195
+ // 2. array 是 assignment 的 right
196
+ const assignPath = arrayPath.parentPath;
197
+ if (!assignPath?.isAssignmentExpression({ operator: "=" })) return false;
198
+
199
+ // 3. assignment 在 LogicalExpression || 中
200
+ const logicalPath = assignPath.parentPath;
201
+ if (!logicalPath?.isLogicalExpression({ operator: "||" })) return false;
202
+
203
+ // 4. logical 在 SpreadElement 中
204
+ const spreadPath = logicalPath.parentPath;
205
+ if (!spreadPath?.isSpreadElement()) return false;
206
+
207
+ // 5. 校验 _cache[x]
208
+ const left = logicalPath.node.left;
209
+ const isCache =
210
+ t.isMemberExpression(left) &&
211
+ t.isIdentifier(left.object) &&
212
+ left.object.name.startsWith("_cache");
213
+
214
+ if (!isCache) return false;
215
+
216
+ // unwrap:用 array.elements 替换 spread(...)
217
+ spreadPath.replaceWithMultiple(arrayPath.node.elements);
218
+
219
+ return true;
220
+ }
221
+
190
222
  function isDynamicTextExpression(node) {
191
223
  // 只要不是 StringLiteral,100% 是动态文本
192
224
  return !t.isStringLiteral(node);
@@ -309,19 +341,26 @@ function createI18nVisitor(option, i18nMap) {
309
341
  if (isVNodeCall(parentPath, "_createTextVNode")) {
310
342
  // createTextVNode强制补充第二个参数 1
311
343
  parentPath.node.arguments = [callExpression, t.numericLiteral(1)];
312
- // 尝试拆 cache
313
- unwrapVueCacheIfNeeded(parentPath);
344
+ // 先尝试 [...(_cache2[4] || (_cache2[4] = [ createTextVNode(...) ]))]
345
+ if (!unwrapVueSlotArrayCache(parentPath)) {
346
+ // 再尝试拆 _cache2[0] || (_cache2[0] = createTextVNode(...))
347
+ unwrapVueCacheCallIfNeeded(parentPath);
348
+ }
314
349
  // 是否 vue.createElementVNode(...),且该 StringLiteral 是第三个参数
315
350
  } else if (isVNodeCall(parentPath, "_createElementVNode")) {
316
351
  const callExpr = parentPath.node;
317
352
  const args = callExpr.arguments;
318
-
319
- // 当前是第三个参数,且参数数量为3
353
+ // 第 3 个参数是 children
320
354
  const argIndex = args.findIndex(arg => arg === path.node);
321
- if (argIndex === 2 && args.length === 3) {
322
- args[2] = callExpression; // 替换第3个参数
323
- args.push(t.numericLiteral(1)); // 添加第4个参数
324
- return;
355
+ if (argIndex === 2) {
356
+ args[2] = callExpression;
357
+ // 强制 patchFlag = 1
358
+ if (args.length < 4) {
359
+ args.push(t.numericLiteral(1));
360
+ } else {
361
+ args[3] = t.numericLiteral(1);
362
+ }
363
+ unwrapVueCacheCallIfNeeded(parentPath);
325
364
  }
326
365
  } else {
327
366
  const hasCreateVNode = transformDirectiveIfNeeded(path, parentPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-i18n-extract-plugin",
3
- "version": "1.0.71",
3
+ "version": "1.0.73",
4
4
  "main": "lib/index.js",
5
5
  "types": "types/index.d.ts",
6
6
  "bin": {