webpack-gc-i18n-plugin 1.1.3 → 1.1.4

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.
@@ -811,8 +811,6 @@ function setLangObj(key, value) {
811
811
  * @returns 处理后的字符串数组
812
812
  */
813
813
  function splitByRegex(str, separatorRegex) {
814
- if (str.includes('\n')) console.log(str, separatorRegex);
815
-
816
814
  // 定义标点符号的正则表达式
817
815
  const punctuationRegex = /[,。?!《》,..:!?""'';'"、0-9\n\r\t\v\f]/;
818
816
  // 创建一个新的正则表达式,用于分割字符串
@@ -976,8 +974,17 @@ function TemplateLiteral (insertOption) {
976
974
  };
977
975
  }
978
976
  function handleTemplateLiteralWithExpressions(node, path) {
979
- const placeholders = node.expressions.map(expression => getExpressionPlaceholder(expression));
980
- if (placeholders.some(placeholder => !placeholder)) {
977
+ let hasOptimizedExpression = false;
978
+ const expressionLabels = node.expressions.map((expression, index) => {
979
+ const optimizedExpression = optimizeConditionalExpression(expression);
980
+ if (optimizedExpression) {
981
+ hasOptimizedExpression = true;
982
+ node.expressions[index] = optimizedExpression.expression;
983
+ return optimizedExpression.label;
984
+ }
985
+ return getExpressionPlaceholder(expression);
986
+ });
987
+ if (expressionLabels.some(placeholder => placeholder === null)) {
981
988
  return false;
982
989
  }
983
990
  const fullValue = node.quasis.reduce((result, quasi, index) => {
@@ -989,9 +996,13 @@ function handleTemplateLiteralWithExpressions(node, path) {
989
996
  console.log('转换异常');
990
997
  }
991
998
  }
992
- return result + value + (index < node.expressions.length ? `\${${placeholders[index]}}` : '');
999
+ return result + value + (index < node.expressions.length ? formatExpressionPlaceholder(expressionLabels[index]) : '');
993
1000
  }, '');
994
1001
  if (!fullValue || !hasOriginSymbols(fullValue) || !option.excludedPattern.length || checkAgainstRegexArray(fullValue, [...option.excludedPattern])) {
1002
+ if (hasOptimizedExpression) {
1003
+ path.skip();
1004
+ return true;
1005
+ }
995
1006
  return false;
996
1007
  }
997
1008
  const {
@@ -999,22 +1010,12 @@ function handleTemplateLiteralWithExpressions(node, path) {
999
1010
  valStr
1000
1011
  } = normalizeTranslateValue(fullValue);
1001
1012
  const id = generateId(valStr);
1002
- const translateNode = createTranslateNode(path, id, valStr, placeholders, node.expressions);
1013
+ const translateNode = createTranslateNode(path, id, valStr, expressionLabels, node.expressions);
1003
1014
  path.replaceWith(translateNode);
1004
- path.skip();
1005
1015
  setLangObj(id, trimmedValue);
1006
1016
  return true;
1007
1017
  }
1008
1018
  function getExpressionPlaceholder(expression) {
1009
- if (types.isIdentifier(expression)) {
1010
- return expression.name;
1011
- }
1012
- if (types.isMemberExpression(expression)) {
1013
- const objectName = getExpressionPlaceholder(expression.object);
1014
- const propertyName = expression.computed ? getExpressionPlaceholder(expression.property) : types.isIdentifier(expression.property) ? expression.property.name : '';
1015
- const value = [objectName, propertyName].filter(Boolean).join('.');
1016
- return cleanVueRuntimePrefix(value);
1017
- }
1018
1019
  if (types.isStringLiteral(expression) || types.isNumericLiteral(expression)) {
1019
1020
  return String(expression.value);
1020
1021
  }
@@ -1025,21 +1026,142 @@ function getExpressionPlaceholder(expression) {
1025
1026
  return getExpressionPlaceholder(expression.expression);
1026
1027
  }
1027
1028
  const generateCode = generate__namespace.default?.default || generate__namespace.default || generate__namespace;
1028
- return cleanVueRuntimePrefix(generateCode(expression).code);
1029
+ return generateCode(expression, {
1030
+ comments: false
1031
+ }).code;
1032
+ }
1033
+ function optimizeConditionalExpression(expression) {
1034
+ if (!types.isConditionalExpression(expression)) return null;
1035
+ const consequent = translateConditionalStringBranch(expression.consequent);
1036
+ const alternate = translateConditionalStringBranch(expression.alternate);
1037
+ if (!consequent.changed && !alternate.changed) return null;
1038
+ return {
1039
+ label: getConditionalExpressionLabel(expression),
1040
+ expression: types.conditionalExpression(types.cloneNode(expression.test), consequent.expression, alternate.expression)
1041
+ };
1042
+ }
1043
+ function translateConditionalStringBranch(expression) {
1044
+ if (types.isStringLiteral(expression) && hasOriginSymbols(expression.value)) {
1045
+ return {
1046
+ changed: true,
1047
+ expression: createTrackedTranslateCall(expression.value)
1048
+ };
1049
+ }
1050
+ if (types.isTemplateLiteral(expression)) {
1051
+ const translateCall = createTemplateLiteralTranslateCall(expression);
1052
+ if (translateCall) {
1053
+ return {
1054
+ changed: true,
1055
+ expression: translateCall
1056
+ };
1057
+ }
1058
+ }
1059
+ if (types.isConditionalExpression(expression)) {
1060
+ const optimizedExpression = optimizeConditionalExpression(expression);
1061
+ if (optimizedExpression) {
1062
+ return {
1063
+ changed: true,
1064
+ expression: optimizedExpression.expression
1065
+ };
1066
+ }
1067
+ }
1068
+ return {
1069
+ changed: false,
1070
+ expression: types.cloneNode(expression)
1071
+ };
1072
+ }
1073
+ function createTrackedTranslateCall(value) {
1074
+ const translateCall = createI18nTranslator({
1075
+ value,
1076
+ isExpression: true
1077
+ });
1078
+ const {
1079
+ trimmedValue,
1080
+ valStr
1081
+ } = normalizeTranslateValue(value);
1082
+ const id = generateId(valStr);
1083
+ if (id && trimmedValue) {
1084
+ setLangObj(id, trimmedValue);
1085
+ }
1086
+ return translateCall;
1087
+ }
1088
+ function createTemplateLiteralTranslateCall(node) {
1089
+ const expressions = node.expressions.map(expression => {
1090
+ const optimizedExpression = optimizeConditionalExpression(expression);
1091
+ return optimizedExpression ? optimizedExpression.expression : expression;
1092
+ });
1093
+ const expressionLabels = expressions.map(expression => getExpressionPlaceholder(expression));
1094
+ if (expressionLabels.some(placeholder => placeholder === null)) {
1095
+ return null;
1096
+ }
1097
+ const fullValue = node.quasis.reduce((result, quasi, index) => {
1098
+ let value = quasi.value.raw || quasi.value.cooked || '';
1099
+ if (asianLangs.some(lang => option.originLang.includes(lang) || option.originLang === lang)) {
1100
+ try {
1101
+ value = unicodeToString(value);
1102
+ } catch (error) {
1103
+ console.log('转换异常');
1104
+ }
1105
+ }
1106
+ return result + value + (index < expressions.length ? formatExpressionPlaceholder(expressionLabels[index]) : '');
1107
+ }, '');
1108
+ if (!fullValue || !hasOriginSymbols(fullValue) || !option.excludedPattern.length || checkAgainstRegexArray(fullValue, [...option.excludedPattern])) {
1109
+ return null;
1110
+ }
1111
+ const {
1112
+ trimmedValue,
1113
+ valStr
1114
+ } = normalizeTranslateValue(fullValue);
1115
+ const id = generateId(valStr);
1116
+ if (id && trimmedValue) {
1117
+ setLangObj(id, trimmedValue);
1118
+ }
1119
+ return createTranslateCall(id, valStr, expressionLabels, expressions);
1120
+ }
1121
+ function getConditionalExpressionLabel(expression) {
1122
+ const test = expression.test;
1123
+ if (types.isBinaryExpression(test) || types.isLogicalExpression(test)) {
1124
+ return getExpressionPlaceholder(test.left);
1125
+ }
1126
+ return getExpressionPlaceholder(test);
1127
+ }
1128
+ function formatExpressionPlaceholder(placeholder) {
1129
+ if (placeholder === null) return '';
1130
+ return placeholder.trim() ? `\${${placeholder}}` : placeholder;
1029
1131
  }
1030
1132
  function createTranslateNode(path, id, valStr) {
1031
1133
  let placeholders = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1032
1134
  let expressions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
1033
- const args = [types.stringLiteral(id), types.stringLiteral(valStr)];
1034
- if (placeholders.length) {
1035
- args.push(types.objectExpression(placeholders.map((placeholder, index) => types.objectProperty(types.stringLiteral(placeholder), types.cloneNode(expressions[index])))));
1036
- }
1037
- const translateCall = types.callExpression(types.identifier(option.translateKey), args);
1135
+ const translateCall = createTranslateCall(id, valStr, placeholders, expressions);
1038
1136
  if (!isVueTemplateRender(path)) {
1039
1137
  return translateCall;
1040
1138
  }
1041
1139
  return types.sequenceExpression([createVueTemplateLanguageDependency(), translateCall]);
1042
1140
  }
1141
+ function createTranslateCall(id, valStr) {
1142
+ let placeholders = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1143
+ let expressions = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1144
+ const args = [types.stringLiteral(id), types.stringLiteral(valStr)];
1145
+ if (placeholders.length) {
1146
+ const placeholderEntries = placeholders.map((placeholder, index) => ({
1147
+ placeholder,
1148
+ expression: expressions[index]
1149
+ })).filter(_ref => {
1150
+ let {
1151
+ placeholder
1152
+ } = _ref;
1153
+ return placeholder && placeholder.trim();
1154
+ });
1155
+ args.push(types.objectExpression(placeholderEntries.map(_ref2 => {
1156
+ let {
1157
+ placeholder,
1158
+ expression
1159
+ } = _ref2;
1160
+ return types.objectProperty(types.stringLiteral(placeholder), types.cloneNode(expression));
1161
+ })));
1162
+ }
1163
+ return types.callExpression(types.identifier(option.translateKey), args);
1164
+ }
1043
1165
  function isVueTemplateRender(path) {
1044
1166
  return Boolean(path.scope?.getBinding('_vm'));
1045
1167
  }
@@ -1053,9 +1175,6 @@ function createVueTemplateLanguageDependency() {
1053
1175
  const i18nLocale = types.logicalExpression('&&', types.cloneNode(i18n), types.memberExpression(types.cloneNode(i18n), types.identifier('locale')));
1054
1176
  return types.logicalExpression('||', storeLanguage, i18nLocale);
1055
1177
  }
1056
- function cleanVueRuntimePrefix(value) {
1057
- return value.replace(/^(?:\$setup|_ctx|_vm|this)\./, '').replace(/([^\w$.])(?:\$setup|_ctx|_vm|this)\./g, '$1');
1058
- }
1059
1178
 
1060
1179
  // 处理模板元素
1061
1180
  function handleTemplateElement(node, insertOption) {