From 2713684d6c5bcacfacf33a48ee8cd282b3e09b0c Mon Sep 17 00:00:00 2001 From: Henning Dieterichs Date: Thu, 22 Aug 2024 13:09:42 +0200 Subject: [PATCH] Makes vs/nls.availableLanguages work for AMD again. --- build/build-monaco-editor.ts | 41 ++++++++++++++++++++++++++++++++++-- build/utils.ts | 20 ++++++++++-------- package-lock.json | 14 ++++++------ package.json | 2 +- src/nls-fix.js | 39 ++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 src/nls-fix.js diff --git a/build/build-monaco-editor.ts b/build/build-monaco-editor.ts index 1a2d3ba08c..c3aa4b5f84 100644 --- a/build/build-monaco-editor.ts +++ b/build/build-monaco-editor.ts @@ -5,7 +5,7 @@ import path = require('path'); import fs = require('fs'); -import { REPO_ROOT, readFiles, writeFiles, IFile } from '../build/utils'; +import { REPO_ROOT, readFiles, writeFiles, IFile, readFile } from '../build/utils'; import { removeDir } from '../build/fs'; import ts = require('typescript'); import { generateMetadata } from './releaseMetadata'; @@ -66,9 +66,10 @@ generateMetadata(); * Release to `dev` or `min`. */ function AMD_releaseOne(type: 'dev' | 'min') { - const coreFiles = readFiles(`node_modules/monaco-editor-core/${type}/**/*`, { + let coreFiles = readFiles(`node_modules/monaco-editor-core/${type}/**/*`, { base: `node_modules/monaco-editor-core/${type}` }); + coreFiles = fixNlsFiles(coreFiles); AMD_addPluginContribs(type, coreFiles); writeFiles(coreFiles, `out/monaco-editor/${type}`); @@ -79,6 +80,33 @@ function AMD_releaseOne(type: 'dev' | 'min') { writeFiles(pluginFiles, `out/monaco-editor/${type}`); } +function fixNlsFiles(files: IFile[]): IFile[] { + return files.map((f) => { + if (!f.path.match(/nls\.messages\.[a-z\-]+\.js/)) { + return f; + } + + const dirName = path.dirname(f.path); + const fileName = path.basename(f.path); + + const newPath = path.join(dirName, 'vs', fileName); + let contentStr = f.contents.toString('utf-8'); + + contentStr = ` +define([], function () { +${contentStr} +}); +`; + + const newContents = Buffer.from(contentStr, 'utf-8'); + + return { + path: newPath, + contents: newContents + }; + }); +} + /** * Edit editor.main.js: * - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main' @@ -96,6 +124,15 @@ function AMD_addPluginContribs(type: 'dev' | 'min', files: IFile[]) { // Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main' contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"'); + // This ensures that old nls-plugin configurations are still respected by the new localization solution. + const contentPrefixSource = readFile('src/nls-fix.js') + .contents.toString('utf-8') + .replace(/\r\n|\n/g, ' '); + + // TODO: Instead of adding this source to the header to maintain the source map indices, it should rewrite the sourcemap! + const searchValue = 'https://github.com/microsoft/vscode/blob/main/LICENSE.txt'; + contents = contents.replace(searchValue, searchValue + ' */ ' + contentPrefixSource + ' /*'); + const pluginFiles = readFiles(`out/languages/bundled/amd-${type}/**/monaco.contribution.js`, { base: `out/languages/bundled/amd-${type}` }); diff --git a/build/utils.ts b/build/utils.ts index 06f056ef67..e69ad6bd45 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -268,16 +268,18 @@ export function readFiles( }); const base = options.base; + return files.map((file) => readFile(file, base)); +} + +export function readFile(file: string, base: string = '') { const baseLength = base === '' ? 0 : base.endsWith('/') ? base.length : base.length + 1; - return files.map((file) => { - const fullPath = path.join(REPO_ROOT, file); - const contents = fs.readFileSync(fullPath); - const relativePath = file.substring(baseLength); - return { - path: relativePath, - contents - }; - }); + const fullPath = path.join(REPO_ROOT, file); + const contents = fs.readFileSync(fullPath); + const relativePath = file.substring(baseLength); + return { + path: relativePath, + contents + }; } export function writeFiles(files: IFile[], dest: string) { diff --git a/package-lock.json b/package-lock.json index a3d016df29..edbc746cf7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,7 +25,7 @@ "jsdom": "^19.0.0", "jsonc-parser": "^3.0.0", "mocha": "^9.2.0", - "monaco-editor-core": "0.51.0-dev-20240725", + "monaco-editor-core": "0.51.0-rc2", "parcel": "^2.7.0", "pin-github-action": "^1.8.0", "playwright": "^1.32.2", @@ -5394,9 +5394,9 @@ "dev": true }, "node_modules/monaco-editor-core": { - "version": "0.51.0-dev-20240725", - "resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.51.0-dev-20240725.tgz", - "integrity": "sha512-N/ukRZDRZ31CmVY9iUgmPnjT9kYXVEFhqc6I9/YWCAV8WpgArQXUB3iaMB0QnmQ08yo3D5XgsoHqf+komUj8nA==", + "version": "0.51.0-rc2", + "resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.51.0-rc2.tgz", + "integrity": "sha512-4mZE6qv75JfKfX7YhQqPJc//k8FONTBzVozlJjIS3mU9em/7jaDPBE3uD+iBTjRNANOk+eih9z/DL5UM8CYxvQ==", "dev": true }, "node_modules/mri": { @@ -11150,9 +11150,9 @@ } }, "monaco-editor-core": { - "version": "0.51.0-dev-20240725", - "resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.51.0-dev-20240725.tgz", - "integrity": "sha512-N/ukRZDRZ31CmVY9iUgmPnjT9kYXVEFhqc6I9/YWCAV8WpgArQXUB3iaMB0QnmQ08yo3D5XgsoHqf+komUj8nA==", + "version": "0.51.0-rc2", + "resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.51.0-rc2.tgz", + "integrity": "sha512-4mZE6qv75JfKfX7YhQqPJc//k8FONTBzVozlJjIS3mU9em/7jaDPBE3uD+iBTjRNANOk+eih9z/DL5UM8CYxvQ==", "dev": true }, "mri": { diff --git a/package.json b/package.json index a14480f13e..44e582225f 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "jsdom": "^19.0.0", "jsonc-parser": "^3.0.0", "mocha": "^9.2.0", - "monaco-editor-core": "0.51.0-dev-20240725", + "monaco-editor-core": "0.51.0-rc2", "parcel": "^2.7.0", "pin-github-action": "^1.8.0", "playwright": "^1.32.2", diff --git a/src/nls-fix.js b/src/nls-fix.js new file mode 100644 index 0000000000..9de52eecb1 --- /dev/null +++ b/src/nls-fix.js @@ -0,0 +1,39 @@ +/* This fix ensures that old nls-plugin configurations are still respected by the new localization solution. */ +/* We should try to avoid this file and find a different solution. */ +/* Warning: This file still has to work when replacing "\n" with " "! */ + +/** + * @type {typeof define} + */ +const globalDefine = globalThis.define; +globalDefine('vs/nls.messages-loader', [], function (...args) { + return { + load: (name, req, load, config) => { + const requestedLanguage = config['vs/nls']?.availableLanguages?.['*']; + if (!requestedLanguage || requestedLanguage === 'en') { + load({}); + } else { + req([`vs/nls.messages.${requestedLanguage}`], () => { + load({}); + }); + } + } + }; +}); +globalDefine( + 'vs/nls.messages', + ['require', 'exports', 'vs/nls.messages-loader!'], + function (require, exports) { + Object.assign(exports, { + getNLSMessages: () => globalThis._VSCODE_NLS_MESSAGES, + getNLSLanguage: () => globalThis._VSCODE_NLS_LANGUAGE + }); + } +); +define = function (...args) { + if (args.length > 0 && args[0] === 'vs/nls.messages') { + return; + } + return globalDefine(...args); +}; +define.amd = true;