monaco-editor-nls-adapter 2.2.0 → 2.2.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/codegen.js +37 -0
- package/lite.js +2 -1
- package/package.json +7 -2
- package/vite-plugin.js +5 -3
package/codegen.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 生成按需加载语言包的代码片段
|
|
3
|
+
* @param {string[]} languages 语言代码数组
|
|
4
|
+
* @param {boolean} isAsync 是否为异步加载 (ESM/import)
|
|
5
|
+
* @returns {string} 代码片段
|
|
6
|
+
*/
|
|
7
|
+
function generateLocalesCode(languages, isAsync) {
|
|
8
|
+
if (!languages || languages.length === 0) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (isAsync) {
|
|
13
|
+
// 异步版本 (用于 initAsync)
|
|
14
|
+
const map = languages.map(lang => ` '${lang}': () => import('./locales/${lang}.json')`).join(',\n');
|
|
15
|
+
return `(async (locale) => {
|
|
16
|
+
const locales = {
|
|
17
|
+
${map}
|
|
18
|
+
};
|
|
19
|
+
if (locales[locale]) return await locales[locale]();
|
|
20
|
+
throw new Error('[monaco-editor-nls-adapter] Locale not bundled: ' + locale);
|
|
21
|
+
})(targetLocale)`;
|
|
22
|
+
} else {
|
|
23
|
+
// 同步版本 (用于 init)
|
|
24
|
+
const map = languages.map(lang => ` '${lang}': () => require('./locales/${lang}.json')`).join(',\n');
|
|
25
|
+
return `((locale) => {
|
|
26
|
+
const locales = {
|
|
27
|
+
${map}
|
|
28
|
+
};
|
|
29
|
+
if (locales[locale]) return locales[locale]();
|
|
30
|
+
throw new Error('[monaco-editor-nls-adapter] Locale not bundled: ' + locale);
|
|
31
|
+
})(targetLocale)`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
generateLocalesCode
|
|
37
|
+
};
|
package/lite.js
CHANGED
|
@@ -39,5 +39,6 @@ module.exports = {
|
|
|
39
39
|
proxy,
|
|
40
40
|
// 插件导出 (仍保留,以便用户可以从 lite 引用所有工具)
|
|
41
41
|
vitePlugin: require('./vite-plugin'),
|
|
42
|
-
|
|
42
|
+
// __dirname 仅在 Node 环境存在;浏览器端打包时需要兜底,避免运行时 ReferenceError
|
|
43
|
+
loader: (typeof __dirname !== 'undefined' ? __dirname : '') + '/loader.js'
|
|
43
44
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "monaco-editor-nls-adapter",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"description": "Multi-language NLS adapter for Monaco Editor 0.50.0+ (Self-hosted)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.js",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"./proxy": {
|
|
20
20
|
"types": "./proxy.d.ts",
|
|
21
|
-
"require": "./proxy.js"
|
|
21
|
+
"require": "./proxy.js",
|
|
22
|
+
"default": "./proxy.js"
|
|
22
23
|
},
|
|
23
24
|
"./loader": {
|
|
24
25
|
"types": "./loader.d.ts",
|
|
@@ -41,6 +42,7 @@
|
|
|
41
42
|
"loader.js",
|
|
42
43
|
"proxy.js",
|
|
43
44
|
"transform.js",
|
|
45
|
+
"codegen.js",
|
|
44
46
|
"vite-plugin.js",
|
|
45
47
|
"locales/",
|
|
46
48
|
"README.md",
|
|
@@ -69,6 +71,9 @@
|
|
|
69
71
|
"url": "https://github.com/leepule/monaco-editor-nls-adapter/issues"
|
|
70
72
|
},
|
|
71
73
|
"homepage": "https://github.com/leepule/monaco-editor-nls-adapter#readme",
|
|
74
|
+
"publishConfig": {
|
|
75
|
+
"registry": "https://registry.npmjs.org/"
|
|
76
|
+
},
|
|
72
77
|
"peerDependencies": {
|
|
73
78
|
"monaco-editor": ">= 0.50.0"
|
|
74
79
|
},
|
package/vite-plugin.js
CHANGED
|
@@ -15,10 +15,12 @@ function monacoNlsPlugin(options = {}) {
|
|
|
15
15
|
name: 'vite-plugin-monaco-nls-adapter',
|
|
16
16
|
enforce: 'pre',
|
|
17
17
|
transform(code, id) {
|
|
18
|
-
|
|
18
|
+
// Vite dev 模式下 id 可能携带查询参数 (如 ?v=xxxx),需先剥离再判断
|
|
19
|
+
const cleanId = id.split('?')[0]
|
|
20
|
+
if (!cleanId.endsWith('.js')) return
|
|
19
21
|
|
|
20
22
|
// 统一路径格式
|
|
21
|
-
const normalizedId =
|
|
23
|
+
const normalizedId = cleanId.replace(/\\/g, '/')
|
|
22
24
|
|
|
23
25
|
// 1. 处理适配器自身的 index.js (按需打包语言包)
|
|
24
26
|
if (languages && (normalizedId.endsWith('monaco-editor-nls-adapter/index.js') || normalizedId.endsWith('monaco-editor-nls-adapter/index.ts'))) {
|
|
@@ -45,7 +47,7 @@ function monacoNlsPlugin(options = {}) {
|
|
|
45
47
|
|
|
46
48
|
// 2. 处理 Monaco Editor 源代码 (注入本地化逻辑)
|
|
47
49
|
if (normalizedId.indexOf('monaco-editor') !== -1 && normalizedId.includes(monacoRoot)) {
|
|
48
|
-
const result = transform(code,
|
|
50
|
+
const result = transform(code, cleanId, options)
|
|
49
51
|
|
|
50
52
|
// 如果 transform 返回的是对象 ({ code, map }),直接返回
|
|
51
53
|
if (result && typeof result === 'object') {
|