monaco-editor-nls-adapter 2.2.2 → 2.3.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monaco-editor-nls-adapter",
3
- "version": "2.2.2",
3
+ "version": "2.3.0",
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",
@@ -13,6 +13,7 @@
13
13
  "require": "./index.js"
14
14
  },
15
15
  "./lite": {
16
+ "types": "./lite.d.ts",
16
17
  "import": "./lite.js",
17
18
  "require": "./lite.js"
18
19
  },
@@ -39,18 +40,25 @@
39
40
  "index.js",
40
41
  "lite.js",
41
42
  "index.d.ts",
43
+ "lite.d.ts",
42
44
  "loader.js",
45
+ "loader.d.ts",
43
46
  "proxy.js",
47
+ "proxy.d.ts",
44
48
  "transform.js",
49
+ "transform.d.ts",
45
50
  "codegen.js",
51
+ "scripts/",
46
52
  "vite-plugin.js",
53
+ "vite-plugin.d.ts",
47
54
  "locales/",
48
55
  "README.md",
49
56
  "README_zh.md"
50
57
  ],
51
58
  "scripts": {
52
- "minify-locales": "for f in locales/*.json; do jq -c . \"$f\" > \"$f.tmp\" && mv \"$f.tmp\" \"$f\"; done",
53
- "prepublishOnly": "npm run minify-locales"
59
+ "minify-locales": "node scripts/minify-locales.js",
60
+ "prepublishOnly": "npm run minify-locales",
61
+ "test": "node tests/simple.test.js && node tests/verify-on-demand.js"
54
62
  },
