vue-i18n-extract-plugin 1.0.52 → 1.0.53

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/README.md CHANGED
@@ -53,6 +53,7 @@ const defaultOptions = {
53
53
  autoImportI18n: true, // 是否自动导入 i18n 模块
54
54
  autoTranslate: true, // 提取完成后是否自动翻译
55
55
  cleanTranslate: true, // 是否清理无用的翻译内容
56
+ keepRaw: false, // 开启后只做转换不生成hash值,即:"测试" -> $t("测试"), 开启rewrite时生效
56
57
  enabled: true, // 是否启用插件
57
58
  outputJsonFileInPlugin: true, // 是否在插件中输出 JSON 文件
58
59
  outputJsonFileDebounceTimeInPlugin: 2000, // 输出 JSON 文件的防抖时间
package/lib/extract.js CHANGED
@@ -30,6 +30,11 @@ const { autoTranslate, cleanTranslate, cleanI18nMap } = require("./translate");
30
30
  const { i18nImportAstTransform } = require("./import-i18n-transform");
31
31
 
32
32
  let globalI18nMap = {};
33
+ const keepRawTextOptions = {
34
+ jsescOption: {
35
+ minimal: true // 保留原始字符串,不被转义成Unicode
36
+ }
37
+ };
33
38
 
34
39
  function encodeToString(str) {
35
40
  return str.indexOf("'") === -1 ? `'${str}'` : `"${str}"`;
@@ -68,7 +73,7 @@ function unwrapTransformedCode(ast) {
68
73
  });
69
74
 
70
75
  if (extractedNode) {
71
- const { code: exprCode } = generate(extractedNode);
76
+ const { code: exprCode } = generate(extractedNode, keepRawTextOptions);
72
77
  return exprCode;
73
78
  } else {
74
79
  console.warn("未能提取到 _expr 表达式");
@@ -118,7 +123,7 @@ function extractTCallsFromInterpolation(code, options, i18nMap) {
118
123
  traverse(program, createI18nVisitor(options, i18nMap));
119
124
 
120
125
  if (options.rewrite) {
121
- return generate(ast, { compact: true }).code;
126
+ return generate(ast, { compact: true, ...keepRawTextOptions }).code;
122
127
  }
123
128
 
124
129
  // return ast;
@@ -252,7 +257,7 @@ function transformScript(code, options, useAst, i18nMap) {
252
257
  }
253
258
  return {
254
259
  changed: true,
255
- code: generate(ast, { retainLines: true }).code
260
+ code: generate(ast, { retainLines: true, ...keepRawTextOptions }).code
256
261
  };
257
262
  }
258
263
 
package/lib/options.js CHANGED
@@ -7,6 +7,7 @@ const defaultOptions = {
7
7
  autoImportI18n: true, // 是否自动导入 i18n 模块
8
8
  autoTranslate: true, // 提取完成后是否自动翻译
9
9
  cleanTranslate: true, // 是否清理无用的翻译内容
10
+ keepRaw: false, // 开启后只做转换不生成hash值,即:"测试" -> $t("测试"), 开启rewrite时生效
10
11
  enabled: true, // 是否启用插件
11
12
  outputJsonFileInPlugin: true, // 是否在插件中输出 JSON 文件
12
13
  outputJsonFileDebounceTimeInPlugin: 2000, // 输出 JSON 文件的防抖时间
package/lib/visitors.js CHANGED
@@ -106,6 +106,13 @@ function shouldTransform(path) {
106
106
  );
107
107
  }
108
108
 
109
+ function generateText(rawText, hashedText, options) {
110
+ if (options.keepRaw && options.rewrite) {
111
+ return rawText;
112
+ }
113
+ return hashedText;
114
+ }
115
+
109
116
  function createI18nVisitor(option, i18nMap) {
110
117
  const excludedCall = [...option.excludedCall, ...EXCLUDED_CALL];
111
118
 
@@ -136,7 +143,7 @@ function createI18nVisitor(option, i18nMap) {
136
143
  i18nMap[hashed] = keyText;
137
144
  }
138
145
 
139
- const newArg = t.stringLiteral(hashed);
146
+ const newArg = t.stringLiteral(generateText(keyText, hashed, option));
140
147
  path.node.arguments[0] = newArg;
141
148
  },
142
149
 
@@ -192,7 +199,7 @@ function createI18nVisitor(option, i18nMap) {
192
199
  t.identifier("_ctx"),
193
200
  t.identifier(option.translateKey)
194
201
  ),
195
- [t.stringLiteral(hashed)]
202
+ [t.stringLiteral(generateText(value, hashed, option))]
196
203
  );
197
204
 
198
205
  // 判断是否createTextVNode或MemberExpression(如 Vue.createTextVNode)
@@ -219,7 +226,7 @@ function createI18nVisitor(option, i18nMap) {
219
226
  if (!hasCreateVNode) {
220
227
  // 生成 $t("hashed")
221
228
  callExpression = t.callExpression(t.identifier(option.translateKey), [
222
- t.stringLiteral(hashed)
229
+ t.stringLiteral(generateText(value, hashed, option))
223
230
  ]);
224
231
  }
225
232
  }
@@ -267,7 +274,7 @@ function createI18nVisitor(option, i18nMap) {
267
274
  }
268
275
 
269
276
  // 替换为字符类型翻译节点
270
- const tCallExpression = `${option.translateKey}('${hashed}')`;
277
+ const tCallExpression = `${option.translateKey}('${generateText(value, hashed, option)}')`;
271
278
  node.value.raw = node.value.cooked = `\${${tCallExpression}}`;
272
279
  },
273
280
  JSXText(path) {
@@ -291,7 +298,7 @@ function createI18nVisitor(option, i18nMap) {
291
298
  path.replaceWith(
292
299
  t.jsxExpressionContainer(
293
300
  t.callExpression(t.identifier(option.translateKey), [
294
- t.stringLiteral(hashed)
301
+ t.stringLiteral(generateText(text, hashed, option))
295
302
  ])
296
303
  )
297
304
  );
@@ -318,7 +325,7 @@ function createI18nVisitor(option, i18nMap) {
318
325
  path.replaceWith(
319
326
  t.jsxExpressionContainer(
320
327
  t.callExpression(t.identifier(option.translateKey), [
321
- t.stringLiteral(hashed)
328
+ t.stringLiteral(generateText(value, hashed, option))
322
329
  ])
323
330
  )
324
331
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-i18n-extract-plugin",
3
- "version": "1.0.52",
3
+ "version": "1.0.53",
4
4
  "main": "lib/index.js",
5
5
  "types": "types/index.d.ts",
6
6
  "bin": {