webpack-gc-i18n-plugin 1.1.7 → 1.1.9
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/customLoader/index.cjs +122 -11
- package/dist/customLoader/index.cjs.map +1 -1
- package/dist/customLoader/index.mjs +122 -11
- package/dist/customLoader/index.mjs.map +1 -1
- package/dist/index.cjs +562 -68
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +121 -2
- package/dist/index.mjs +549 -69
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -50,6 +50,16 @@ PERFORMANCE OF THIS SOFTWARE.
|
|
|
50
50
|
***************************************************************************** */
|
|
51
51
|
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
52
52
|
|
|
53
|
+
var __assign = function () {
|
|
54
|
+
__assign = Object.assign || function __assign(t) {
|
|
55
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
56
|
+
s = arguments[i];
|
|
57
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
58
|
+
}
|
|
59
|
+
return t;
|
|
60
|
+
};
|
|
61
|
+
return __assign.apply(this, arguments);
|
|
62
|
+
};
|
|
53
63
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
54
64
|
function adopt(value) {
|
|
55
65
|
return value instanceof P ? value : new P(function (resolve) {
|
|
@@ -415,7 +425,7 @@ const REGEX_MAP = {
|
|
|
415
425
|
*/
|
|
416
426
|
function splitByRegex(str, separatorRegex) {
|
|
417
427
|
// 定义标点符号的正则表达式
|
|
418
|
-
const punctuationRegex = /[
|
|
428
|
+
const punctuationRegex = /[,。?!《》()(),..:!?""'';'"、0-9\n\r\t\v\f]/;
|
|
419
429
|
// 创建一个新的正则表达式,用于分割字符串
|
|
420
430
|
const splitRegex = new RegExp(`(${separatorRegex.source}|${punctuationRegex.source})`, separatorRegex.flags);
|
|
421
431
|
|
|
@@ -425,7 +435,7 @@ function splitByRegex(str, separatorRegex) {
|
|
|
425
435
|
let currentMatch = '';
|
|
426
436
|
|
|
427
437
|
// 定义连接标点符号的正则表达式
|
|
428
|
-
const connectPunctuationRegex = /[
|
|
438
|
+
const connectPunctuationRegex = /[,。?!《》()(),..:!?;'"、0-9]/;
|
|
429
439
|
// 创建一个新的正则表达式,用于检测是否需要连接
|
|
430
440
|
const connectRegex = new RegExp(`(${separatorRegex.source}|${connectPunctuationRegex.source})`, separatorRegex.flags);
|
|
431
441
|
|
|
@@ -472,7 +482,85 @@ function splitByRegex(str, separatorRegex) {
|
|
|
472
482
|
if (tempStr) {
|
|
473
483
|
finalResult.push(tempStr);
|
|
474
484
|
}
|
|
475
|
-
return finalResult;
|
|
485
|
+
return mergeInlineTextSeparatedOriginChunks(finalResult, separatorRegex);
|
|
486
|
+
}
|
|
487
|
+
function mergeInlineTextSeparatedOriginChunks(strArray, separatorRegex) {
|
|
488
|
+
const result = [];
|
|
489
|
+
for (let i = 0; i < strArray.length; i++) {
|
|
490
|
+
let current = strArray[i];
|
|
491
|
+
if (!separatorRegex.test(current) && i + 1 < strArray.length && separatorRegex.test(strArray[i + 1])) {
|
|
492
|
+
const {
|
|
493
|
+
beforeInlineText,
|
|
494
|
+
inlineText
|
|
495
|
+
} = splitTrailingInlineText(current);
|
|
496
|
+
if (inlineText && /[^\s\u00a0]/.test(inlineText)) {
|
|
497
|
+
if (beforeInlineText) {
|
|
498
|
+
result.push(beforeInlineText);
|
|
499
|
+
}
|
|
500
|
+
current = `${normalizeInlineTextPrefix(inlineText)}${strArray[i + 1]}`;
|
|
501
|
+
i += 1;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
if (!separatorRegex.test(current) && isInlineTextSeparator(current) && /[^\s\u00a0]/.test(current) && i + 1 < strArray.length && separatorRegex.test(strArray[i + 1])) {
|
|
505
|
+
current = `${normalizeInlineTextPrefix(current)}${strArray[i + 1]}`;
|
|
506
|
+
i += 1;
|
|
507
|
+
}
|
|
508
|
+
if (separatorRegex.test(current)) {
|
|
509
|
+
while (i + 2 < strArray.length && isInlineTextSeparator(strArray[i + 1]) && separatorRegex.test(strArray[i + 2])) {
|
|
510
|
+
current += `${normalizeInlineTextSeparator(strArray[i + 1])}${strArray[i + 2]}`;
|
|
511
|
+
i += 2;
|
|
512
|
+
}
|
|
513
|
+
if (i + 1 < strArray.length) {
|
|
514
|
+
const {
|
|
515
|
+
inlineText,
|
|
516
|
+
rest
|
|
517
|
+
} = splitLeadingInlineText(strArray[i + 1]);
|
|
518
|
+
if (inlineText) {
|
|
519
|
+
current += normalizeInlineTextSeparator(inlineText);
|
|
520
|
+
strArray[i + 1] = rest;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
if (current) {
|
|
525
|
+
result.push(current);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return result;
|
|
529
|
+
}
|
|
530
|
+
function isInlineTextSeparator(value) {
|
|
531
|
+
return value !== '' && !/[<>]/.test(value);
|
|
532
|
+
}
|
|
533
|
+
function normalizeInlineTextSeparator(value) {
|
|
534
|
+
return /^[\s\u00a0]+$/.test(value) ? ' ' : value.replace(/[\s\u00a0]+/g, ' ');
|
|
535
|
+
}
|
|
536
|
+
function normalizeInlineTextPrefix(value) {
|
|
537
|
+
return value.replace(/[\s\u00a0]+/g, ' ').trimStart();
|
|
538
|
+
}
|
|
539
|
+
function splitLeadingInlineText(value) {
|
|
540
|
+
const tagIndex = value.search(/[<>]/);
|
|
541
|
+
if (tagIndex === -1) {
|
|
542
|
+
return {
|
|
543
|
+
inlineText: value,
|
|
544
|
+
rest: ''
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
return {
|
|
548
|
+
inlineText: value.slice(0, tagIndex),
|
|
549
|
+
rest: value.slice(tagIndex)
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function splitTrailingInlineText(value) {
|
|
553
|
+
const tagEndIndex = value.lastIndexOf('>');
|
|
554
|
+
if (tagEndIndex === -1) {
|
|
555
|
+
return {
|
|
556
|
+
beforeInlineText: '',
|
|
557
|
+
inlineText: value
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
return {
|
|
561
|
+
beforeInlineText: value.slice(0, tagEndIndex + 1),
|
|
562
|
+
inlineText: value.slice(tagEndIndex + 1)
|
|
563
|
+
};
|
|
476
564
|
}
|
|
477
565
|
|
|
478
566
|
/**
|
|
@@ -482,7 +570,7 @@ function splitByRegex(str, separatorRegex) {
|
|
|
482
570
|
*/
|
|
483
571
|
function checkNeedSplit(str) {
|
|
484
572
|
// 检查字符串中是否包含需要切割的特殊字符
|
|
485
|
-
return str.includes('
|
|
573
|
+
return str.includes('\\') || str.includes('>') || str.includes('<');
|
|
486
574
|
}
|
|
487
575
|
|
|
488
576
|
/**
|
|
@@ -807,6 +895,13 @@ function createI18nTranslator(createOption) {
|
|
|
807
895
|
uncodeValue: valStr,
|
|
808
896
|
namespace: nameSpace
|
|
809
897
|
};
|
|
898
|
+
insertOption?.collector?.record?.({
|
|
899
|
+
key: option.useValueAsKey ? trimmedValue : generatedKey,
|
|
900
|
+
value: trimmedValue,
|
|
901
|
+
namespace: nameSpace,
|
|
902
|
+
resourcePath: insertOption?.resourcePath,
|
|
903
|
+
root: insertOption?.root
|
|
904
|
+
});
|
|
810
905
|
if (option.translateExtends) {
|
|
811
906
|
const {
|
|
812
907
|
handleCodeCall,
|
|
@@ -1164,9 +1259,13 @@ function getExpressionPlaceholder(expression) {
|
|
|
1164
1259
|
return getExpressionPlaceholder(expression.expression);
|
|
1165
1260
|
}
|
|
1166
1261
|
const generateCode = generate__namespace.default?.default || generate__namespace.default || generate__namespace;
|
|
1167
|
-
return generateCode(expression, {
|
|
1262
|
+
return normalizeExpressionPlaceholder(generateCode(expression, {
|
|
1168
1263
|
comments: false
|
|
1169
|
-
}).code;
|
|
1264
|
+
}).code);
|
|
1265
|
+
}
|
|
1266
|
+
function normalizeExpressionPlaceholder(placeholder) {
|
|
1267
|
+
if (placeholder === null) return null;
|
|
1268
|
+
return placeholder.replace(/(^|[^\w$])(?:_vm|\$data)\s*\./g, '$1').replace(/(^|[^\w$])(?:_vm|\$data)\s*\[/g, '$1[');
|
|
1170
1269
|
}
|
|
1171
1270
|
function optimizeConditionalExpression(expression) {
|
|
1172
1271
|
if (!types.isConditionalExpression(expression)) return null;
|
|
@@ -1412,11 +1511,11 @@ function CallExpressionFn (insertOption) {
|
|
|
1412
1511
|
isExpression: true
|
|
1413
1512
|
});
|
|
1414
1513
|
path.replaceWith(replaceNode);
|
|
1415
|
-
translateSetLang(replaceNode);
|
|
1514
|
+
translateSetLang(replaceNode, insertOption);
|
|
1416
1515
|
}
|
|
1417
1516
|
} else if (option.translateType === TranslateTypeEnum.FULL_AUTO) {
|
|
1418
1517
|
// 全自动模式下还是只收集 单独 $t 调用
|
|
1419
|
-
if (callee.name === option.translateKey || callee.property && callee.property.name === option.translateKey) translateSetLang(node);
|
|
1518
|
+
if (callee.name === option.translateKey || callee.property && callee.property.name === option.translateKey) translateSetLang(node, insertOption);
|
|
1420
1519
|
}
|
|
1421
1520
|
}
|
|
1422
1521
|
};
|
|
@@ -1427,7 +1526,7 @@ function CallExpressionFn (insertOption) {
|
|
|
1427
1526
|
* @param node - 调用表达式节点
|
|
1428
1527
|
* @return
|
|
1429
1528
|
*/
|
|
1430
|
-
function translateSetLang(node) {
|
|
1529
|
+
function translateSetLang(node, insertOption) {
|
|
1431
1530
|
// 获取调用表达式的参数
|
|
1432
1531
|
let arg = node.arguments || [];
|
|
1433
1532
|
// 提取参数作为值
|
|
@@ -1448,6 +1547,13 @@ function translateSetLang(node) {
|
|
|
1448
1547
|
}
|
|
1449
1548
|
// 调用翻译工具的 setLangObj 方法设置语言对象属性
|
|
1450
1549
|
setLangObj(resolvedId, value);
|
|
1550
|
+
insertOption?.collector?.record?.({
|
|
1551
|
+
key: option.useValueAsKey ? value : resolvedId,
|
|
1552
|
+
value,
|
|
1553
|
+
namespace: option.namespace,
|
|
1554
|
+
resourcePath: insertOption?.resourcePath,
|
|
1555
|
+
root: insertOption?.root
|
|
1556
|
+
});
|
|
1451
1557
|
}
|
|
1452
1558
|
}
|
|
1453
1559
|
|
|
@@ -1602,7 +1708,7 @@ var index$1 = /*#__PURE__*/Object.freeze({
|
|
|
1602
1708
|
*/
|
|
1603
1709
|
module.exports = function (source) {
|
|
1604
1710
|
return __awaiter(this, void 0, void 0, function () {
|
|
1605
|
-
var baseUtils, option$1, filter, global, sourceObj, result, e_1;
|
|
1711
|
+
var baseUtils, option$1, filter, global, loaderOptions, sourceObj, result, e_1;
|
|
1606
1712
|
var _a;
|
|
1607
1713
|
return __generator(this, function (_b) {
|
|
1608
1714
|
switch (_b.label) {
|
|
@@ -1620,6 +1726,7 @@ module.exports = function (source) {
|
|
|
1620
1726
|
if (option$1.excludedPath.length && baseUtils.checkAgainstRegexArray(global.resourcePath, option$1.excludedPath)) {
|
|
1621
1727
|
return [2 /*return*/, source]; // 在黑名单目录中的文件,不处理,直接返回原始代码。
|
|
1622
1728
|
}
|
|
1729
|
+
loaderOptions = global.getOptions ? global.getOptions() : {};
|
|
1623
1730
|
if (!option$1.translateExtends) return [3 /*break*/, 2];
|
|
1624
1731
|
return [4 /*yield*/, (_a = option$1.translateExtends) === null || _a === void 0 ? void 0 : _a.handleInitFile(source, global.resourcePath)];
|
|
1625
1732
|
case 1:
|
|
@@ -1635,7 +1742,11 @@ module.exports = function (source) {
|
|
|
1635
1742
|
return [4 /*yield*/, babel__namespace.transformAsync(sourceObj.source, {
|
|
1636
1743
|
configFile: false,
|
|
1637
1744
|
// 不加载本地 Babel 配置文件
|
|
1638
|
-
plugins: [filter.default(sourceObj)
|
|
1745
|
+
plugins: [filter.default(__assign(__assign({}, sourceObj), {
|
|
1746
|
+
resourcePath: global.resourcePath,
|
|
1747
|
+
root: loaderOptions.root || global.rootContext,
|
|
1748
|
+
collector: loaderOptions.collector
|
|
1749
|
+
}))] // 使用核心模块提供的 `filter` 插件
|
|
1639
1750
|
})
|
|
1640
1751
|
// 如果转换成功,返回转换后的代码;否则返回空字符串。
|
|
1641
1752
|
];
|