webpack-gc-i18n-plugin 1.1.11 → 1.1.13

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, Iterator */
28
+ /* global Reflect, Promise, SuppressedError, Symbol */
29
29
 
30
30
  var __assign = function () {
31
31
  __assign = Object.assign || function __assign(t) {
@@ -77,8 +77,12 @@ function __generator(thisArg, body) {
77
77
  f,
78
78
  y,
79
79
  t,
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 () {
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 () {
82
86
  return this;
83
87
  }), g;
84
88
  function verb(n) {
@@ -547,7 +551,7 @@ function splitTrailingInlineText(value) {
547
551
  */
548
552
  function checkNeedSplit(str) {
549
553
  // 检查字符串中是否包含需要切割的特殊字符
550
- return str.includes('\\') || str.includes('>') || str.includes('<');
554
+ return str.includes('\\') || /<\/?[A-Za-z][^>]*>/.test(str);
551
555
  }
552
556
 
553
557
  /**
@@ -556,49 +560,29 @@ function checkNeedSplit(str) {
556
560
  * @return {types.CallExpression} - babel的深度扫描的表达式
557
561
  */
558
562
  function convertToTemplateLiteral(strArray, option) {
559
- const quasis = [];
563
+ const quasis = [types.templateElement({
564
+ raw: '',
565
+ cooked: ''
566
+ }, false)];
560
567
  const expressions = [];
561
- strArray.forEach((str, index) => {
562
- if (index === 0) {
563
- if (getOriginRegex().test(str)) {
564
- quasis.push(types.templateElement({
565
- raw: '',
566
- cooked: ''
567
- }, false));
568
- expressions.push(createI18nTranslator({
569
- value: str,
570
- isExpression: true,
571
- insertOption: option
572
- }));
573
- } else {
574
- quasis.push(types.templateElement({
575
- raw: str,
576
- cooked: str
577
- }, false));
578
- }
568
+ strArray.forEach(str => {
569
+ if (getOriginRegex().test(str)) {
570
+ expressions.push(createI18nTranslator({
571
+ value: str,
572
+ isExpression: true,
573
+ insertOption: option
574
+ }));
575
+ quasis.push(types.templateElement({
576
+ raw: '',
577
+ cooked: ''
578
+ }, false));
579
579
  } else {
580
- if (getOriginRegex().test(str)) {
581
- expressions.push(createI18nTranslator({
582
- value: str,
583
- isExpression: true,
584
- insertOption: option
585
- }));
586
- } else {
587
- quasis.push(types.templateElement({
588
- raw: str,
589
- cooked: str
590
- }, false));
591
- }
580
+ const quasi = quasis[quasis.length - 1];
581
+ quasi.value.raw += str;
582
+ quasi.value.cooked = `${quasi.value.cooked || ''}${str}`;
592
583
  }
593
584
  });
594
- if (quasis.length === expressions.length) {
595
- quasis.push(types.templateElement({
596
- raw: '',
597
- cooked: ''
598
- }, true));
599
- } else if (quasis.length > expressions.length) {
600
- quasis[quasis.length - 1].tail = true;
601
- }
585
+ quasis[quasis.length - 1].tail = true;
602
586
  const templateLiteral = types.templateLiteral(quasis, expressions);
603
587
  const deepScanCall = types.callExpression(types.identifier('$deepScan'), [templateLiteral]);
604
588
  // 打印转换结果
@@ -1605,6 +1589,7 @@ function StringLiteralFn (insertOption) {
1605
1589
 
1606
1590
  // 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
1607
1591
  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);
1608
1593
  let replaceNode;
1609
1594
  if (option.deepScan && checkNeedSplit(value)) {
1610
1595
  replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
@@ -1623,9 +1608,47 @@ function StringLiteralFn (insertOption) {
1623
1608
  });
1624
1609
  }
1625
1610
  path.replaceWith(replaceNode);
1611
+ markVueCompiledVNodeDynamic(vueVNodeCallPath);
1626
1612
  }
1627
1613
  };
1628
1614
  }
1615
+ function getVueCompiledVNodeCallPath(path) {
1616
+ 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;
1621
+ }
1622
+ function markVueCompiledVNodeDynamic(callPath) {
1623
+ 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;
1631
+ }
1632
+ const logicalPath = callPath.findParent(parentPath => {
1633
+ const node = parentPath.node;
1634
+ if (!types.isLogicalExpression(node) || node.operator !== '||') return false;
1635
+ const right = node.right;
1636
+ return types.isAssignmentExpression(right) && right.operator === '=' && right.right === callPath.node && isVueRenderCacheMember(node.left) && isVueRenderCacheMember(right.left);
1637
+ });
1638
+ if (logicalPath) {
1639
+ logicalPath.replaceWith(callPath.node);
1640
+ }
1641
+ }
1642
+ function isCachedPatchFlag(node) {
1643
+ return types.isNumericLiteral(node) && node.value === -1 || types.isUnaryExpression(node) && node.operator === '-' && types.isNumericLiteral(node.argument) && node.argument.value === 1;
1644
+ }
1645
+ function isVueVNodeCreateCall(node) {
1646
+ const callee = node.callee;
1647
+ return types.isIdentifier(callee) && ['_createElementVNode', '_createVNode', 'createElementVNode', 'createVNode'].includes(callee.name);
1648
+ }
1649
+ function isVueRenderCacheMember(node) {
1650
+ return types.isMemberExpression(node) && types.isIdentifier(node.object) && node.object.name === '_cache';
1651
+ }
1629
1652
  function isTranslateCall(node) {
1630
1653
  if (!types.isCallExpression(node)) return false;
1631
1654
  const callee = node.callee;