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.
package/dist/index.mjs CHANGED
@@ -26,7 +26,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26
26
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27
27
  PERFORMANCE OF THIS SOFTWARE.
28
28
  ***************************************************************************** */
29
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
29
+ /* global Reflect, Promise, SuppressedError, Symbol */
30
30
 
31
31
  var __assign = function () {
32
32
  __assign = Object.assign || function __assign(t) {
@@ -78,8 +78,12 @@ function __generator(thisArg, body) {
78
78
  f,
79
79
  y,
80
80
  t,
81
- g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
82
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function () {
81
+ g;
82
+ return g = {
83
+ next: verb(0),
84
+ "throw": verb(1),
85
+ "return": verb(2)
86
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
83
87
  return this;
84
88
  }), g;
85
89
  function verb(n) {
@@ -1045,7 +1049,7 @@ function splitTrailingInlineText(value) {
1045
1049
  */
1046
1050
  function checkNeedSplit(str) {
1047
1051
  // 检查字符串中是否包含需要切割的特殊字符
1048
- return str.includes('\\') || str.includes('>') || str.includes('<');
1052
+ return str.includes('\\') || /<\/?[A-Za-z][^>]*>/.test(str);
1049
1053
  }
1050
1054
 
1051
1055
  /**
@@ -1054,49 +1058,29 @@ function checkNeedSplit(str) {
1054
1058
  * @return {types.CallExpression} - babel的深度扫描的表达式
1055
1059
  */
1056
1060
  function convertToTemplateLiteral(strArray, option) {
1057
- const quasis = [];
1061
+ const quasis = [types.templateElement({
1062
+ raw: '',
1063
+ cooked: ''
1064
+ }, false)];
1058
1065
  const expressions = [];
1059
- strArray.forEach((str, index) => {
1060
- if (index === 0) {
1061
- if (getOriginRegex().test(str)) {
1062
- quasis.push(types.templateElement({
1063
- raw: '',
1064
- cooked: ''
1065
- }, false));
1066
- expressions.push(createI18nTranslator({
1067
- value: str,
1068
- isExpression: true,
1069
- insertOption: option
1070
- }));
1071
- } else {
1072
- quasis.push(types.templateElement({
1073
- raw: str,
1074
- cooked: str
1075
- }, false));
1076
- }
1066
+ strArray.forEach(str => {
1067
+ if (getOriginRegex().test(str)) {
1068
+ expressions.push(createI18nTranslator({
1069
+ value: str,
1070
+ isExpression: true,
1071
+ insertOption: option
1072
+ }));
1073
+ quasis.push(types.templateElement({
1074
+ raw: '',
1075
+ cooked: ''
1076
+ }, false));
1077
1077
  } else {
1078
- if (getOriginRegex().test(str)) {
1079
- expressions.push(createI18nTranslator({
1080
- value: str,
1081
- isExpression: true,
1082
- insertOption: option
1083
- }));
1084
- } else {
1085
- quasis.push(types.templateElement({
1086
- raw: str,
1087
- cooked: str
1088
- }, false));
1089
- }
1078
+ const quasi = quasis[quasis.length - 1];
1079
+ quasi.value.raw += str;
1080
+ quasi.value.cooked = `${quasi.value.cooked || ''}${str}`;
1090
1081
  }
1091
1082
  });
1092
- if (quasis.length === expressions.length) {
1093
- quasis.push(types.templateElement({
1094
- raw: '',
1095
- cooked: ''
1096
- }, true));
1097
- } else if (quasis.length > expressions.length) {
1098
- quasis[quasis.length - 1].tail = true;
1099
- }
1083
+ quasis[quasis.length - 1].tail = true;
1100
1084
  const templateLiteral = types.templateLiteral(quasis, expressions);
1101
1085
  const deepScanCall = types.callExpression(types.identifier('$deepScan'), [templateLiteral]);
1102
1086
  // 打印转换结果
@@ -2962,6 +2946,7 @@ function StringLiteralFn (insertOption) {
2962
2946
 
2963
2947
  // 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
2964
2948
  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;
2949
+ const vueVNodeCallPath = getVueCompiledVNodeCallPath(path);
2965
2950
  let replaceNode;
2966
2951
  if (option.deepScan && checkNeedSplit(value)) {
2967
2952
  replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
@@ -2980,9 +2965,47 @@ function StringLiteralFn (insertOption) {
2980
2965
  });
2981
2966
  }
2982
2967
  path.replaceWith(replaceNode);
2968
+ markVueCompiledVNodeDynamic(vueVNodeCallPath);
2983
2969
  }
2984
2970
  };
2985
2971
  }
2972
+ function getVueCompiledVNodeCallPath(path) {
2973
+ const callPath = path.parentPath;
2974
+ if (!callPath || !types.isCallExpression(callPath.node)) return;
2975
+ if (!isVueVNodeCreateCall(callPath.node)) return;
2976
+ if (path.listKey !== 'arguments' || path.key !== 2) return;
2977
+ return callPath;
2978
+ }
2979
+ function markVueCompiledVNodeDynamic(callPath) {
2980
+ if (!callPath || !types.isCallExpression(callPath.node)) return;
2981
+ const patchFlag = callPath.node.arguments[3];
2982
+ if (isCachedPatchFlag(patchFlag)) {
2983
+ const textPatchFlag = types.numericLiteral(1);
2984
+ textPatchFlag.leadingComments = null;
2985
+ textPatchFlag.innerComments = null;
2986
+ textPatchFlag.trailingComments = null;
2987
+ callPath.node.arguments[3] = textPatchFlag;
2988
+ }
2989
+ const logicalPath = callPath.findParent(parentPath => {
2990
+ const node = parentPath.node;
2991
+ if (!types.isLogicalExpression(node) || node.operator !== '||') return false;
2992
+ const right = node.right;
2993
+ return types.isAssignmentExpression(right) && right.operator === '=' && right.right === callPath.node && isVueRenderCacheMember(node.left) && isVueRenderCacheMember(right.left);
2994
+ });
2995
+ if (logicalPath) {
2996
+ logicalPath.replaceWith(callPath.node);
2997
+ }
2998
+ }
2999
+ function isCachedPatchFlag(node) {
3000
+ return types.isNumericLiteral(node) && node.value === -1 || types.isUnaryExpression(node) && node.operator === '-' && types.isNumericLiteral(node.argument) && node.argument.value === 1;
3001
+ }
3002
+ function isVueVNodeCreateCall(node) {
3003
+ const callee = node.callee;
3004
+ return types.isIdentifier(callee) && ['_createElementVNode', '_createVNode', 'createElementVNode', 'createVNode'].includes(callee.name);
3005
+ }
3006
+ function isVueRenderCacheMember(node) {
3007
+ return types.isMemberExpression(node) && types.isIdentifier(node.object) && node.object.name === '_cache';
3008
+ }
2986
3009
  function isTranslateCall(node) {
2987
3010
  if (!types.isCallExpression(node)) return false;
2988
3011
  const callee = node.callee;