vue-i18n-extract-plugin 1.0.77 → 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 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
- // 将静态属性转换为动态属性,如 <div title="xxx" /> => <div :title="$t('hashed')" />
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] = t.numericLiteral(8);
104
+ if (!args[patchFlagIndex]) {
105
+ args[patchFlagIndex] = t.numericLiteral(8);
106
+ }
95
107
 
96
108
  // 设置 dynamicProps = ["propKey"]
97
109
  const existingDynamicProps = args[dynamicPropsIndex];
98
- if (!existingDynamicProps || !t.isArrayExpression(existingDynamicProps)) {
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")) {
@@ -43,6 +43,8 @@ function vitePluginI18n(option) {
43
43
  config = resolvedConfig;
44
44
  },
45
45
  async transform(code, path) {
46
+ path = path.split('?')[0];
47
+
46
48
  if (!option.enabled || !filter(path)) return;
47
49
 
48
50
  return transformAsync(code, {
@@ -11,6 +11,8 @@ function vitePluginImportI18n(option) {
11
11
  name: "vite-plugin-import-i18n",
12
12
  enforce: "pre",
13
13
  async transform(code, path) {
14
+ path = path.split('?')[0];
15
+
14
16
  if (!option.enabled || !option.autoImportI18n || !filter(path)) return;
15
17
 
16
18
  return i18nImportTransform(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-i18n-extract-plugin",
3
- "version": "1.0.77",
3
+ "version": "1.0.79",
4
4
  "main": "lib/index.js",
5
5
  "types": "types/index.d.ts",
6
6
  "bin": {