55
63
  "keywords": [
56
64
  "monaco-editor",
package/proxy.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * NLS 代理对象,由 loader/plugin 智能注入到 Monaco Editor 生命周期中。
3
+ * 兼容 VS Code 和 Monaco 0.50.0+ 的内部签名。
4
+ */
5
+ export interface NLSProxy {
6
+ /**
7
+ * 核心本地化翻译函数 / Core localization function.
8
+ * @param path 模块相对于 esm 的路径 (由 transform 注入) / Module path relative to 'esm'.
9
+ * @param data 翻译标识符 (key) 或包含 key 的配置对象 / Translation key or object.
10
+ * @param defaultMessage 默认回退消息 / Fallback message.
11
+ * @param args 模板插值参数 / Template interpolation arguments.
12
+ */
13
+ localize(path: string, data: any, defaultMessage: string, ...args: any[]): string;
14
+
15
+ /**
16
+ * 同 localize,但返回包含原始文本的对象 / Same as localize, returns object with 'value' and 'original'.
17
+ */
18
+ localize2(path: string, data: any, defaultMessage: string, ...args: any[]): { value: string; original: string };
19
+
20
+ /**
21
+ * 设置当前的语言包字典数据 / Set the current locale dictionary data.
22
+ * @param data 翻译数据字典 / Translation dictionary.
23
+ * @param locale 语言代码名称 / Locale code name (e.g. 'zh-hans').
24
+ */
25
+ setLocaleData(data: Record<string, any>, locale?: string): void;
26
+
27
+ /**
28
+ * 获取当前已加载的完整字典数据 / Get the raw translation data.
29
+ */
30
+ getLocaleData(): Record<string, any> | null;
31
+
32
+ /**
33
+ * 获取当前活跃的语言代码名称 / Get the current active locale name.
34
+ */
35
+ getLocaleName(): string;
36
+
37
+ /** 获取默认配置语言 (通常为已弃用或返回 undefined) */
38
+ getConfiguredDefaultLocale(): string | undefined;
39
+
40
+ /** VS Code 兼容接口:加载消息包 */
41
+ loadMessageBundle(file: string): (path: string, data: any, defaultMessage: string, ...args: any[]) => string;
42
+
43
+ /** VS Code 兼容接口:初始化配置 */
44
+ config(opt: any): (file: string) => any;
45
+
46
+ /**
47
+ * 动态创建一个针对特定 Context 的代理实例 / Create a contextual proxy instance.
48
+ */
49
+ create(key: string, data: any): {
50
+ localize: (idx: any, defaultValue: string, ...args: any[]) => string;
51
+ localize2: (idx: any, defaultValue: string, ...args: any[]) => { value: string; original: string };
52
+ getConfiguredDefaultLocale: () => string | undefined;
53
+ };
54
+ }
55
+
56
+ /**
57
+ * 默认导出的 NLS 代理实例
58
+ */
59
+ declare const proxy: NLSProxy;
60
+ export default proxy;
@@ -0,0 +1,19 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+
4
+ const localesDir = path.join(__dirname, '..', 'locales')
5
+
6
+ function minifyLocaleFile(filePath) {
7
+ const original = fs.readFileSync(filePath, 'utf8')
8
+ const parsed = JSON.parse(original)
9
+ const minified = JSON.stringify(parsed)
10
+
11
+ if (original !== minified) {
12
+ fs.writeFileSync(filePath, minified)
13
+ }
14
+ }
15
+
16
+ for (const entry of fs.readdirSync(localesDir)) {
17
+ if (!entry.endsWith('.json')) continue
18
+ minifyLocaleFile(path.join(localesDir, entry))
19
+ }
package/transform.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { PluginOptions } from './vite-plugin';
2
+
3
+ /**
4
+ * 转换 Monaco Editor 源代码以注入本地化路径。
5
+ * 支持 Webpack Loader 和 Vite Plugin 调用。
6
+ *
7
+ * @param source 源代码内容
8
+ * @param id 文件路径 (resourcePath 或 vite id)
9
+ * @param options 配置项
10
+ * @returns 转换后的对象 { code, map } 或原始字符串
11
+ */
12
+ export function transform(
13
+ source: string,
14
+ id: string,
15
+ options?: PluginOptions
16
+ ): { code: string; map: any } | string;
package/transform.js CHANGED
@@ -13,10 +13,11 @@ const LOCALIZE_REGEX = /nls\.localize(\d?)\(/g
13
13
  function transform(source, id, options = {}) {
14
14
  // 统一路径分隔符
15
15
  const resourcePath = id.replace(/\\/g, '/')
16
+ const hasCustomMonacoPath = typeof options.monacoPath === 'string' && options.monacoPath.length > 0
16
17
  let monacoRoot = options.monacoPath || 'monaco-editor/esm'
17
18
 
18
19
  // 4. 自动路径探测增强:如果是默认路径且在 node_modules 中,尝试自动定位物理路径
19
- if (monacoRoot === 'monaco-editor/esm' && !CACHED_MONACO_ROOT) {
20
+ if (!hasCustomMonacoPath && monacoRoot === 'monaco-editor/esm' && !CACHED_MONACO_ROOT) {
20
21
  try {
21
22
  // 尝试自动定位 monaco-editor 的安装目录 (适配 pnpm, npm, yarn)
22
23
  const pkgPath = require.resolve('monaco-editor/package.json', { paths: [process.cwd()] })
@@ -27,7 +28,7 @@ function transform(source, id, options = {}) {
27
28
  }
28
29
 
29
30
  // 优先匹配物理路径,否则回退到特征字符串匹配
30
- const matchPath = CACHED_MONACO_ROOT || monacoRoot
31
+ const matchPath = hasCustomMonacoPath ? monacoRoot : (CACHED_MONACO_ROOT || monacoRoot)
31
32
 
32
33
  if (resourcePath.includes(matchPath)) {
33
34
  // 计算模块路径 (相对于 esm 目录)
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 为 Vite/Rollup 应用注入 Monaco Editor 本地化路径转换的插件。
3
+ * [Vite/Rollup Plugin] Inject localization path transformations into Monaco Editor.
4
+ */
5
+ declare function monacoNlsPlugin(options?: monacoNlsPlugin.PluginOptions): any;
6
+
7
+ declare namespace monacoNlsPlugin {
8
+ /**
9
+ * 插件配置项 / Plugin configuration options.
10
+ */
11
+ export interface PluginOptions {
12
+ /**
13
+ * 匹配 monaco-editor 源码的目录名或物理路径片段。
14
+ * [Optional] The path fragment to identify Monaco Editor's source directory (e.g. 'monaco-editor/esm').
15
+ * 默认会自动探测 node_modules 下的 monaco-editor/esm,通常无需配置。
16
+ * Matches npm, pnpm, and yarn local paths automatically if using default 'monaco-editor'.
17
+ */
18
+ monacoPath?: string;
19
+
20
+ /**
21
+ * 需要包含在最终产物中的语言包列表。
22
+ * [Optional] List of languages to include in the bundle (e.g. ['zh-hans', 'en']).
23
+ * 如果不指定,则默认包含所有可用的语言包。
24
+ * If not specified, all available language packs will be included.
25
+ */
26
+ languages?: string[];
27
+ }
28
+ }
29
+
30
+ export = monacoNlsPlugin;
package/vite-plugin.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { transform } = require('./transform')
2
- const { generateLocalesCode } = require('./codegen')
2
+ const { replaceLocaleLoaders } = require('./codegen')
3
3
 
4
4
  /**
5
5
  * Vite Plugin for Monaco Editor NLS Adapter
@@ -24,23 +24,8 @@ function monacoNlsPlugin(options = {}) {
24
24
 
25
25
  // 1. 处理适配器自身的 index.js (按需打包语言包)
26
26
  if (languages && (normalizedId.endsWith('monaco-editor-nls-adapter/index.js') || normalizedId.endsWith('monaco-editor-nls-adapter/index.ts'))) {
27
- let newCode = code
28
-
29
- // 替换同步 require
30
- const syncCode = generateLocalesCode(languages, false)
31
- if (syncCode) {
32
- newCode = newCode.replace(/require\(`\.\/locales\/\$\{targetLocale\}\.json`\)/g, syncCode)
33
- }
34
-
35
- // 替换异步 import
36
- const asyncCode = generateLocalesCode(languages, true)
37
- if (asyncCode) {
38
- // 注意:index.js 中的 import 是带注释的: import(/* webpackChunkName: "nls-[request]" */ `./locales/${targetLocale}.json`)
39
- newCode = newCode.replace(/import\(.*?\`\.\/locales\/\$\{targetLocale\}\.json\`\)/g, asyncCode)
40
- }
41
-
42
27
  return {
43
- code: newCode,
28
+ code: replaceLocaleLoaders(code, languages),
44
29
  map: null // 对于这种简单的替换,暂不生成 sourcemap 以保持性能
45
30
  }
46
31
  }