vite-gc-i18n-plugin 1.1.1 → 1.1.2
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.cjs +51 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +50 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -12,6 +12,7 @@ var _ = require('lodash');
|
|
|
12
12
|
var path = require('node:path');
|
|
13
13
|
var fs = require('node:fs');
|
|
14
14
|
var babel = require('@babel/core');
|
|
15
|
+
var generate = require('@babel/generator');
|
|
15
16
|
|
|
16
17
|
function _interopNamespaceDefault(e) {
|
|
17
18
|
var n = Object.create(null);
|
|
@@ -33,6 +34,7 @@ function _interopNamespaceDefault(e) {
|
|
|
33
34
|
var OpenCC__namespace = /*#__PURE__*/_interopNamespaceDefault(OpenCC);
|
|
34
35
|
var types__namespace = /*#__PURE__*/_interopNamespaceDefault(types);
|
|
35
36
|
var babel__namespace = /*#__PURE__*/_interopNamespaceDefault(babel);
|
|
37
|
+
var generate__namespace = /*#__PURE__*/_interopNamespaceDefault(generate);
|
|
36
38
|
|
|
37
39
|
/******************************************************************************
|
|
38
40
|
Copyright (c) Microsoft Corporation.
|
|
@@ -1028,8 +1030,8 @@ const DEFAULT_OPTION = {
|
|
|
1028
1030
|
useValueAsKey: false,
|
|
1029
1031
|
/** 是否在构建结束之后将最新的翻译重新打包到主包中,默认不打包 */
|
|
1030
1032
|
buildToDist: false,
|
|
1031
|
-
/**
|
|
1032
|
-
uploadEnabled:
|
|
1033
|
+
/** 是否启用上传翻译文件到服务器,默认开启 */
|
|
1034
|
+
uploadEnabled: true,
|
|
1033
1035
|
/** 翻译器,决定自动翻译使用的api与调用方式,默认使用 Google 翻译器并使用7890(clash)端口代理 */
|
|
1034
1036
|
translator: new GoogleTranslator({
|
|
1035
1037
|
proxyOption: {
|
|
@@ -2315,15 +2317,9 @@ function handleTemplateLiteralWithExpressions(node, path) {
|
|
|
2315
2317
|
valStr
|
|
2316
2318
|
} = normalizeTranslateValue(fullValue);
|
|
2317
2319
|
const id = generateId(valStr);
|
|
2318
|
-
const translateNode =
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
const valueMap = types.objectExpression(node.expressions.map((expression, index) => {
|
|
2322
|
-
const placeholder = placeholders[index];
|
|
2323
|
-
return types.objectProperty(types.stringLiteral(placeholder), expression);
|
|
2324
|
-
}));
|
|
2325
|
-
const interpolationNode = types.callExpression(types.arrowFunctionExpression([types.identifier('__value'), valuesName], types.callExpression(types.memberExpression(types.identifier('__value'), types.identifier('replace')), [types.regExpLiteral('\\\\?\\$\\{([^}]+)\\}', 'g'), types.arrowFunctionExpression([types.identifier('__match'), keyName], types.conditionalExpression(types.binaryExpression('in', keyName, valuesName), types.memberExpression(valuesName, keyName, true), types.identifier('__match')))])), [translateNode, valueMap]);
|
|
2326
|
-
path.replaceWith(interpolationNode);
|
|
2320
|
+
const translateNode = createTranslateNode(path, id, valStr, placeholders, node.expressions);
|
|
2321
|
+
path.replaceWith(translateNode);
|
|
2322
|
+
path.skip();
|
|
2327
2323
|
setLangObj(id, trimmedValue);
|
|
2328
2324
|
return true;
|
|
2329
2325
|
}
|
|
@@ -2335,7 +2331,7 @@ function getExpressionPlaceholder(expression) {
|
|
|
2335
2331
|
const objectName = getExpressionPlaceholder(expression.object);
|
|
2336
2332
|
const propertyName = expression.computed ? getExpressionPlaceholder(expression.property) : types.isIdentifier(expression.property) ? expression.property.name : '';
|
|
2337
2333
|
const value = [objectName, propertyName].filter(Boolean).join('.');
|
|
2338
|
-
return value
|
|
2334
|
+
return cleanVueRuntimePrefix(value);
|
|
2339
2335
|
}
|
|
2340
2336
|
if (types.isStringLiteral(expression) || types.isNumericLiteral(expression)) {
|
|
2341
2337
|
return String(expression.value);
|
|
@@ -2346,7 +2342,37 @@ function getExpressionPlaceholder(expression) {
|
|
|
2346
2342
|
if (types.isTSNonNullExpression(expression)) {
|
|
2347
2343
|
return getExpressionPlaceholder(expression.expression);
|
|
2348
2344
|
}
|
|
2349
|
-
|
|
2345
|
+
const generateCode = generate__namespace.default?.default || generate__namespace.default || generate__namespace;
|
|
2346
|
+
return cleanVueRuntimePrefix(generateCode(expression).code);
|
|
2347
|
+
}
|
|
2348
|
+
function createTranslateNode(path, id, valStr) {
|
|
2349
|
+
let placeholders = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
|
|
2350
|
+
let expressions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
|
|
2351
|
+
const args = [types.stringLiteral(id), types.stringLiteral(valStr)];
|
|
2352
|
+
if (placeholders.length) {
|
|
2353
|
+
args.push(types.objectExpression(placeholders.map((placeholder, index) => types.objectProperty(types.stringLiteral(placeholder), types.cloneNode(expressions[index])))));
|
|
2354
|
+
}
|
|
2355
|
+
const translateCall = types.callExpression(types.identifier(exports.option.translateKey), args);
|
|
2356
|
+
if (!isVueTemplateRender(path)) {
|
|
2357
|
+
return translateCall;
|
|
2358
|
+
}
|
|
2359
|
+
return types.sequenceExpression([createVueTemplateLanguageDependency(), translateCall]);
|
|
2360
|
+
}
|
|
2361
|
+
function isVueTemplateRender(path) {
|
|
2362
|
+
return Boolean(path.scope?.getBinding('_vm'));
|
|
2363
|
+
}
|
|
2364
|
+
function createVueTemplateLanguageDependency() {
|
|
2365
|
+
const vm = types.identifier('_vm');
|
|
2366
|
+
const store = types.memberExpression(types.cloneNode(vm), types.identifier('$store'));
|
|
2367
|
+
const storeState = types.memberExpression(types.cloneNode(store), types.identifier('state'));
|
|
2368
|
+
const siteState = types.memberExpression(types.cloneNode(storeState), types.identifier('site'));
|
|
2369
|
+
const storeLanguage = types.logicalExpression('&&', types.logicalExpression('&&', types.logicalExpression('&&', types.cloneNode(store), types.cloneNode(storeState)), types.cloneNode(siteState)), types.memberExpression(types.cloneNode(siteState), types.identifier('language')));
|
|
2370
|
+
const i18n = types.memberExpression(types.cloneNode(vm), types.identifier('$i18n'));
|
|
2371
|
+
const i18nLocale = types.logicalExpression('&&', types.cloneNode(i18n), types.memberExpression(types.cloneNode(i18n), types.identifier('locale')));
|
|
2372
|
+
return types.logicalExpression('||', storeLanguage, i18nLocale);
|
|
2373
|
+
}
|
|
2374
|
+
function cleanVueRuntimePrefix(value) {
|
|
2375
|
+
return value.replace(/^(?:\$setup|_ctx|_vm|this)\./, '').replace(/([^\w$.])(?:\$setup|_ctx|_vm|this)\./g, '$1');
|
|
2350
2376
|
}
|
|
2351
2377
|
|
|
2352
2378
|
// 处理模板元素
|
|
@@ -2469,7 +2495,7 @@ function StringLiteralFn (insertOption) {
|
|
|
2469
2495
|
const extractFnName = extractFunctionName(parent);
|
|
2470
2496
|
|
|
2471
2497
|
// 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
|
|
2472
|
-
if (parent
|
|
2498
|
+
if (isTranslateCall(parent) || types__namespace.isImportDeclaration(parent) || parent.key === node || types__namespace.isCallExpression(parent) && extractFnName && (exports.option.excludedCall.includes(extractFnName) || extractFnName?.split('.')?.pop() && exports.option.excludedCall.includes(extractFnName?.split('.')?.pop() || ''))) return;
|
|
2473
2499
|
let replaceNode;
|
|
2474
2500
|
if (exports.option.deepScan && checkNeedSplit(value)) {
|
|
2475
2501
|
replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
|
|
@@ -2491,6 +2517,17 @@ function StringLiteralFn (insertOption) {
|
|
|
2491
2517
|
}
|
|
2492
2518
|
};
|
|
2493
2519
|
}
|
|
2520
|
+
function isTranslateCall(node) {
|
|
2521
|
+
if (!types__namespace.isCallExpression(node)) return false;
|
|
2522
|
+
const callee = node.callee;
|
|
2523
|
+
if (types__namespace.isIdentifier(callee)) {
|
|
2524
|
+
return callee.name === exports.option.translateKey;
|
|
2525
|
+
}
|
|
2526
|
+
if (types__namespace.isMemberExpression(callee)) {
|
|
2527
|
+
return types__namespace.isIdentifier(callee.property) && callee.property.name === exports.option.translateKey;
|
|
2528
|
+
}
|
|
2529
|
+
return false;
|
|
2530
|
+
}
|
|
2494
2531
|
|
|
2495
2532
|
/*
|
|
2496
2533
|
* @Author: xiaoshanwen
|