Skip to content

Commit

Permalink
Bring the nls loader plugin into our sources (#152338)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Jun 16, 2022
1 parent 5d97179 commit 9db5a36
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 430 deletions.
2 changes: 0 additions & 2 deletions .eslintignore
Expand Up @@ -26,7 +26,5 @@
**/src/vs/*/**/*.d.ts
**/src/vs/base/test/common/filters.perf.data.js
**/src/vs/loader.js
**/src/vs/nls.build.js
**/src/vs/nls.js
**/test/unit/assert.js
**/typings/**
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -576,7 +576,7 @@
"restrictions": []
},
{
"target": "src/vs/{loader.d.ts,css.ts,css.build.ts,monaco.d.ts,nls.d.ts,nls.mock.ts}",
"target": "src/vs/{loader.d.ts,css.ts,css.build.ts,monaco.d.ts,nls.ts,nls.build.ts,nls.mock.ts}",
"restrictions": []
},
{
Expand Down
2 changes: 0 additions & 2 deletions build/filters.js
Expand Up @@ -66,8 +66,6 @@ module.exports.indentationFilter = [
'!**/LICENSE.{txt,rtf}',
'!LICENSES.chromium.html',
'!**/LICENSE',
'!src/vs/nls.js',
'!src/vs/nls.build.js',
'!src/vs/loader.js',
'!src/vs/base/browser/dompurify/*',
'!src/vs/base/common/marked/marked.js',
Expand Down
12 changes: 6 additions & 6 deletions build/gulpfile.editor.js
Expand Up @@ -29,14 +29,17 @@ const editorEntryPoints = [
name: 'vs/editor/editor.main',
include: [],
exclude: ['vs/css', 'vs/nls'],
prepend: ['out-editor-build/vs/css.js', 'out-editor-build/vs/nls.js'],
prepend: [
{ path: 'out-editor-build/vs/css.js' },
{ path: 'out-editor-build/vs/nls.js', amdModuleId: 'vs/nls' }
],
},
{
name: 'vs/base/common/worker/simpleWorker',
include: ['vs/editor/common/services/editorSimpleWorker'],
exclude: ['vs/nls'],
prepend: ['vs/loader.js'],
append: ['vs/base/worker/workerMain'],
prepend: [{ path: 'vs/loader.js' }],
append: [{ path: 'vs/base/worker/workerMain' }],
dest: 'vs/base/worker/workerMain.js'
}
];
Expand Down Expand Up @@ -110,9 +113,6 @@ const createESMSourcesAndResourcesTask = task.define('extract-editor-esm', () =>
'inlineEntryPoint:0.ts',
'inlineEntryPoint:1.ts',
'vs/loader.js',
'vs/nls.ts',
'vs/nls.build.js',
'vs/nls.d.ts',
'vs/base/worker/workerMain.ts',
],
renames: {
Expand Down
20 changes: 13 additions & 7 deletions build/lib/bundle.js
Expand Up @@ -42,14 +42,17 @@ function bundle(entryPoints, config, callback) {
if (!config.paths['vs/css']) {
config.paths['vs/css'] = 'out-build/vs/css.build';
}
config.buildForceInvokeFactory = config.buildForceInvokeFactory || {};
config.buildForceInvokeFactory['vs/nls'] = true;
config.buildForceInvokeFactory['vs/css'] = true;
loader.config(config);
loader(['require'], (localRequire) => {
const resolvePath = (path) => {
const r = localRequire.toUrl(path);
const resolvePath = (entry) => {
const r = localRequire.toUrl(entry.path);
if (!/\.js/.test(r)) {
return r + '.js';
return { path: r + '.js', amdModuleId: entry.amdModuleId };
}
return r;
return { path: r, amdModuleId: entry.amdModuleId };
};
for (const moduleId in entryPointsMap) {
const entryPoint = entryPointsMap[moduleId];
Expand Down Expand Up @@ -330,10 +333,13 @@ function emitEntryPoint(modulesMap, deps, entryPoint, includedModules, prepend,
plugin.writeFile(pluginName, entryPoint, req, write, {});
}
});
const toIFile = (path) => {
const contents = readFileAndRemoveBOM(path);
const toIFile = (entry) => {
let contents = readFileAndRemoveBOM(entry.path);
if (entry.amdModuleId) {
contents = contents.replace(/^define\(/m, `define("${entry.amdModuleId}",`);
}
return {
path: path,
path: entry.path,
contents: contents
};
};
Expand Down
40 changes: 29 additions & 11 deletions build/lib/bundle.ts
Expand Up @@ -42,12 +42,17 @@ interface ILoaderPluginReqFunc {
toUrl(something: string): string;
}

export interface IExtraFile {
path: string;
amdModuleId?: string;
}

export interface IEntryPoint {
name: string;
include?: string[];
exclude?: string[];
prepend?: string[];
append?: string[];
prepend?: IExtraFile[];
append?: IExtraFile[];
dest?: string;
}

Expand Down Expand Up @@ -92,6 +97,13 @@ interface IPartialBundleResult {
export interface ILoaderConfig {
isBuild?: boolean;
paths?: { [path: string]: any };
/*
* Normally, during a build, no module factories are invoked. This can be used
* to forcefully execute a module's factory.
*/
buildForceInvokeFactory: {
[moduleId: string]: boolean;
};
}

/**
Expand Down Expand Up @@ -132,15 +144,18 @@ export function bundle(entryPoints: IEntryPoint[], config: ILoaderConfig, callba
if (!config.paths['vs/css']) {
config.paths['vs/css'] = 'out-build/vs/css.build';
}
config.buildForceInvokeFactory = config.buildForceInvokeFactory || {};
config.buildForceInvokeFactory['vs/nls'] = true;
config.buildForceInvokeFactory['vs/css'] = true;
loader.config(config);

loader(['require'], (localRequire: any) => {
const resolvePath = (path: string) => {
const r = localRequire.toUrl(path);
const resolvePath = (entry: IExtraFile) => {
const r = localRequire.toUrl(entry.path);
if (!/\.js/.test(r)) {
return r + '.js';
return { path: r + '.js', amdModuleId: entry.amdModuleId };
}
return r;
return { path: r, amdModuleId: entry.amdModuleId };
};
for (const moduleId in entryPointsMap) {
const entryPoint = entryPointsMap[moduleId];
Expand Down Expand Up @@ -403,8 +418,8 @@ function emitEntryPoint(
deps: IGraph,
entryPoint: string,
includedModules: string[],
prepend: string[],
append: string[],
prepend: IExtraFile[],
append: IExtraFile[],
dest: string | undefined
): IEmitEntryPointResult {
if (!dest) {
Expand Down Expand Up @@ -478,10 +493,13 @@ function emitEntryPoint(
}
});

const toIFile = (path: string): IFile => {
const contents = readFileAndRemoveBOM(path);
const toIFile = (entry: IExtraFile): IFile => {
let contents = readFileAndRemoveBOM(entry.path);
if (entry.amdModuleId) {
contents = contents.replace(/^define\(/m, `define("${entry.amdModuleId}",`);
}
return {
path: path,
path: entry.path,
contents: contents
};
};
Expand Down
24 changes: 15 additions & 9 deletions build/lib/optimize.js
Expand Up @@ -35,19 +35,25 @@ function loaderConfig() {
}
exports.loaderConfig = loaderConfig;
const IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i;
function loaderPlugin(src, base, amdModuleId) {
return (gulp
.src(src, { base })
.pipe(es.through(function (data) {
if (amdModuleId) {
let contents = data.contents.toString('utf8');
contents = contents.replace(/^define\(/m, `define("${amdModuleId}",`);
data.contents = Buffer.from(contents);
}
this.emit('data', data);
})));
}
function loader(src, bundledFileHeader, bundleLoader, externalLoaderInfo) {
let sources = [
`${src}/vs/loader.js`
];
let loaderStream = gulp.src(`${src}/vs/loader.js`, { base: `${src}` });
if (bundleLoader) {
sources = sources.concat([
`${src}/vs/css.js`,
`${src}/vs/nls.js`
]);
loaderStream = es.merge(loaderStream, loaderPlugin(`${src}/vs/css.js`, `${src}`, 'vs/css'), loaderPlugin(`${src}/vs/nls.js`, `${src}`, 'vs/nls'));
}
let isFirst = true;
return (gulp
.src(sources, { base: `${src}` })
return (loaderStream
.pipe(es.through(function (data) {
if (isFirst) {
isFirst = false;
Expand Down
31 changes: 22 additions & 9 deletions build/lib/optimize.ts
Expand Up @@ -41,21 +41,34 @@ export function loaderConfig() {

const IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i;

function loaderPlugin(src: string, base: string, amdModuleId: string | undefined): NodeJS.ReadWriteStream {
return (
gulp
.src(src, { base })
.pipe(es.through(function (data: VinylFile) {
if (amdModuleId) {
let contents = data.contents.toString('utf8');
contents = contents.replace(/^define\(/m, `define("${amdModuleId}",`);
data.contents = Buffer.from(contents);
}
this.emit('data', data);
}))
);
}

function loader(src: string, bundledFileHeader: string, bundleLoader: boolean, externalLoaderInfo?: any): NodeJS.ReadWriteStream {
let sources = [
`${src}/vs/loader.js`
];
let loaderStream = gulp.src(`${src}/vs/loader.js`, { base: `${src}` });
if (bundleLoader) {
sources = sources.concat([
`${src}/vs/css.js`,
`${src}/vs/nls.js`
]);
loaderStream = es.merge(
loaderStream,
loaderPlugin(`${src}/vs/css.js`, `${src}`, 'vs/css'),
loaderPlugin(`${src}/vs/nls.js`, `${src}`, 'vs/nls'),
);
}

let isFirst = true;
return (
gulp
.src(sources, { base: `${src}` })
loaderStream
.pipe(es.through(function (data) {
if (isFirst) {
isFirst = false;
Expand Down
5 changes: 2 additions & 3 deletions build/lib/standalone.js
Expand Up @@ -106,9 +106,8 @@ function extractEditor(options) {
'vs/css.ts',
'vs/loader.js',
'vs/loader.d.ts',
'vs/nls.build.js',
'vs/nls.d.ts',
'vs/nls.js',
'vs/nls.build.ts',
'vs/nls.ts',
'vs/nls.mock.ts',
].forEach(copyFile);
}
Expand Down
5 changes: 2 additions & 3 deletions build/lib/standalone.ts
Expand Up @@ -119,9 +119,8 @@ export function extractEditor(options: tss.ITreeShakingOptions & { destRoot: str
'vs/css.ts',
'vs/loader.js',
'vs/loader.d.ts',
'vs/nls.build.js',
'vs/nls.d.ts',
'vs/nls.js',
'vs/nls.build.ts',
'vs/nls.ts',
'vs/nls.mock.ts',
].forEach(copyFile);
}
Expand Down
7 changes: 5 additions & 2 deletions src/buildfile.js
Expand Up @@ -33,8 +33,11 @@ exports.base = [
name: 'vs/editor/common/services/editorSimpleWorker',
include: ['vs/base/common/worker/simpleWorker'],
exclude: ['vs/nls'],
prepend: ['vs/loader.js', 'vs/nls.js'],
append: ['vs/base/worker/workerMain'],
prepend: [
{ path: 'vs/loader.js' },
{ path: 'vs/nls.js', amdModuleId: 'vs/nls' }
],
append: [{ path: 'vs/base/worker/workerMain' }],
dest: 'vs/base/worker/workerMain.js'
},
{
Expand Down
14 changes: 13 additions & 1 deletion src/vs/loader.js
Expand Up @@ -291,6 +291,9 @@ var AMDLoader;
if (typeof options.isBuild !== 'boolean') {
options.isBuild = false;
}
if (typeof options.buildForceInvokeFactory !== 'object') {
options.buildForceInvokeFactory = {};
}
if (typeof options.paths !== 'object') {
options.paths = {};
}
Expand Down Expand Up @@ -536,6 +539,15 @@ var AMDLoader;
Configuration.prototype.isBuild = function () {
return this.options.isBuild;
};
Configuration.prototype.shouldInvokeFactory = function (strModuleId) {
if (!this.options.isBuild) {
// outside of a build, all factories should be invoked
return true;
}
// during a build, only explicitly marked or anonymous modules get their factories invoked
return (this.options.buildForceInvokeFactory[strModuleId]
|| AMDLoader.Utilities.isAnonymousModule(strModuleId));
};
/**
* Test if module `moduleId` is expected to be defined multiple times
*/
Expand Down Expand Up @@ -1171,7 +1183,7 @@ var AMDLoader;
}
};
Module._invokeFactory = function (config, strModuleId, callback, dependenciesValues) {
if (config.isBuild() && !AMDLoader.Utilities.isAnonymousModule(strModuleId)) {
if (!config.shouldInvokeFactory(strModuleId)) {
return {
returnedValue: null,
producedError: null
Expand Down

0 comments on commit 9db5a36

Please sign in to comment.