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.
@@ -48,7 +48,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
48
48
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
49
49
  PERFORMANCE OF THIS SOFTWARE.
50
50
  ***************************************************************************** */
51
- /* global Reflect, Promise, SuppressedError, Symbol */
51
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
52
52
 
53
53
  var __assign = function () {
54
54
  __assign = Object.assign || function __assign(t) {
@@ -100,12 +100,8 @@ function __generator(thisArg, body) {
100
100
  f,
101
101
  y,
102
102
  t,
103
- g;
104
- return g = {
105
- next: verb(0),
106
- "throw": verb(1),
107
- "return": verb(2)
108
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
103
+ g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
104
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function () {
109
105
  return this;
110
106
  }), g;
111
107
  function verb(n) {
@@ -701,7 +697,12 @@ function hasOriginSymbols$1(value) {
701
697
  const originRegex = REGEX_MAP[option.originLang];
702
698
  return originRegex ? originRegex.test(value) : false;
703
699
  }
700
+ function isTemplateFragmentSegment(value) {
701
+ return /[^\p{Script=Han}A-Za-z0-9_\s]/u.test(value);
702
+ }
704
703
  function getTemplateSemanticSplitSegments(value) {
704
+ const expressionCount = value.match(/\$\{[^}]+}/g)?.length || 0;
705
+ if (expressionCount < 2) return [];
705
706
  const segments = value.split(/\$\{[^}]+}/g);
706
707
  const result = new Set();
707
708
  for (let index = 0; index < segments.length - 1; index++) {
@@ -709,7 +710,7 @@ function getTemplateSemanticSplitSegments(value) {
709
710
  const afterValue = segments[index + 1];
710
711
  if (!hasOriginSymbols$1(beforeValue) || !hasOriginSymbols$1(afterValue)) continue;
711
712
  [beforeValue, afterValue].forEach(segment => {
712
- if (segment.trim().length > 1) {
713
+ if (segment.trim().length > 1 && isTemplateFragmentSegment(segment)) {
713
714
  result.add(segment);
714
715
  }
715
716
  });
@@ -1068,6 +1069,8 @@ let TranslateTypeEnum = /*#__PURE__*/function (TranslateTypeEnum) {
1068
1069
  * @FilePath: /i18n_translation_vite/packages/autoI18nPluginCore/src/option.ts
1069
1070
  */
1070
1071
 
1072
+ const DEFAULT_ORIGIN_LANG = 'zh-cn';
1073
+ const DEFAULT_TARGET_LANG_LIST = ['zh-tw'];
1071
1074
  /**
1072
1075
  * 默认插件配置选项
1073
1076
  */
@@ -1091,10 +1094,10 @@ const DEFAULT_OPTION = {
1091
1094
  distPath: '',
1092
1095
  /** 打包后生成文件的主文件名称,默认是 'index' */
1093
1096
  distKey: 'index',
1094
- /** 来源语言,默认是中文 */
1095
- originLang: OriginLangKeyEnum.ZH,
1096
- /** 翻译目标语言列表,默认包含英文 */
1097
- targetLangList: ['en'],
1097
+ /** 来源语言,由插件固定使用默认值 */
1098
+ originLang: DEFAULT_ORIGIN_LANG,
1099
+ /** 翻译目标语言列表,由插件固定使用默认值 */
1100
+ targetLangList: [...DEFAULT_TARGET_LANG_LIST],
1098
1101
  /** 语言key,用于请求谷歌api和生成配置文件下对应语言的内容文件 */
1099
1102
  langKey: [],
1100
1103
  /** 命名空间,防止全局命名冲突 */
@@ -1612,7 +1615,7 @@ function StringLiteralFn (insertOption) {
1612
1615
 
1613
1616
  // 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
1614
1617
  if (isTranslateCall(parent) || types__namespace.isImportDeclaration(parent) || parent.key === node || types__namespace.isCallExpression(parent) && extractFnName && (option.excludedCall.includes(extractFnName) || extractFnName?.split('.')?.pop() && option.excludedCall.includes(extractFnName?.split('.')?.pop() || ''))) return;
1615
- const vueVNodeCallPath = getVueCompiledVNodeCallPath(path);
1618
+ const vueVNodeInfo = getVueCompiledVNodeInfo(path);
1616
1619
  let replaceNode;
1617
1620
  if (option.deepScan && checkNeedSplit(value)) {
1618
1621
  replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
@@ -1631,26 +1634,38 @@ function StringLiteralFn (insertOption) {
1631
1634
  });
1632
1635
  }
1633
1636
  path.replaceWith(replaceNode);
1634
- markVueCompiledVNodeDynamic(vueVNodeCallPath);
1637
+ markVueCompiledVNodeDynamic(vueVNodeInfo?.callPath, vueVNodeInfo?.dynamicPropName);
1635
1638
  }
1636
1639
  };
1637
1640
  }
1638
- function getVueCompiledVNodeCallPath(path) {
1641
+ function getVueCompiledVNodeInfo(path) {
1639
1642
  const callPath = path.parentPath;
1640
- if (!callPath || !types__namespace.isCallExpression(callPath.node)) return;
1641
- if (!isVueVNodeCreateCall(callPath.node)) return;
1642
- if (path.listKey !== 'arguments' || path.key !== 2) return;
1643
- return callPath;
1643
+ if (callPath && types__namespace.isCallExpression(callPath.node) && isVueVNodeCreateCall(callPath.node) && path.listKey === 'arguments' && path.key === 2) {
1644
+ return {
1645
+ callPath
1646
+ };
1647
+ }
1648
+ const propertyPath = path.parentPath;
1649
+ const propsPath = propertyPath?.parentPath;
1650
+ const vnodeCallPath = propsPath?.parentPath;
1651
+ 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;
1652
+ const key = propertyPath.node.key;
1653
+ const dynamicPropName = types__namespace.isIdentifier(key) ? key.name : types__namespace.isStringLiteral(key) ? key.value : undefined;
1654
+ if (!dynamicPropName) return;
1655
+ return {
1656
+ callPath: vnodeCallPath,
1657
+ dynamicPropName
1658
+ };
1644
1659
  }
1645
- function markVueCompiledVNodeDynamic(callPath) {
1660
+ function markVueCompiledVNodeDynamic(callPath, dynamicPropName) {
1646
1661
  if (!callPath || !types__namespace.isCallExpression(callPath.node)) return;
1647
- const patchFlag = callPath.node.arguments[3];
1648
- if (isCachedPatchFlag(patchFlag)) {
1649
- const textPatchFlag = types__namespace.numericLiteral(1);
1650
- textPatchFlag.leadingComments = null;
1651
- textPatchFlag.innerComments = null;
1652
- textPatchFlag.trailingComments = null;
1653
- callPath.node.arguments[3] = textPatchFlag;
1662
+ if (dynamicPropName) {
1663
+ markVueCompiledPropDynamic(callPath.node, dynamicPropName);
1664
+ } else {
1665
+ const patchFlag = callPath.node.arguments[3];
1666
+ if (isCachedPatchFlag(patchFlag)) {
1667
+ callPath.node.arguments[3] = createPatchFlag(1);
1668
+ }
1654
1669
  }
1655
1670
  const logicalPath = callPath.findParent(parentPath => {
1656
1671
  const node = parentPath.node;
@@ -1662,6 +1677,33 @@ function markVueCompiledVNodeDynamic(callPath) {
1662
1677
  logicalPath.replaceWith(callPath.node);
1663
1678
  }
1664
1679
  }
1680
+ function markVueCompiledPropDynamic(call, propName) {
1681
+ while (call.arguments.length < 3) {
1682
+ call.arguments.push(types__namespace.nullLiteral());
1683
+ }
1684
+ const patchFlag = call.arguments[3];
1685
+ if (!patchFlag || isCachedPatchFlag(patchFlag)) {
1686
+ call.arguments[3] = createPatchFlag(8);
1687
+ } else if (types__namespace.isNumericLiteral(patchFlag) && (patchFlag.value & 8) !== 8) {
1688
+ call.arguments[3] = createPatchFlag(patchFlag.value | 8);
1689
+ }
1690
+ const dynamicProps = call.arguments[4];
1691
+ if (types__namespace.isArrayExpression(dynamicProps)) {
1692
+ const hasProp = dynamicProps.elements.some(element => types__namespace.isStringLiteral(element) && element.value === propName);
1693
+ if (!hasProp) {
1694
+ dynamicProps.elements.push(types__namespace.stringLiteral(propName));
1695
+ }
1696
+ } else {
1697
+ call.arguments[4] = types__namespace.arrayExpression([types__namespace.stringLiteral(propName)]);
1698
+ }
1699
+ }
1700
+ function createPatchFlag(value) {
1701
+ const patchFlag = types__namespace.numericLiteral(value);
1702
+ patchFlag.leadingComments = null;
1703
+ patchFlag.innerComments = null;
1704
+ patchFlag.trailingComments = null;
1705
+ return patchFlag;
1706
+ }
1665
1707
  function isCachedPatchFlag(node) {
1666
1708
  return types__namespace.isNumericLiteral(node) && node.value === -1 || types__namespace.isUnaryExpression(node) && node.operator === '-' && types__namespace.isNumericLiteral(node.argument) && node.argument.value === 1;
1667
1709
  }