vite-gc-i18n-plugin 1.1.1 → 1.1.3
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 +53 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +52 -15
- 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: {
|
|
@@ -2167,7 +2169,8 @@ async function upload(json) {
|
|
|
2167
2169
|
data
|
|
2168
2170
|
}, {
|
|
2169
2171
|
headers: {
|
|
2170
|
-
'Content-Type': 'application/json'
|
|
2172
|
+
'Content-Type': 'application/json',
|
|
2173
|
+
Authorization: '728635D658AE11F18F33000C29A621CA'
|
|
2171
2174
|
}
|
|
2172
2175
|
});
|
|
2173
2176
|
if (res.status === 200 && res.data.result === 0) {
|
|
@@ -2315,15 +2318,9 @@ function handleTemplateLiteralWithExpressions(node, path) {
|
|
|
2315
2318
|
valStr
|
|
2316
2319
|
} = normalizeTranslateValue(fullValue);
|
|
2317
2320
|
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);
|
|
2321
|
+
const translateNode = createTranslateNode(path, id, valStr, placeholders, node.expressions);
|
|
2322
|
+
path.replaceWith(translateNode);
|
|
2323
|
+
path.skip();
|
|
2327
2324
|
setLangObj(id, trimmedValue);
|
|
2328
2325
|
return true;
|
|
2329
2326
|
}
|
|
@@ -2335,7 +2332,7 @@ function getExpressionPlaceholder(expression) {
|
|
|
2335
2332
|
const objectName = getExpressionPlaceholder(expression.object);
|
|
2336
2333
|
const propertyName = expression.computed ? getExpressionPlaceholder(expression.property) : types.isIdentifier(expression.property) ? expression.property.name : '';
|
|
2337
2334
|
const value = [objectName, propertyName].filter(Boolean).join('.');
|
|
2338
|
-
return value
|
|
2335
|
+
return cleanVueRuntimePrefix(value);
|
|
2339
2336
|
}
|
|
2340
2337
|
if (types.isStringLiteral(expression) || types.isNumericLiteral(expression)) {
|
|
2341
2338
|
return String(expression.value);
|
|
@@ -2346,7 +2343,37 @@ function getExpressionPlaceholder(expression) {
|
|
|
2346
2343
|
if (types.isTSNonNullExpression(expression)) {
|
|
2347
2344
|
return getExpressionPlaceholder(expression.expression);
|
|
2348
2345
|
}
|
|
2349
|
-
|
|
2346
|
+
const generateCode = generate__namespace.default?.default || generate__namespace.default || generate__namespace;
|
|
2347
|
+
return cleanVueRuntimePrefix(generateCode(expression).code);
|
|
2348
|
+
}
|
|
2349
|
+
function createTranslateNode(path, id, valStr) {
|
|
2350
|
+
let placeholders = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
|
|
2351
|
+
let expressions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
|
|
2352
|
+
const args = [types.stringLiteral(id), types.stringLiteral(valStr)];
|
|
2353
|
+
if (placeholders.length) {
|
|
2354
|
+
args.push(types.objectExpression(placeholders.map((placeholder, index) => types.objectProperty(types.stringLiteral(placeholder), types.cloneNode(expressions[index])))));
|
|
2355
|
+
}
|
|
2356
|
+
const translateCall = types.callExpression(types.identifier(exports.option.translateKey), args);
|
|
2357
|
+
if (!isVueTemplateRender(path)) {
|
|
2358
|
+
return translateCall;
|
|
2359
|
+
}
|
|
2360
|
+
return types.sequenceExpression([createVueTemplateLanguageDependency(), translateCall]);
|
|
2361
|
+
}
|
|
2362
|
+
function isVueTemplateRender(path) {
|
|
2363
|
+
return Boolean(path.scope?.getBinding('_vm'));
|
|
2364
|
+
}
|
|
2365
|
+
function createVueTemplateLanguageDependency() {
|
|
2366
|
+
const vm = types.identifier('_vm');
|
|
2367
|
+
const store = types.memberExpression(types.cloneNode(vm), types.identifier('$store'));
|
|
2368
|
+
const storeState = types.memberExpression(types.cloneNode(store), types.identifier('state'));
|
|
2369
|
+
const siteState = types.memberExpression(types.cloneNode(storeState), types.identifier('site'));
|
|
2370
|
+
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')));
|
|
2371
|
+
const i18n = types.memberExpression(types.cloneNode(vm), types.identifier('$i18n'));
|
|
2372
|
+
const i18nLocale = types.logicalExpression('&&', types.cloneNode(i18n), types.memberExpression(types.cloneNode(i18n), types.identifier('locale')));
|
|
2373
|
+
return types.logicalExpression('||', storeLanguage, i18nLocale);
|
|
2374
|
+
}
|
|
2375
|
+
function cleanVueRuntimePrefix(value) {
|
|
2376
|
+
return value.replace(/^(?:\$setup|_ctx|_vm|this)\./, '').replace(/([^\w$.])(?:\$setup|_ctx|_vm|this)\./g, '$1');
|
|
2350
2377
|
}
|
|
2351
2378
|
|
|
2352
2379
|
// 处理模板元素
|
|
@@ -2469,7 +2496,7 @@ function StringLiteralFn (insertOption) {
|
|
|
2469
2496
|
const extractFnName = extractFunctionName(parent);
|
|
2470
2497
|
|
|
2471
2498
|
// 防止导入语句,只处理那些当前节点不是键值对的键的字符串字面量,调用语句判断当前调用语句是否包含需要过滤的调用语句
|
|
2472
|
-
if (parent
|
|
2499
|
+
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
2500
|
let replaceNode;
|
|
2474
2501
|
if (exports.option.deepScan && checkNeedSplit(value)) {
|
|
2475
2502
|
replaceNode = convertToTemplateLiteral(splitByRegex(value, getOriginRegex()), insertOption);
|
|
@@ -2491,6 +2518,17 @@ function StringLiteralFn (insertOption) {
|
|
|
2491
2518
|
}
|
|
2492
2519
|
};
|
|
2493
2520
|
}
|
|
2521
|
+
function isTranslateCall(node) {
|
|
2522
|
+
if (!types__namespace.isCallExpression(node)) return false;
|
|
2523
|
+
const callee = node.callee;
|
|
2524
|
+
if (types__namespace.isIdentifier(callee)) {
|
|
2525
|
+
return callee.name === exports.option.translateKey;
|
|
2526
|
+
}
|
|
2527
|
+
if (types__namespace.isMemberExpression(callee)) {
|
|
2528
|
+
return types__namespace.isIdentifier(callee.property) && callee.property.name === exports.option.translateKey;
|
|
2529
|
+
}
|
|
2530
|
+
return false;
|
|
2531
|
+
}
|
|
2494
2532
|
|
|
2495
2533
|
/*
|
|
2496
2534
|
* @Author: xiaoshanwen
|