Skip to content

Commit 2713684

Browse files
committed
Makes vs/nls.availableLanguages work for AMD again.
1 parent f618747 commit 2713684

File tree

5 files changed

+97
-19
lines changed

5 files changed

+97
-19
lines changed

build/build-monaco-editor.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import path = require('path');
77
import fs = require('fs');
8-
import { REPO_ROOT, readFiles, writeFiles, IFile } from '../build/utils';
8+
import { REPO_ROOT, readFiles, writeFiles, IFile, readFile } from '../build/utils';
99
import { removeDir } from '../build/fs';
1010
import ts = require('typescript');
1111
import { generateMetadata } from './releaseMetadata';
@@ -66,9 +66,10 @@ generateMetadata();
6666
* Release to `dev` or `min`.
6767
*/
6868
function AMD_releaseOne(type: 'dev' | 'min') {
69-
const coreFiles = readFiles(`node_modules/monaco-editor-core/${type}/**/*`, {
69+
let coreFiles = readFiles(`node_modules/monaco-editor-core/${type}/**/*`, {
7070
base: `node_modules/monaco-editor-core/${type}`
7171
});
72+
coreFiles = fixNlsFiles(coreFiles);
7273
AMD_addPluginContribs(type, coreFiles);
7374
writeFiles(coreFiles, `out/monaco-editor/${type}`);
7475

@@ -79,6 +80,33 @@ function AMD_releaseOne(type: 'dev' | 'min') {
7980
writeFiles(pluginFiles, `out/monaco-editor/${type}`);
8081
}
8182

83+
function fixNlsFiles(files: IFile[]): IFile[] {
84+
return files.map((f) => {
85+
if (!f.path.match(/nls\.messages\.[a-z\-]+\.js/)) {
86+
return f;
87+
}
88+
89+
const dirName = path.dirname(f.path);
90+
const fileName = path.basename(f.path);
91+
92+
const newPath = path.join(dirName, 'vs', fileName);
93+
let contentStr = f.contents.toString('utf-8');
94+
95+
contentStr = `
96+
define([], function () {
97+
${contentStr}
98+
});
99+
`;
100+
101+
const newContents = Buffer.from(contentStr, 'utf-8');
102+
103+
return {
104+
path: newPath,
105+
contents: newContents
106+
};
107+
});
108+
}
109+
82110
/**
83111
* Edit editor.main.js:
84112
* - 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[]) {
96124
// Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
97125
contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"');
98126

127+
// This ensures that old nls-plugin configurations are still respected by the new localization solution.
128+
const contentPrefixSource = readFile('src/nls-fix.js')
129+
.contents.toString('utf-8')
130+
.replace(/\r\n|\n/g, ' ');
131+
132+
// TODO: Instead of adding this source to the header to maintain the source map indices, it should rewrite the sourcemap!
133+
const searchValue = 'https://github.com/microsoft/vscode/blob/main/LICENSE.txt';
134+
contents = contents.replace(searchValue, searchValue + ' */ ' + contentPrefixSource + ' /*');
135+
99136
const pluginFiles = readFiles(`out/languages/bundled/amd-${type}/**/monaco.contribution.js`, {
100137
base: `out/languages/bundled/amd-${type}`
101138
});

build/utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,18 @@ export function readFiles(
268268
});
269269

270270
const base = options.base;
271+
return files.map((file) => readFile(file, base));
272+
}
273+
274+
export function readFile(file: string, base: string = '') {
271275
const baseLength = base === '' ? 0 : base.endsWith('/') ? base.length : base.length + 1;
272-
return files.map((file) => {
273-
const fullPath = path.join(REPO_ROOT, file);
274-
const contents = fs.readFileSync(fullPath);
275-
const relativePath = file.substring(baseLength);
276-
return {
277-
path: relativePath,
278-
contents
279-
};
280-
});
276+
const fullPath = path.join(REPO_ROOT, file);
277+
const contents = fs.readFileSync(fullPath);
278+
const relativePath = file.substring(baseLength);
279+
return {
280+
path: relativePath,
281+
contents
282+
};
281283
}
282284

283285
export function writeFiles(files: IFile[], dest: string) {

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"jsdom": "^19.0.0",
5353
"jsonc-parser": "^3.0.0",
5454
"mocha": "^9.2.0",
55-
"monaco-editor-core": "0.51.0-dev-20240725",
55+
"monaco-editor-core": "0.51.0-rc2",
5656
"parcel": "^2.7.0",
5757
"pin-github-action": "^1.8.0",
5858
"playwright": "^1.32.2",

src/nls-fix.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* This fix ensures that old nls-plugin configurations are still respected by the new localization solution. */
2+
/* We should try to avoid this file and find a different solution. */
3+
/* Warning: This file still has to work when replacing "\n" with " "! */
4+
5+
/**
6+
* @type {typeof define}
7+
*/
8+
const globalDefine = globalThis.define;
9+
globalDefine('vs/nls.messages-loader', [], function (...args) {
10+
return {
11+
load: (name, req, load, config) => {
12+
const requestedLanguage = config['vs/nls']?.availableLanguages?.['*'];
13+
if (!requestedLanguage || requestedLanguage === 'en') {
14+
load({});
15+
} else {
16+
req([`vs/nls.messages.${requestedLanguage}`], () => {
17+
load({});
18+
});
19+
}
20+
}
21+
};
22+
});
23+
globalDefine(
24+
'vs/nls.messages',
25+
['require', 'exports', 'vs/nls.messages-loader!'],
26+
function (require, exports) {
27+
Object.assign(exports, {
28+
getNLSMessages: () => globalThis._VSCODE_NLS_MESSAGES,
29+
getNLSLanguage: () => globalThis._VSCODE_NLS_LANGUAGE
30+
});
31+
}
32+
);
33+
define = function (...args) {
34+
if (args.length > 0 && args[0] === 'vs/nls.messages') {
35+
return;
36+
}
37+
return globalDefine(...args);
38+
};
39+
define.amd = true;

0 commit comments

Comments
 (0)