webpack-gc-i18n-plugin 1.1.10 → 1.1.12

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.
@@ -843,6 +843,38 @@ function extractFunctionName(node) {
843
843
  return callName;
844
844
  }
845
845
 
846
+ /**
847
+ * @description 判断调用表达式是否是 console.*(...)
848
+ * 只匹配真实点成员调用,例如 console.log(...) / console.debug(...)。
849
+ */
850
+ function isConsoleMemberCall(node) {
851
+ if (!types.isCallExpression(node) || !types.isMemberExpression(node.callee)) {
852
+ return false;
853
+ }
854
+ const {
855
+ object,
856
+ property,
857
+ computed
858
+ } = node.callee;
859
+ return !computed && types.isIdentifier(object) && object.name === 'console' && types.isIdentifier(property);
860
+ }
861
+
862
+ /**
863
+ * @description 判断当前节点是否位于 console.*(...) 的参数子树中。
864
+ */
865
+ function isInsideConsoleCallArgument(path) {
866
+ let currentPath = path;
867
+ while (currentPath?.parentPath) {
868
+ const parentPath = currentPath.parentPath;
869
+ const parentNode = parentPath.node;
870
+ if (isConsoleMemberCall(parentNode)) {
871
+ return parentNode.arguments.some(arg => arg === currentPath.node);
872
+ }
873
+ currentPath = parentPath;
874
+ }
875
+ return false;
876
+ }
877
+
846
878
  /**
847
879
  * @description: 提取文件的中文部分
848
880
  * @param {string} fileContent
@@ -1022,6 +1054,8 @@ var base = /*#__PURE__*/Object.freeze({
1022
1054
  generateId: generateId,
1023
1055
  getOriginRegex: getOriginRegex,
1024
1056
  hasOriginSymbols: hasOriginSymbols,
1057
+ isConsoleMemberCall: isConsoleMemberCall,
1058
+ isInsideConsoleCallArgument: isInsideConsoleCallArgument,
1025
1059
  normalizeTranslateValue: normalizeTranslateValue,
1026
1060
  removeComments: removeComments,
1027
1061
  truncate: truncate,
@@ -1173,6 +1207,9 @@ function TemplateLiteral (insertOption) {
1173
1207
  if (types.isTaggedTemplateExpression(parent)) {
1174
1208
  return;
1175
1209
  }
1210
+ if (isInsideConsoleCallArgument(path)) {
1211
+ return;
1212
+ }
1176
1213
 
1177
1214
  // 获取真实调用函数
1178
1215
  const extractFnName = extractFunctionName(parent);
@@ -1584,11 +1621,14 @@ function StringLiteralFn (insertOption) {
1584
1621
  }
1585
1622
  }
1586
1623
  if (hasOriginSymbols(value) && option.excludedPattern.length && !checkAgainstRegexArray(value, [...option.excludedPattern])) {
1624
+ if (isInsideConsoleCallArgument(path)) return;
1625
+
1587
1626
  // 获取真实调用函数
1588
1627
  const extractFnName = extractFunctionName(parent);
1589
1628
 
1590
1629
  // 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
1591
1630
  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;
1631
+ const vueVNodeCallPath = getVueCompiledVNodeCallPath(path);
1592
1632
  let replaceNode;
1593
1633
  if (option.deepScan && checkNeedSplit(value)) {
1594
1634
  replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
@@ -1607,9 +1647,47 @@ function StringLiteralFn (insertOption) {
1607
1647
  });
1608
1648
  }
1609
1649
  path.replaceWith(replaceNode);
1650
+ markVueCompiledVNodeDynamic(vueVNodeCallPath);
1610
1651
  }
1611
1652
  };
1612
1653
  }
1654
+ function getVueCompiledVNodeCallPath(path) {
1655
+ const callPath = path.parentPath;
1656
+ if (!callPath || !types__namespace.isCallExpression(callPath.node)) return;
1657
+ if (!isVueVNodeCreateCall(callPath.node)) return;
1658
+ if (path.listKey !== 'arguments' || path.key !== 2) return;
1659
+ return callPath;
1660
+ }
1661
+ function markVueCompiledVNodeDynamic(callPath) {
1662
+ if (!callPath || !types__namespace.isCallExpression(callPath.node)) return;
1663
+ const patchFlag = callPath.node.arguments[3];
1664
+ if (isCachedPatchFlag(patchFlag)) {
1665
+ const textPatchFlag = types__namespace.numericLiteral(1);
1666
+ textPatchFlag.leadingComments = null;
1667
+ textPatchFlag.innerComments = null;
1668
+ textPatchFlag.trailingComments = null;
1669
+ callPath.node.arguments[3] = textPatchFlag;
1670
+ }
1671
+ const logicalPath = callPath.findParent(parentPath => {
1672
+ const node = parentPath.node;
1673
+ if (!types__namespace.isLogicalExpression(node) || node.operator !== '||') return false;
1674
+ const right = node.right;
1675
+ return types__namespace.isAssignmentExpression(right) && right.operator === '=' && right.right === callPath.node && isVueRenderCacheMember(node.left) && isVueRenderCacheMember(right.left);
1676
+ });
1677
+ if (logicalPath) {
1678
+ logicalPath.replaceWith(callPath.node);
1679
+ }
1680
+ }
1681
+ function isCachedPatchFlag(node) {
1682
+ return types__namespace.isNumericLiteral(node) && node.value === -1 || types__namespace.isUnaryExpression(node) && node.operator === '-' && types__namespace.isNumericLiteral(node.argument) && node.argument.value === 1;
1683
+ }
1684
+ function isVueVNodeCreateCall(node) {
1685
+ const callee = node.callee;
1686
+ return types__namespace.isIdentifier(callee) && ['_createElementVNode', '_createVNode', 'createElementVNode', 'createVNode'].includes(callee.name);
1687
+ }
1688
+ function isVueRenderCacheMember(node) {
1689
+ return types__namespace.isMemberExpression(node) && types__namespace.isIdentifier(node.object) && node.object.name === '_cache';
1690
+ }
1613
1691
  function isTranslateCall(node) {
1614
1692
  if (!types__namespace.isCallExpression(node)) return false;
1615
1693
  const callee = node.callee;