@kuankuan/assist-2026 0.1.1 → 0.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/i18n.d.ts +15 -0
- package/dist/i18n.js +63 -0
- package/dist/plugins/buildInfo.d.ts +2 -0
- package/dist/plugins/buildInfo.js +100 -0
- package/dist/plugins/ki18nConfigLoad.d.ts +4 -0
- package/dist/plugins/ki18nConfigLoad.js +62 -0
- package/package.json +11 -5
- package/src/i18n.ts +69 -0
- package/src/plugins/buildInfo.ts +119 -0
- package/src/plugins/ki18nConfigLoad.ts +89 -0
- package/styles/theme.scss +0 -4
- package/types/ki18nVirtualModule.d.ts +5 -0
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
export declare const languageName: {
|
|
3
|
+
_auto: import("vue").ComputedRef<string>;
|
|
4
|
+
'zh-cn': string;
|
|
5
|
+
'en-us': string;
|
|
6
|
+
};
|
|
7
|
+
export declare const languageList: {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string | import("vue").ComputedRef<string>;
|
|
10
|
+
}[];
|
|
11
|
+
export declare const localSettingLanguage: Ref<string, string>;
|
|
12
|
+
export declare const language: import("vue").WritableComputedRef<string, string>;
|
|
13
|
+
export declare const i18n: import("vue-i18n").I18n<any, {}, {}, string, false>;
|
|
14
|
+
export declare function t(key: string): string;
|
|
15
|
+
export declare function tRef(key: string): import("vue").ComputedRef<string>;
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createI18n } from 'vue-i18n';
|
|
2
|
+
import { computed, ref, watch } from 'vue';
|
|
3
|
+
import messages, { langs } from 'virtual:k-i18n-config:main';
|
|
4
|
+
import storageRef from './ref/storageRef';
|
|
5
|
+
export const languageName = {
|
|
6
|
+
_auto: computed(() => `Auto (${languageName[browserLanguage.value]})`),
|
|
7
|
+
'zh-cn': '简体中文',
|
|
8
|
+
'en-us': 'English',
|
|
9
|
+
};
|
|
10
|
+
export const languageList = ['_auto', ...langs].map((i) => ({
|
|
11
|
+
id: i,
|
|
12
|
+
name: languageName[i],
|
|
13
|
+
}));
|
|
14
|
+
const LOCALSTORAGE_KEY = '_vue_i18n_main_locale';
|
|
15
|
+
function getBrowserLanguage() {
|
|
16
|
+
const languages = (navigator?.languages || []).map((i) => i.toLowerCase());
|
|
17
|
+
const _langsOnly = langs.map((i) => i.split('-')[0]);
|
|
18
|
+
for (const i of languages) {
|
|
19
|
+
if (langs.includes(i)) {
|
|
20
|
+
return i;
|
|
21
|
+
}
|
|
22
|
+
if (_langsOnly.includes(i)) {
|
|
23
|
+
return langs[_langsOnly.indexOf(i)];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return langs[0] || 'en-us';
|
|
27
|
+
}
|
|
28
|
+
const browserLanguage = ref(getBrowserLanguage());
|
|
29
|
+
export const localSettingLanguage = storageRef(LOCALSTORAGE_KEY, '_auto');
|
|
30
|
+
if (localSettingLanguage.value !== '_auto' && !langs.includes(localSettingLanguage.value)) {
|
|
31
|
+
localSettingLanguage.value = '_auto';
|
|
32
|
+
}
|
|
33
|
+
export const language = computed({
|
|
34
|
+
get: () => {
|
|
35
|
+
const currentBrowserLanguage = browserLanguage.value;
|
|
36
|
+
if (localSettingLanguage.value === '_auto') {
|
|
37
|
+
return currentBrowserLanguage;
|
|
38
|
+
}
|
|
39
|
+
return localSettingLanguage.value;
|
|
40
|
+
},
|
|
41
|
+
set: (newValue) => {
|
|
42
|
+
localSettingLanguage.value = newValue;
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
watch(language, (value) => {
|
|
46
|
+
i18n.global.locale.value = value;
|
|
47
|
+
});
|
|
48
|
+
export const i18n = createI18n({
|
|
49
|
+
legacy: false,
|
|
50
|
+
locale: language.value,
|
|
51
|
+
fallbackLocale: 'en-us',
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
+
messages: messages,
|
|
54
|
+
});
|
|
55
|
+
window.addEventListener('languagechange', () => {
|
|
56
|
+
browserLanguage.value = getBrowserLanguage();
|
|
57
|
+
});
|
|
58
|
+
export function t(key) {
|
|
59
|
+
return i18n.global.t(key);
|
|
60
|
+
}
|
|
61
|
+
export function tRef(key) {
|
|
62
|
+
return computed(() => i18n.global.t(key));
|
|
63
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import child_process from 'child_process';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
const MODULE_ID = 'visual:k-build-info';
|
|
4
|
+
function execSubProcess(command) {
|
|
5
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
6
|
+
child_process.exec(command, (err, stdout) => {
|
|
7
|
+
if (err) {
|
|
8
|
+
reject(err);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
resolve(stdout.trim());
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return promise;
|
|
15
|
+
}
|
|
16
|
+
async function getGitInfo() {
|
|
17
|
+
return {
|
|
18
|
+
branch: await execSubProcess('git rev-parse --abbrev-ref HEAD').then(void 0, () => 'unknown'),
|
|
19
|
+
lastCommit: {
|
|
20
|
+
time: await execSubProcess('git log -1 --format=%cd --date=iso')
|
|
21
|
+
.then((d) => new Date(d).getTime())
|
|
22
|
+
.then(void 0, () => NaN),
|
|
23
|
+
message: await execSubProcess('git log -1 --format=%s').then(void 0, () => 'unknown'),
|
|
24
|
+
author: await execSubProcess('git log -1 --format=%an').then(void 0, () => 'unknown'),
|
|
25
|
+
id: await execSubProcess('git log -1 --format=%h').then(void 0, () => 'unknown'),
|
|
26
|
+
fullId: await execSubProcess('git log -1 --format=%H').then(void 0, () => 'unknown'),
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export default function VitePluginBuildInfo() {
|
|
31
|
+
return {
|
|
32
|
+
name: 'vite-plugin-build-info',
|
|
33
|
+
resolveId(id) {
|
|
34
|
+
if (id === MODULE_ID) {
|
|
35
|
+
return '\0' + id;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
async load(id) {
|
|
39
|
+
if (id === '\0' + MODULE_ID) {
|
|
40
|
+
const res = {
|
|
41
|
+
buildTime: Date.now(),
|
|
42
|
+
meta: this.meta,
|
|
43
|
+
mode: this.environment.mode,
|
|
44
|
+
git: await getGitInfo(),
|
|
45
|
+
os: {
|
|
46
|
+
platform: os.platform(),
|
|
47
|
+
release: os.release(),
|
|
48
|
+
arch: os.arch(),
|
|
49
|
+
node: process.version,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
const showRes = await Promise.all([
|
|
53
|
+
{
|
|
54
|
+
title: '构建时间',
|
|
55
|
+
content: async () => new Date(res.buildTime).toLocaleString().replace(/ /g, '\n'),
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
title: '环境',
|
|
59
|
+
content: async () => res.mode.toUpperCase(),
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
title: '分支',
|
|
63
|
+
content: async () => res.git.branch,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
title: '上次提交',
|
|
67
|
+
content: async () => res.git.lastCommit.id,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
title: 'Vite',
|
|
71
|
+
content: async () => res.meta.viteVersion || 'unknown',
|
|
72
|
+
url: 'https://vite.dev/',
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
title: 'rolldownVersion' in res.meta ? 'Rolldown' : 'Rollup',
|
|
76
|
+
content: async () => res.meta
|
|
77
|
+
.rolldownVersion || res.meta.rollupVersion,
|
|
78
|
+
url: 'rolldownVersion' in res.meta ? 'https://rolldown.rs/' : 'https://rollupjs.org/',
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
title: 'Node',
|
|
82
|
+
content: async () => res.os.node,
|
|
83
|
+
url: 'https://nodejs.org/',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
title: '操作系统',
|
|
87
|
+
content: async () => `${res.os.platform}\n${res.os.arch}`,
|
|
88
|
+
},
|
|
89
|
+
].map((i) => Promise.resolve(typeof i.content === 'function' ? i.content() : i.content)
|
|
90
|
+
.then(void 0, () => 'unknown')
|
|
91
|
+
.then((content) => ({
|
|
92
|
+
title: i.title,
|
|
93
|
+
url: i.url,
|
|
94
|
+
content,
|
|
95
|
+
}))));
|
|
96
|
+
return `export default ${JSON.stringify(res)};export const show = ${JSON.stringify(showRes)};`;
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
function transformConfig(config) {
|
|
3
|
+
const keys = Object.keys(config);
|
|
4
|
+
const result = {};
|
|
5
|
+
if (keys.length === 0) {
|
|
6
|
+
return result;
|
|
7
|
+
}
|
|
8
|
+
if (typeof config[keys[0]] === 'string') {
|
|
9
|
+
const configItem = config;
|
|
10
|
+
for (const lang of keys) {
|
|
11
|
+
result[lang] = configItem[lang];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
const configItem = config;
|
|
16
|
+
for (const key of keys) {
|
|
17
|
+
const subResult = transformConfig(configItem[key]);
|
|
18
|
+
for (const lang in subResult) {
|
|
19
|
+
if (!(lang in result)) {
|
|
20
|
+
result[lang] = {};
|
|
21
|
+
}
|
|
22
|
+
result[lang][key] = subResult[lang];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
export default function VitePluginKI18nConfigLoad({ messages }) {
|
|
29
|
+
const configIds = Object.keys(messages).map((configId) => `virtual:k-i18n-config:${configId}`);
|
|
30
|
+
const configPaths = Object.values(messages);
|
|
31
|
+
const getConfig = async (id) => {
|
|
32
|
+
if (id.endsWith('.json')) {
|
|
33
|
+
return fs.promises.readFile(id, 'utf-8').then((data) => JSON.parse(data));
|
|
34
|
+
}
|
|
35
|
+
return import(id).then((module) => module.default);
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
name: 'vite-plugin-k-i18n-config-load',
|
|
39
|
+
buildStart() {
|
|
40
|
+
// 添加配置文件作为依赖,以便在它们变化时触发重新构建
|
|
41
|
+
for (const configPath of configPaths) {
|
|
42
|
+
this.addWatchFile(configPath);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
resolveId(id) {
|
|
46
|
+
if (configIds.includes(id)) {
|
|
47
|
+
return '\0' + id;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async load(id) {
|
|
51
|
+
if (id[0] === '\0' && configIds.includes(id.slice(1))) {
|
|
52
|
+
const configId = id.replace(/^\0virtual:k-i18n-config:/, '');
|
|
53
|
+
const configPath = messages[configId];
|
|
54
|
+
// 确保每次加载时都添加文件依赖
|
|
55
|
+
this.addWatchFile(configPath);
|
|
56
|
+
const config = await getConfig(configPath);
|
|
57
|
+
const transformedConfig = transformConfig(config.default || config);
|
|
58
|
+
return `export default ${JSON.stringify(transformedConfig, null, 2)};export const langs = ${JSON.stringify(Object.keys(transformedConfig))};`;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kuankuan/assist-2026",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A toolset from kuankuan",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,13 +30,15 @@
|
|
|
30
30
|
"eslint": "^10.0.3",
|
|
31
31
|
"prettier": "^3.8.1",
|
|
32
32
|
"typescript": "^5.9.3",
|
|
33
|
-
"typescript-eslint": "^8.56.1"
|
|
33
|
+
"typescript-eslint": "^8.56.1",
|
|
34
|
+
"vite": "^7.3.1"
|
|
34
35
|
},
|
|
35
36
|
"files": [
|
|
36
37
|
"dist",
|
|
37
38
|
"src",
|
|
38
39
|
"styles",
|
|
39
|
-
"bin"
|
|
40
|
+
"bin",
|
|
41
|
+
"types"
|
|
40
42
|
],
|
|
41
43
|
"engines": {
|
|
42
44
|
"node": ">=18.0.0"
|
|
@@ -71,7 +73,11 @@
|
|
|
71
73
|
}
|
|
72
74
|
},
|
|
73
75
|
"dependencies": {
|
|
74
|
-
"compressing": "
|
|
75
|
-
"vue": "
|
|
76
|
+
"compressing": ">=2.0.0",
|
|
77
|
+
"vue": ">=3.0.0",
|
|
78
|
+
"vue-i18n": ">=11.0.0"
|
|
79
|
+
},
|
|
80
|
+
"peerDependencies": {
|
|
81
|
+
"vite": ">=7.0.0"
|
|
76
82
|
}
|
|
77
83
|
}
|
package/src/i18n.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { createI18n } from 'vue-i18n';
|
|
2
|
+
import { computed, ref, watch, Ref } from 'vue';
|
|
3
|
+
import messages, { langs } from 'virtual:k-i18n-config:main';
|
|
4
|
+
import storageRef from './ref/storageRef';
|
|
5
|
+
|
|
6
|
+
export const languageName = {
|
|
7
|
+
_auto: computed(
|
|
8
|
+
(): string => `Auto (${languageName[browserLanguage.value as keyof typeof languageName]})`
|
|
9
|
+
),
|
|
10
|
+
'zh-cn': '简体中文',
|
|
11
|
+
'en-us': 'English',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const languageList = ['_auto', ...langs].map((i) => ({
|
|
15
|
+
id: i,
|
|
16
|
+
name: languageName[i as keyof typeof languageName],
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
const LOCALSTORAGE_KEY = '_vue_i18n_main_locale';
|
|
20
|
+
function getBrowserLanguage() {
|
|
21
|
+
const languages = (navigator?.languages || []).map((i) => i.toLowerCase());
|
|
22
|
+
const _langsOnly = langs.map((i: string) => i.split('-')[0]);
|
|
23
|
+
for (const i of languages) {
|
|
24
|
+
if (langs.includes(i)) {
|
|
25
|
+
return i;
|
|
26
|
+
}
|
|
27
|
+
if (_langsOnly.includes(i)) {
|
|
28
|
+
return langs[_langsOnly.indexOf(i)]!;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return langs[0] || 'en-us';
|
|
32
|
+
}
|
|
33
|
+
const browserLanguage = ref(getBrowserLanguage());
|
|
34
|
+
export const localSettingLanguage = storageRef(LOCALSTORAGE_KEY, '_auto');
|
|
35
|
+
if (localSettingLanguage.value !== '_auto' && !langs.includes(localSettingLanguage.value)) {
|
|
36
|
+
localSettingLanguage.value = '_auto';
|
|
37
|
+
}
|
|
38
|
+
export const language = computed({
|
|
39
|
+
get: () => {
|
|
40
|
+
const currentBrowserLanguage = browserLanguage.value;
|
|
41
|
+
if (localSettingLanguage.value === '_auto') {
|
|
42
|
+
return currentBrowserLanguage;
|
|
43
|
+
}
|
|
44
|
+
return localSettingLanguage.value;
|
|
45
|
+
},
|
|
46
|
+
set: (newValue) => {
|
|
47
|
+
localSettingLanguage.value = newValue;
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
watch(language, (value) => {
|
|
51
|
+
(i18n.global.locale as unknown as Ref<string>).value = value;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export const i18n = createI18n({
|
|
55
|
+
legacy: false,
|
|
56
|
+
locale: language.value,
|
|
57
|
+
fallbackLocale: 'en-us',
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
messages: messages as any,
|
|
60
|
+
});
|
|
61
|
+
window.addEventListener('languagechange', () => {
|
|
62
|
+
browserLanguage.value = getBrowserLanguage();
|
|
63
|
+
});
|
|
64
|
+
export function t(key: string) {
|
|
65
|
+
return i18n.global.t(key);
|
|
66
|
+
}
|
|
67
|
+
export function tRef(key: string) {
|
|
68
|
+
return computed(() => i18n.global.t(key));
|
|
69
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import child_process from 'child_process';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
|
|
5
|
+
const MODULE_ID = 'visual:k-build-info';
|
|
6
|
+
|
|
7
|
+
function execSubProcess(command: string) {
|
|
8
|
+
const { promise, resolve, reject } = Promise.withResolvers<string>();
|
|
9
|
+
child_process.exec(command, (err, stdout) => {
|
|
10
|
+
if (err) {
|
|
11
|
+
reject(err);
|
|
12
|
+
} else {
|
|
13
|
+
resolve(stdout.trim());
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return promise;
|
|
17
|
+
}
|
|
18
|
+
async function getGitInfo() {
|
|
19
|
+
return {
|
|
20
|
+
branch: await execSubProcess('git rev-parse --abbrev-ref HEAD').then(void 0, () => 'unknown'),
|
|
21
|
+
lastCommit: {
|
|
22
|
+
time: await execSubProcess('git log -1 --format=%cd --date=iso')
|
|
23
|
+
.then((d) => new Date(d).getTime())
|
|
24
|
+
.then(void 0, () => NaN),
|
|
25
|
+
message: await execSubProcess('git log -1 --format=%s').then(void 0, () => 'unknown'),
|
|
26
|
+
author: await execSubProcess('git log -1 --format=%an').then(void 0, () => 'unknown'),
|
|
27
|
+
id: await execSubProcess('git log -1 --format=%h').then(void 0, () => 'unknown'),
|
|
28
|
+
fullId: await execSubProcess('git log -1 --format=%H').then(void 0, () => 'unknown'),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default function VitePluginBuildInfo(): Plugin {
|
|
34
|
+
return {
|
|
35
|
+
name: 'vite-plugin-build-info',
|
|
36
|
+
|
|
37
|
+
resolveId(id) {
|
|
38
|
+
if (id === MODULE_ID) {
|
|
39
|
+
return '\0' + id;
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
async load(id) {
|
|
43
|
+
if (id === '\0' + MODULE_ID) {
|
|
44
|
+
const res = {
|
|
45
|
+
buildTime: Date.now(),
|
|
46
|
+
meta: this.meta,
|
|
47
|
+
mode: this.environment.mode,
|
|
48
|
+
git: await getGitInfo(),
|
|
49
|
+
os: {
|
|
50
|
+
platform: os.platform(),
|
|
51
|
+
release: os.release(),
|
|
52
|
+
arch: os.arch(),
|
|
53
|
+
node: process.version,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const showRes = await Promise.all(
|
|
57
|
+
(
|
|
58
|
+
[
|
|
59
|
+
{
|
|
60
|
+
title: '构建时间',
|
|
61
|
+
content: async () => new Date(res.buildTime).toLocaleString().replace(/ /g, '\n'),
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
title: '环境',
|
|
65
|
+
content: async () => res.mode.toUpperCase(),
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
title: '分支',
|
|
69
|
+
content: async () => res.git.branch,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
title: '上次提交',
|
|
73
|
+
content: async () => res.git.lastCommit.id,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
title: 'Vite',
|
|
77
|
+
content: async () =>
|
|
78
|
+
(res.meta as unknown as { viteVersion?: string }).viteVersion || 'unknown',
|
|
79
|
+
url: 'https://vite.dev/',
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
title: 'rolldownVersion' in res.meta ? 'Rolldown' : 'Rollup',
|
|
83
|
+
content: async () =>
|
|
84
|
+
((res.meta as { rolldownVersion?: string; rollupVersion?: string })
|
|
85
|
+
.rolldownVersion as string) || res.meta.rollupVersion,
|
|
86
|
+
url:
|
|
87
|
+
'rolldownVersion' in res.meta ? 'https://rolldown.rs/' : 'https://rollupjs.org/',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
title: 'Node',
|
|
91
|
+
content: async () => res.os.node,
|
|
92
|
+
url: 'https://nodejs.org/',
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
title: '操作系统',
|
|
96
|
+
content: async () => `${res.os.platform}\n${res.os.arch}`,
|
|
97
|
+
},
|
|
98
|
+
] as {
|
|
99
|
+
title: string;
|
|
100
|
+
content: Promise<string> | (() => Promise<string>);
|
|
101
|
+
url?: string;
|
|
102
|
+
}[]
|
|
103
|
+
).map((i) =>
|
|
104
|
+
Promise.resolve(typeof i.content === 'function' ? i.content() : i.content)
|
|
105
|
+
.then(void 0, () => 'unknown')
|
|
106
|
+
.then((content) => ({
|
|
107
|
+
title: i.title,
|
|
108
|
+
url: i.url,
|
|
109
|
+
content,
|
|
110
|
+
}))
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
return `export default ${JSON.stringify(res)};export const show = ${JSON.stringify(
|
|
114
|
+
showRes
|
|
115
|
+
)};`;
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
type I18nConfigItem = {
|
|
4
|
+
[lang in string]: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type I18nConfig =
|
|
8
|
+
| {
|
|
9
|
+
[key in string]: I18nConfig;
|
|
10
|
+
}
|
|
11
|
+
| I18nConfigItem;
|
|
12
|
+
|
|
13
|
+
type I18nConfigResult = {
|
|
14
|
+
[lang in string]: string | I18nConfigResult;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function transformConfig(config: I18nConfig) {
|
|
18
|
+
const keys = Object.keys(config);
|
|
19
|
+
|
|
20
|
+
const result: I18nConfigResult = {};
|
|
21
|
+
if (keys.length === 0) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
if (typeof config[keys[0]] === 'string') {
|
|
25
|
+
const configItem = config as I18nConfigItem;
|
|
26
|
+
for (const lang of keys) {
|
|
27
|
+
result[lang] = configItem[lang];
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
const configItem = config as { [key: string]: I18nConfig };
|
|
31
|
+
for (const key of keys) {
|
|
32
|
+
const subResult = transformConfig(configItem[key]);
|
|
33
|
+
for (const lang in subResult) {
|
|
34
|
+
if (!(lang in result)) {
|
|
35
|
+
result[lang] = {};
|
|
36
|
+
}
|
|
37
|
+
(
|
|
38
|
+
result[lang] as {
|
|
39
|
+
[lang in string]: string | I18nConfigResult;
|
|
40
|
+
}
|
|
41
|
+
)[key] = subResult[lang];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default function VitePluginKI18nConfigLoad({ messages }: { messages: Record<string, string> }): Plugin {
|
|
49
|
+
const configIds = Object.keys(messages).map((configId) => `virtual:k-i18n-config:${configId}`);
|
|
50
|
+
const configPaths = Object.values(messages);
|
|
51
|
+
|
|
52
|
+
const getConfig = async (id: string) => {
|
|
53
|
+
if (id.endsWith('.json')) {
|
|
54
|
+
return fs.promises.readFile(id, 'utf-8').then((data) => JSON.parse(data));
|
|
55
|
+
}
|
|
56
|
+
return import(id).then((module) => module.default);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
name: 'vite-plugin-k-i18n-config-load',
|
|
61
|
+
|
|
62
|
+
buildStart() {
|
|
63
|
+
// 添加配置文件作为依赖,以便在它们变化时触发重新构建
|
|
64
|
+
for (const configPath of configPaths) {
|
|
65
|
+
this.addWatchFile(configPath);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
resolveId(id) {
|
|
70
|
+
if (configIds.includes(id)) {
|
|
71
|
+
return '\0' + id;
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
async load(id) {
|
|
76
|
+
if (id[0] === '\0' && configIds.includes(id.slice(1))) {
|
|
77
|
+
const configId = id.replace(/^\0virtual:k-i18n-config:/, '');
|
|
78
|
+
const configPath = messages[configId];
|
|
79
|
+
|
|
80
|
+
// 确保每次加载时都添加文件依赖
|
|
81
|
+
this.addWatchFile(configPath);
|
|
82
|
+
|
|
83
|
+
const config = await getConfig(configPath);
|
|
84
|
+
const transformedConfig = transformConfig(config.default || config);
|
|
85
|
+
return `export default ${JSON.stringify(transformedConfig, null, 2)};export const langs = ${JSON.stringify(Object.keys(transformedConfig))};`;
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
package/styles/theme.scss
CHANGED
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
$defaultTheme: (
|
|
6
6
|
light: (
|
|
7
|
-
link-color: rgb(0, 0, 130),
|
|
8
|
-
link-hover-color: rgb(0, 0, 69),
|
|
9
7
|
color: #111111,
|
|
10
8
|
background: #eeeeee,
|
|
11
9
|
active-color: #0006c1,
|
|
@@ -14,8 +12,6 @@ $defaultTheme: (
|
|
|
14
12
|
name: 'light',
|
|
15
13
|
),
|
|
16
14
|
dark: (
|
|
17
|
-
link-color: rgb(90, 90, 255),
|
|
18
|
-
link-hover-color: rgb(146, 146, 255),
|
|
19
15
|
color: #eeeeee,
|
|
20
16
|
background: #1e1e1e,
|
|
21
17
|
active-color: #686dff,
|