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.
@@ -25,7 +25,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
25
25
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
26
26
  PERFORMANCE OF THIS SOFTWARE.
27
27
  ***************************************************************************** */
28
- /* global Reflect, Promise, SuppressedError, Symbol */
28
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
29
29
 
30
30
  var __assign = function () {
31
31
  __assign = Object.assign || function __assign(t) {
@@ -77,12 +77,8 @@ function __generator(thisArg, body) {
77
77
  f,
78
78
  y,
79
79
  t,
80
- g;
81
- return g = {
82
- next: verb(0),
83
- "throw": verb(1),
84
- "return": verb(2)
85
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
80
+ g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
81
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function () {
86
82
  return this;
87
83
  }), g;
88
84
  function verb(n) {
@@ -678,7 +674,12 @@ function hasOriginSymbols$1(value) {
678
674
  const originRegex = REGEX_MAP[option.originLang];
679
675
  return originRegex ? originRegex.test(value) : false;
680
676
  }
677
+ function isTemplateFragmentSegment(value) {
678
+ return /[^\p{Script=Han}A-Za-z0-9_\s]/u.test(value);
679
+ }
681
680
  function getTemplateSemanticSplitSegments(value) {
681
+ const expressionCount = value.match(/\$\{[^}]+}/g)?.length || 0;
682
+ if (expressionCount < 2) return [];
682
683
  const segments = value.split(/\$\{[^}]+}/g);
683
684
  const result = new Set();
684
685
  for (let index = 0; index < segments.length - 1; index++) {
@@ -686,7 +687,7 @@ function getTemplateSemanticSplitSegments(value) {
686
687
  const afterValue = segments[index + 1];
687
688
  if (!hasOriginSymbols$1(beforeValue) || !hasOriginSymbols$1(afterValue)) continue;
688
689
  [beforeValue, afterValue].forEach(segment => {
689
- if (segment.trim().length > 1) {
690
+ if (segment.trim().length > 1 && isTemplateFragmentSegment(segment)) {
690
691
  result.add(segment);
691
692
  }
692
693
  });
@@ -1045,6 +1046,8 @@ let TranslateTypeEnum = /*#__PURE__*/function (TranslateTypeEnum) {
1045
1046
  * @FilePath: /i18n_translation_vite/packages/autoI18nPluginCore/src/option.ts
1046
1047
  */
1047
1048
 
1049
+ const DEFAULT_ORIGIN_LANG = 'zh-cn';
1050
+ const DEFAULT_TARGET_LANG_LIST = ['zh-tw'];
1048
1051
  /**
1049
1052
  * 默认插件配置选项
1050
1053
  */
@@ -1068,10 +1071,10 @@ const DEFAULT_OPTION = {
1068
1071
  distPath: '',
1069
1072
  /** 打包后生成文件的主文件名称,默认是 'index' */
1070
1073
  distKey: 'index',
1071
- /** 来源语言,默认是中文 */
1072
- originLang: OriginLangKeyEnum.ZH,
1073
- /** 翻译目标语言列表,默认包含英文 */
1074
- targetLangList: ['en'],
1074
+ /** 来源语言,由插件固定使用默认值 */
1075
+ originLang: DEFAULT_ORIGIN_LANG,
1076
+ /** 翻译目标语言列表,由插件固定使用默认值 */
1077
+ targetLangList: [...DEFAULT_TARGET_LANG_LIST],
1075
1078
  /** 语言key,用于请求谷歌api和生成配置文件下对应语言的内容文件 */
1076
1079
  langKey: [],
1077
1080
  /** 命名空间,防止全局命名冲突 */
@@ -1589,7 +1592,7 @@ function StringLiteralFn (insertOption) {
1589
1592
 
1590
1593
  // 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
1591
1594
  if (isTranslateCall(parent) || types.isImportDeclaration(parent) || parent.key === node || types.isCallExpression(parent) && extractFnName && (option.excludedCall.includes(extractFnName) || extractFnName?.split('.')?.pop() && option.excludedCall.includes(extractFnName?.split('.')?.pop() || ''))) return;
1592
- const vueVNodeCallPath = getVueCompiledVNodeCallPath(path);
1595
+ const vueVNodeInfo = getVueCompiledVNodeInfo(path);
1593
1596
  let replaceNode;
1594
1597
  if (option.deepScan && checkNeedSplit(value)) {
1595
1598
  replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
@@ -1608,26 +1611,38 @@ function StringLiteralFn (insertOption) {
1608
1611
  });
1609
1612
  }
1610
1613
  path.replaceWith(replaceNode);
1611
- markVueCompiledVNodeDynamic(vueVNodeCallPath);
1614
+ markVueCompiledVNodeDynamic(vueVNodeInfo?.callPath, vueVNodeInfo?.dynamicPropName);
1612
1615
  }
1613
1616
  };
1614
1617
  }
1615
- function getVueCompiledVNodeCallPath(path) {
1618
+ function getVueCompiledVNodeInfo(path) {
1616
1619
  const callPath = path.parentPath;
1617
- if (!callPath || !types.isCallExpression(callPath.node)) return;
1618
- if (!isVueVNodeCreateCall(callPath.node)) return;
1619
- if (path.listKey !== 'arguments' || path.key !== 2) return;
1620
- return callPath;
1620
+ if (callPath && types.isCallExpression(callPath.node) && isVueVNodeCreateCall(callPath.node) && path.listKey === 'arguments' && path.key === 2) {
1621
+ return {
1622
+ callPath
1623
+ };
1624
+ }
1625
+ const propertyPath = path.parentPath;
1626
+ const propsPath = propertyPath?.parentPath;
1627
+ const vnodeCallPath = propsPath?.parentPath;
1628
+ if (!propertyPath || !types.isObjectProperty(propertyPath.node) || propertyPath.node.value !== path.node || propertyPath.node.computed || !propsPath || !types.isObjectExpression(propsPath.node) || propsPath.listKey !== 'arguments' || propsPath.key !== 1 || !vnodeCallPath || !types.isCallExpression(vnodeCallPath.node) || !isVueVNodeCreateCall(vnodeCallPath.node)) return;
1629
+ const key = propertyPath.node.key;
1630
+ const dynamicPropName = types.isIdentifier(key) ? key.name : types.isStringLiteral(key) ? key.value : undefined;
1631
+ if (!dynamicPropName) return;
1632
+ return {
1633
+ callPath: vnodeCallPath,
1634
+ dynamicPropName
1635
+ };
1621
1636
  }
1622
- function markVueCompiledVNodeDynamic(callPath) {
1637
+ function markVueCompiledVNodeDynamic(callPath, dynamicPropName) {
1623
1638
  if (!callPath || !types.isCallExpression(callPath.node)) return;
1624
- const patchFlag = callPath.node.arguments[3];
1625
- if (isCachedPatchFlag(patchFlag)) {
1626
- const textPatchFlag = types.numericLiteral(1);
1627
- textPatchFlag.leadingComments = null;
1628
- textPatchFlag.innerComments = null;
1629
- textPatchFlag.trailingComments = null;
1630
- callPath.node.arguments[3] = textPatchFlag;
1639
+ if (dynamicPropName) {
1640
+ markVueCompiledPropDynamic(callPath.node, dynamicPropName);
1641
+ } else {
1642
+ const patchFlag = callPath.node.arguments[3];
1643
+ if (isCachedPatchFlag(patchFlag)) {
1644
+ callPath.node.arguments[3] = createPatchFlag(1);
1645
+ }
1631
1646
  }
1632
1647
  const logicalPath = callPath.findParent(parentPath => {
1633
1648
  const node = parentPath.node;
@@ -1639,6 +1654,33 @@ function markVueCompiledVNodeDynamic(callPath) {
1639
1654
  logicalPath.replaceWith(callPath.node);
1640
1655
  }
1641
1656
  }
1657
+ function markVueCompiledPropDynamic(call, propName) {
1658
+ while (call.arguments.length < 3) {
1659
+ call.arguments.push(types.nullLiteral());
1660
+ }
1661
+ const patchFlag = call.arguments[3];
1662
+ if (!patchFlag || isCachedPatchFlag(patchFlag)) {
1663
+ call.arguments[3] = createPatchFlag(8);
1664
+ } else if (types.isNumericLiteral(patchFlag) && (patchFlag.value & 8) !== 8) {
1665
+ call.arguments[3] = createPatchFlag(patchFlag.value | 8);
1666
+ }
1667
+ const dynamicProps = call.arguments[4];
1668
+ if (types.isArrayExpression(dynamicProps)) {
1669
+ const hasProp = dynamicProps.elements.some(element => types.isStringLiteral(element) && element.value === propName);
1670
+ if (!hasProp) {
1671
+ dynamicProps.elements.push(types.stringLiteral(propName));
1672
+ }
1673
+ } else {
1674
+ call.arguments[4] = types.arrayExpression([types.stringLiteral(propName)]);
1675
+ }
1676
+ }
1677
+ function createPatchFlag(value) {
1678
+ const patchFlag = types.numericLiteral(value);
1679
+ patchFlag.leadingComments = null;
1680
+ patchFlag.innerComments = null;
1681
+ patchFlag.trailingComments = null;
1682
+ return patchFlag;
1683
+ }
1642
1684
  function isCachedPatchFlag(node) {
1643
1685
  return types.isNumericLiteral(node) && node.value === -1 || types.isUnaryExpression(node) && node.operator === '-' && types.isNumericLiteral(node.argument) && node.argument.value === 1;
1644
1686
  }