vue-i18n-extract-plugin 1.0.65 → 1.0.67
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/.github/workflows/deploy.yml +24 -0
- package/README.md +1 -1
- package/lib/babel-plugin-import-i18n.js +3 -2
- package/lib/extract.js +3 -2
- package/lib/import-i18n-transform.js +19 -8
- package/lib/options.js +1 -1
- package/lib/utils.js +5 -0
- package/lib/vite-plugin-import-i18n.js +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +2 -0
- package/types/options.d.ts +1 -1
- package/types/utils.d.ts +1 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Publish to NPM
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [created]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout
|
|
13
|
+
uses: actions/checkout@v2
|
|
14
|
+
|
|
15
|
+
- name: Setup Node
|
|
16
|
+
uses: actions/setup-node@v3
|
|
17
|
+
with:
|
|
18
|
+
node-version: 22
|
|
19
|
+
registry-url: "https://registry.npmjs.org"
|
|
20
|
+
|
|
21
|
+
- name: Publish package to NPM 📦
|
|
22
|
+
run: npm publish
|
|
23
|
+
env:
|
|
24
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/README.md
CHANGED
|
@@ -64,7 +64,7 @@ const defaultOptions = {
|
|
|
64
64
|
excludedCall: [], // 排除的调用函数名称数组,目前已内置的函数请参阅:https://github.com/semdy/vue-i18n-extract-plugin/blob/main/lib/utils.js#L189
|
|
65
65
|
includePath: ['src/'], // 包含路径的数组
|
|
66
66
|
excludedPath: [], // 排除路径的数组 refer to https://github.com/mrmlnc/fast-glob?tab=readme-ov-file#how-to-exclude-directory-from-reading
|
|
67
|
-
allowedExtensions: [".vue", ".tsx", ".ts", ".jsx", ".js"], // 允许提取的文件扩展名
|
|
67
|
+
allowedExtensions: [".vue", ".nvue", ".uvue", ".tsx", ".ts", ".jsx", ".js"], // 允许提取的文件扩展名
|
|
68
68
|
fromLang: 'zh-cn', // 源语言, 目前支持提取的语言有:zh-cn(zh-tw), en, ja, ko, ru
|
|
69
69
|
translateLangKeys: ["zh-tw", "en"], // 需要翻译为的语言键
|
|
70
70
|
i18nPkgImportPath: "@/i18n", // i18n语言包导入路径
|
|
@@ -11,13 +11,14 @@ module.exports = function () {
|
|
|
11
11
|
i18nPkgImportPath: importPath,
|
|
12
12
|
JSXElement,
|
|
13
13
|
jsx,
|
|
14
|
-
enabled
|
|
14
|
+
enabled,
|
|
15
|
+
autoImportI18n
|
|
15
16
|
} = {
|
|
16
17
|
...defaultOptions,
|
|
17
18
|
...state.opts
|
|
18
19
|
};
|
|
19
20
|
|
|
20
|
-
if (!enabled) return;
|
|
21
|
+
if (!enabled || !autoImportI18n) return;
|
|
21
22
|
|
|
22
23
|
const ast = path.parentPath?.node || path.node;
|
|
23
24
|
const importNames = jsx ? [importName, JSXElement] : [importName];
|
package/lib/extract.js
CHANGED
|
@@ -23,7 +23,8 @@ const {
|
|
|
23
23
|
padEmptyLine,
|
|
24
24
|
isEmptyObject,
|
|
25
25
|
getLangJsonPath,
|
|
26
|
-
readJsonWithDefault
|
|
26
|
+
readJsonWithDefault,
|
|
27
|
+
isVueLike
|
|
27
28
|
} = require("./utils");
|
|
28
29
|
const { defaultOptions } = require("./options");
|
|
29
30
|
const { autoTranslate, cleanTranslate, cleanI18nMap } = require("./translate");
|
|
@@ -545,7 +546,7 @@ async function extractI18n(options) {
|
|
|
545
546
|
if (!content.trim()) continue;
|
|
546
547
|
|
|
547
548
|
try {
|
|
548
|
-
if (file
|
|
549
|
+
if (isVueLike(file)) {
|
|
549
550
|
const vueContent = processVueFile(content, options);
|
|
550
551
|
changed = vueContent.changed;
|
|
551
552
|
code = vueContent.code;
|
|
@@ -2,6 +2,7 @@ const parser = require("@babel/parser");
|
|
|
2
2
|
const traverse = require("@babel/traverse").default;
|
|
3
3
|
const generate = require("@babel/generator").default;
|
|
4
4
|
const t = require("@babel/types");
|
|
5
|
+
const { isVueLike } = require("./utils");
|
|
5
6
|
|
|
6
7
|
function i18nImportAstTransform(ast, importName, importPath) {
|
|
7
8
|
let hasI18nImport = false;
|
|
@@ -19,12 +20,15 @@ function i18nImportAstTransform(ast, importName, importPath) {
|
|
|
19
20
|
|
|
20
21
|
const sourcePath = path.node.source.value;
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
// 判断是否有 import { $t } from '@/i18n' 或 import $t from '@/i18n'
|
|
24
|
+
const existImport = path.node.specifiers.some(
|
|
25
|
+
spec =>
|
|
24
26
|
(t.isImportSpecifier(spec) || t.isImportDefaultSpecifier(spec)) &&
|
|
25
27
|
spec.local.name === importName
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// 判断是否是其它路径导入了 $t
|
|
31
|
+
const importedElsewhere = sourcePath !== importPath && existImport;
|
|
28
32
|
|
|
29
33
|
if (importedElsewhere) {
|
|
30
34
|
conflictDefined = true;
|
|
@@ -32,12 +36,17 @@ function i18nImportAstTransform(ast, importName, importPath) {
|
|
|
32
36
|
return;
|
|
33
37
|
}
|
|
34
38
|
|
|
39
|
+
// 检查是否已经导入目标路径
|
|
35
40
|
if (sourcePath === importPath) {
|
|
41
|
+
hasI18nImport = true;
|
|
42
|
+
|
|
43
|
+
if (existImport) return;
|
|
44
|
+
|
|
45
|
+
// 添加 $t 到已有的 import
|
|
36
46
|
path.node.specifiers.push(
|
|
37
47
|
t.importSpecifier(t.identifier(importName), t.identifier(importName))
|
|
38
48
|
);
|
|
39
49
|
|
|
40
|
-
hasI18nImport = true;
|
|
41
50
|
needTransform = true;
|
|
42
51
|
}
|
|
43
52
|
},
|
|
@@ -81,6 +90,8 @@ function i18nImportAstTransform(ast, importName, importPath) {
|
|
|
81
90
|
} else {
|
|
82
91
|
ast.program.body.unshift(importNode);
|
|
83
92
|
}
|
|
93
|
+
|
|
94
|
+
needTransform = true;
|
|
84
95
|
}
|
|
85
96
|
|
|
86
97
|
return {
|
|
@@ -99,7 +110,7 @@ async function i18nImportTransform(code, path, importNames, importPath) {
|
|
|
99
110
|
plugins: [
|
|
100
111
|
"typescript",
|
|
101
112
|
"jsx",
|
|
102
|
-
path
|
|
113
|
+
isVueLike(path) ? "topLevelAwait" : null
|
|
103
114
|
].filter(Boolean)
|
|
104
115
|
});
|
|
105
116
|
|
|
@@ -117,7 +128,7 @@ async function i18nImportTransform(code, path, importNames, importPath) {
|
|
|
117
128
|
// 只有当需要修改时才重新生成代码
|
|
118
129
|
if (transformNeeded) {
|
|
119
130
|
const { code: newScript } = generate(ast);
|
|
120
|
-
return path
|
|
131
|
+
return isVueLike(path)
|
|
121
132
|
? code.replace(scriptContent, newScript)
|
|
122
133
|
: newScript;
|
|
123
134
|
}
|
|
@@ -129,7 +140,7 @@ async function i18nImportTransform(code, path, importNames, importPath) {
|
|
|
129
140
|
}
|
|
130
141
|
|
|
131
142
|
function extractScriptContent(code, path) {
|
|
132
|
-
if (!path
|
|
143
|
+
if (!isVueLike(path)) return code;
|
|
133
144
|
const scriptMatch = code.match(/<script\b[^>]*>([\s\S]*?)<\/script>/);
|
|
134
145
|
return scriptMatch?.[1] || "";
|
|
135
146
|
}
|
package/lib/options.js
CHANGED
|
@@ -18,7 +18,7 @@ const defaultOptions = {
|
|
|
18
18
|
excludedCall: [], // 排除的调用函数名称数组
|
|
19
19
|
includePath: ["src/"], // 包含路径的数组
|
|
20
20
|
excludedPath: [], // 排除路径的数组
|
|
21
|
-
allowedExtensions: [".vue", ".tsx", ".ts", ".jsx", ".js"], // 允许提取的文件扩展名
|
|
21
|
+
allowedExtensions: [".vue", ".nvue", ".uvue", ".tsx", ".ts", ".jsx", ".js"], // 允许提取的文件扩展名
|
|
22
22
|
fromLang: "zh-cn", // 源语言, 目前支持提取的语言有:zh-cn(zh-tw), en, ja, ko, ru
|
|
23
23
|
translateLangKeys: ["zh-tw", "en"], // 需要翻译为的语言键
|
|
24
24
|
i18nPkgImportPath: "@/i18n", // i18n语言包导入路径
|
package/lib/utils.js
CHANGED
|
@@ -46,6 +46,10 @@ function isEmptyObject(obj) {
|
|
|
46
46
|
return Object.keys(obj).length === 0;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
function isVueLike(filePath) {
|
|
50
|
+
return /\.(vue|nvue|uvue)$/.test(filePath);
|
|
51
|
+
}
|
|
52
|
+
|
|
49
53
|
function checkAgainstRegexArray(value, regexArray = []) {
|
|
50
54
|
for (let i = 0; i < regexArray.length; i++) {
|
|
51
55
|
const regex =
|
|
@@ -259,6 +263,7 @@ module.exports = {
|
|
|
259
263
|
generateId,
|
|
260
264
|
parseArg,
|
|
261
265
|
isEmptyObject,
|
|
266
|
+
isVueLike,
|
|
262
267
|
checkAgainstRegexArray,
|
|
263
268
|
extractFunctionName,
|
|
264
269
|
relativeCWDPath,
|
|
@@ -11,7 +11,7 @@ function vitePluginImportI18n(option) {
|
|
|
11
11
|
name: "vite-plugin-import-i18n",
|
|
12
12
|
enforce: "pre",
|
|
13
13
|
async transform(code, path) {
|
|
14
|
-
if (!option.enabled || !filter(path)) return;
|
|
14
|
+
if (!option.enabled || !option.autoImportI18n || !filter(path)) return;
|
|
15
15
|
|
|
16
16
|
return i18nImportTransform(
|
|
17
17
|
code,
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
package/types/options.d.ts
CHANGED
package/types/utils.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export function hashKey(str: string): string;
|
|
|
5
5
|
export function generateId(text: string, length?: number): string;
|
|
6
6
|
export function parseArg(arg: string): any;
|
|
7
7
|
export function isEmptyObject(obj: Record<string, any>): boolean;
|
|
8
|
+
export function isVueLike(filePath: string): boolean;
|
|
8
9
|
export function checkAgainstRegexArray(value: string, regexArray?: (string | RegExp)[]): boolean;
|
|
9
10
|
export function extractFunctionName(path: NodePath): string;
|
|
10
11
|
export function relativeCWDPath(subPath: string): string;
|