webpack-gc-i18n-plugin 1.1.13 → 1.1.15

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/dist/index.cjs CHANGED
@@ -50,7 +50,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50
50
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51
51
  PERFORMANCE OF THIS SOFTWARE.
52
52
  ***************************************************************************** */
53
- /* global Reflect, Promise, SuppressedError, Symbol */
53
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
54
54
 
55
55
  var __assign = function () {
56
56
  __assign = Object.assign || function __assign(t) {
@@ -102,12 +102,8 @@ function __generator(thisArg, body) {
102
102
  f,
103
103
  y,
104
104
  t,
105
- g;
106
- return g = {
107
- next: verb(0),
108
- "throw": verb(1),
109
- "return": verb(2)
110
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
105
+ g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
106
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function () {
111
107
  return this;
112
108
  }), g;
113
109
  function verb(n) {
@@ -1474,7 +1470,12 @@ function hasOriginSymbols$1(value) {
1474
1470
  const originRegex = REGEX_MAP[exports.option.originLang];
1475
1471
  return originRegex ? originRegex.test(value) : false;
1476
1472
  }
1473
+ function isTemplateFragmentSegment(value) {
1474
+ return /[^\p{Script=Han}A-Za-z0-9_\s]/u.test(value);
1475
+ }
1477
1476
  function getTemplateSemanticSplitSegments(value) {
1477
+ const expressionCount = value.match(/\$\{[^}]+}/g)?.length || 0;
1478
+ if (expressionCount < 2) return [];
1478
1479
  const segments = value.split(/\$\{[^}]+}/g);
1479
1480
  const result = new Set();
1480
1481
  for (let index = 0; index < segments.length - 1; index++) {
@@ -1482,7 +1483,7 @@ function getTemplateSemanticSplitSegments(value) {
1482
1483
  const afterValue = segments[index + 1];
1483
1484
  if (!hasOriginSymbols$1(beforeValue) || !hasOriginSymbols$1(afterValue)) continue;
1484
1485
  [beforeValue, afterValue].forEach(segment => {
1485
- if (segment.trim().length > 1) {
1486
+ if (segment.trim().length > 1 && isTemplateFragmentSegment(segment)) {
1486
1487
  result.add(segment);
1487
1488
  }
1488
1489
  });
@@ -2269,6 +2270,8 @@ let TranslateTypeEnum = /*#__PURE__*/function (TranslateTypeEnum) {
2269
2270
  * @FilePath: /i18n_translation_vite/packages/autoI18nPluginCore/src/option.ts
2270
2271
  */
2271
2272
 
2273
+ const DEFAULT_ORIGIN_LANG = 'zh-cn';
2274
+ const DEFAULT_TARGET_LANG_LIST = ['zh-tw'];
2272
2275
  const EXCLUDED_CALL = ['$deepScan', 'console.info', 'console.warn', 'console.error', '$i8n', 'console.log', '$t', 'require', '$$i8n', '$$t', '_createCommentVNode'];
2273
2276
  /**
2274
2277
  * 默认插件配置选项
@@ -2293,10 +2296,10 @@ const DEFAULT_OPTION = {
2293
2296
  distPath: '',
2294
2297
  /** 打包后生成文件的主文件名称,默认是 'index' */
2295
2298
  distKey: 'index',
2296
- /** 来源语言,默认是中文 */
2297
- originLang: OriginLangKeyEnum.ZH,
2298
- /** 翻译目标语言列表,默认包含英文 */
2299
- targetLangList: ['en'],
2299
+ /** 来源语言,由插件固定使用默认值 */
2300
+ originLang: DEFAULT_ORIGIN_LANG,
2301
+ /** 翻译目标语言列表,由插件固定使用默认值 */
2302
+ targetLangList: [...DEFAULT_TARGET_LANG_LIST],
2300
2303
  /** 语言key,用于请求谷歌api和生成配置文件下对应语言的内容文件 */
2301
2304
  langKey: [],
2302
2305
  /** 命名空间,防止全局命名冲突 */
@@ -2401,6 +2404,8 @@ function initOption(optionInfo) {
2401
2404
  ...DEFAULT_OPTION,
2402
2405
  ...generateUserOption(optionInfo)
2403
2406
  };
2407
+ exports.option.originLang = DEFAULT_ORIGIN_LANG;
2408
+ exports.option.targetLangList = [...DEFAULT_TARGET_LANG_LIST];
2404
2409
 
2405
2410
  // 初始化语言key数组,包含来源语言和目标语言
2406
2411
  exports.option.langKey = [exports.option.originLang, ...exports.option.targetLangList];
@@ -2970,7 +2975,7 @@ function StringLiteralFn (insertOption) {
2970
2975
 
2971
2976
  // 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
2972
2977
  if (isTranslateCall(parent) || types__namespace.isImportDeclaration(parent) || parent.key === node || types__namespace.isCallExpression(parent) && extractFnName && (exports.option.excludedCall.includes(extractFnName) || extractFnName?.split('.')?.pop() && exports.option.excludedCall.includes(extractFnName?.split('.')?.pop() || ''))) return;
2973
- const vueVNodeCallPath = getVueCompiledVNodeCallPath(path);
2978
+ const vueVNodeInfo = getVueCompiledVNodeInfo(path);
2974
2979
  let replaceNode;
2975
2980
  if (exports.option.deepScan && checkNeedSplit(value)) {
2976
2981
  replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
@@ -2989,26 +2994,38 @@ function StringLiteralFn (insertOption) {
2989
2994
  });
2990
2995
  }
2991
2996
  path.replaceWith(replaceNode);
2992
- markVueCompiledVNodeDynamic(vueVNodeCallPath);
2997
+ markVueCompiledVNodeDynamic(vueVNodeInfo?.callPath, vueVNodeInfo?.dynamicPropName);
2993
2998
  }
2994
2999
  };
2995
3000
  }
2996
- function getVueCompiledVNodeCallPath(path) {
3001
+ function getVueCompiledVNodeInfo(path) {
2997
3002
  const callPath = path.parentPath;
2998
- if (!callPath || !types__namespace.isCallExpression(callPath.node)) return;
2999
- if (!isVueVNodeCreateCall(callPath.node)) return;
3000
- if (path.listKey !== 'arguments' || path.key !== 2) return;
3001
- return callPath;
3003
+ if (callPath && types__namespace.isCallExpression(callPath.node) && isVueVNodeCreateCall(callPath.node) && path.listKey === 'arguments' && path.key === 2) {
3004
+ return {
3005
+ callPath
3006
+ };
3007
+ }
3008
+ const propertyPath = path.parentPath;
3009
+ const propsPath = propertyPath?.parentPath;
3010
+ const vnodeCallPath = propsPath?.parentPath;
3011
+ if (!propertyPath || !types__namespace.isObjectProperty(propertyPath.node) || propertyPath.node.value !== path.node || propertyPath.node.computed || !propsPath || !types__namespace.isObjectExpression(propsPath.node) || propsPath.listKey !== 'arguments' || propsPath.key !== 1 || !vnodeCallPath || !types__namespace.isCallExpression(vnodeCallPath.node) || !isVueVNodeCreateCall(vnodeCallPath.node)) return;
3012
+ const key = propertyPath.node.key;
3013
+ const dynamicPropName = types__namespace.isIdentifier(key) ? key.name : types__namespace.isStringLiteral(key) ? key.value : undefined;
3014
+ if (!dynamicPropName) return;
3015
+ return {
3016
+ callPath: vnodeCallPath,
3017
+ dynamicPropName
3018
+ };
3002
3019
  }
3003
- function markVueCompiledVNodeDynamic(callPath) {
3020
+ function markVueCompiledVNodeDynamic(callPath, dynamicPropName) {
3004
3021
  if (!callPath || !types__namespace.isCallExpression(callPath.node)) return;
3005
- const patchFlag = callPath.node.arguments[3];
3006
- if (isCachedPatchFlag(patchFlag)) {
3007
- const textPatchFlag = types__namespace.numericLiteral(1);
3008
- textPatchFlag.leadingComments = null;
3009
- textPatchFlag.innerComments = null;
3010
- textPatchFlag.trailingComments = null;
3011
- callPath.node.arguments[3] = textPatchFlag;
3022
+ if (dynamicPropName) {
3023
+ markVueCompiledPropDynamic(callPath.node, dynamicPropName);
3024
+ } else {
3025
+ const patchFlag = callPath.node.arguments[3];
3026
+ if (isCachedPatchFlag(patchFlag)) {
3027
+ callPath.node.arguments[3] = createPatchFlag(1);
3028
+ }
3012
3029
  }
3013
3030
  const logicalPath = callPath.findParent(parentPath => {
3014
3031
  const node = parentPath.node;
@@ -3020,6 +3037,33 @@ function markVueCompiledVNodeDynamic(callPath) {
3020
3037
  logicalPath.replaceWith(callPath.node);
3021
3038
  }
3022
3039
  }
3040
+ function markVueCompiledPropDynamic(call, propName) {
3041
+ while (call.arguments.length < 3) {
3042
+ call.arguments.push(types__namespace.nullLiteral());
3043
+ }
3044
+ const patchFlag = call.arguments[3];
3045
+ if (!patchFlag || isCachedPatchFlag(patchFlag)) {
3046
+ call.arguments[3] = createPatchFlag(8);
3047
+ } else if (types__namespace.isNumericLiteral(patchFlag) && (patchFlag.value & 8) !== 8) {
3048
+ call.arguments[3] = createPatchFlag(patchFlag.value | 8);
3049
+ }
3050
+ const dynamicProps = call.arguments[4];
3051
+ if (types__namespace.isArrayExpression(dynamicProps)) {
3052
+ const hasProp = dynamicProps.elements.some(element => types__namespace.isStringLiteral(element) && element.value === propName);
3053
+ if (!hasProp) {
3054
+ dynamicProps.elements.push(types__namespace.stringLiteral(propName));
3055
+ }
3056
+ } else {
3057
+ call.arguments[4] = types__namespace.arrayExpression([types__namespace.stringLiteral(propName)]);
3058
+ }
3059
+ }
3060
+ function createPatchFlag(value) {
3061
+ const patchFlag = types__namespace.numericLiteral(value);
3062
+ patchFlag.leadingComments = null;
3063
+ patchFlag.innerComments = null;
3064
+ patchFlag.trailingComments = null;
3065
+ return patchFlag;
3066
+ }
3023
3067
  function isCachedPatchFlag(node) {
3024
3068
  return types__namespace.isNumericLiteral(node) && node.value === -1 || types__namespace.isUnaryExpression(node) && node.operator === '-' && types__namespace.isNumericLiteral(node.argument) && node.argument.value === 1;
3025
3069
  }
@@ -3399,8 +3443,6 @@ var webpackPluginsAutoI18n = /** @class */function () {
3399
3443
  translateKey: '$t',
3400
3444
  distPath: './dist/assets',
3401
3445
  distKey: 'index',
3402
- targetLangList: ['zh-cn', 'zh-tw'],
3403
- originLang: 'zh-cn',
3404
3446
  translator: new CNToTWTranslator()
3405
3447
  }, optionInfo));
3406
3448
  // 检查配置有效性,如果无效则不执行后续逻辑
@@ -3571,6 +3613,8 @@ function generateAdvancedRegex(extensions) {
3571
3613
 
3572
3614
  exports.BaiduTranslator = BaiduTranslator;
3573
3615
  exports.CNToTWTranslator = CNToTWTranslator;
3616
+ exports.DEFAULT_ORIGIN_LANG = DEFAULT_ORIGIN_LANG;
3617
+ exports.DEFAULT_TARGET_LANG_LIST = DEFAULT_TARGET_LANG_LIST;
3574
3618
  exports.EmptyTranslator = EmptyTranslator;
3575
3619
  exports.FunctionFactoryOption = FunctionFactoryOption;
3576
3620
  exports.GoogleTranslator = GoogleTranslator;