Skip to content

Commit 72ef20b

Browse files
committed
Improve monaco-editor-core bundling
1 parent fa48339 commit 72ef20b

7 files changed

Lines changed: 121 additions & 22 deletions

File tree

build/gulpfile.common.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ exports.loaderConfig = function (emptyPaths) {
3939

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

42-
function loader(bundledFileHeader) {
42+
function loader(bundledFileHeader, bundleLoader) {
43+
let sources = [
44+
'out-build/vs/loader.js'
45+
];
46+
if (bundleLoader) {
47+
sources = sources.concat([
48+
'out-build/vs/css.js',
49+
'out-build/vs/nls.js'
50+
]);
51+
}
52+
4353
let isFirst = true;
44-
return gulp.src([
45-
'out-build/vs/loader.js',
46-
'out-build/vs/css.js',
47-
'out-build/vs/nls.js'
48-
], { base: 'out-build' })
54+
return (
55+
gulp
56+
.src(sources, { base: 'out-build' })
4957
.pipe(es.through(function(data) {
5058
if (isFirst) {
5159
isFirst = false;
@@ -64,7 +72,8 @@ function loader(bundledFileHeader) {
6472
.pipe(es.mapSync(function (f) {
6573
f.sourceMap.sourceRoot = util.toFileUri(path.join(path.dirname(__dirname), 'src'));
6674
return f;
67-
}));
75+
}))
76+
);
6877
}
6978

7079
function toConcatStream(bundledFileHeader, sources, dest) {
@@ -116,6 +125,7 @@ function toBundleStream(bundledFileHeader, bundles) {
116125
* - otherSources (for non-AMD files that should get Copyright treatment)
117126
* - resources (svg, etc.)
118127
* - loaderConfig
128+
* - bundleLoader (boolean - true by default - append css and nls to loader)
119129
* - header (basically the Copyright treatment)
120130
* - bundleInfo (boolean - emit bundleInfo.json file)
121131
* - out (out folder name)
@@ -126,6 +136,7 @@ exports.optimizeTask = function(opts) {
126136
const resources = opts.resources;
127137
const loaderConfig = opts.loaderConfig;
128138
const bundledFileHeader = opts.header;
139+
const bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader);
129140
const out = opts.out;
130141

131142
return function() {
@@ -174,7 +185,7 @@ exports.optimizeTask = function(opts) {
174185
}));
175186

176187
const result = es.merge(
177-
loader(bundledFileHeader),
188+
loader(bundledFileHeader, bundleLoader),
178189
bundlesStream,
179190
otherSourcesStream,
180191
resourcesStream,

build/gulpfile.editor.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ var editorEntryPoints = [
2222
{
2323
name: 'vs/editor/editor.main',
2424
include: [],
25-
exclude: [ 'vs/css', 'vs/nls' ]
25+
exclude: [],
26+
prepend: [ 'vs/css.js', 'vs/nls.js' ],
2627
},
2728
{
2829
name: 'vs/base/common/worker/simpleWorker',
2930
include: [ 'vs/editor/common/services/editorSimpleWorker' ],
30-
exclude: [ 'vs/css', 'vs/nls' ]
31+
prepend: [ 'vs/loader.js' ],
32+
append: [ 'vs/base/worker/workerMain' ],
33+
dest: 'vs/base/worker/workerMain.js'
3134
},
3235
]
3336

@@ -37,14 +40,11 @@ var editorResources = [
3740
'!out-build/vs/base/browser/ui/toolbar/**/*',
3841
'!out-build/vs/base/browser/ui/octiconLabel/**/*',
3942
'!out-build/vs/editor/contrib/defineKeybinding/**/*',
40-
'out-build/vs/base/worker/workerMain.{js,js.map}',
4143
'!out-build/vs/workbench/**',
4244
'!**/test/**'
4345
];
4446

4547
var editorOtherSources = [
46-
'out-build/vs/css.js',
47-
'out-build/vs/nls.js'
4848
];
4949

5050
var BUNDLED_FILE_HEADER = [
@@ -75,6 +75,7 @@ gulp.task('optimize-editor', ['clean-optimized-editor', 'compile-build'], common
7575
otherSources: editorOtherSources,
7676
resources: editorResources,
7777
loaderConfig: editorLoaderConfig(),
78+
bundleLoader: false,
7879
header: BUNDLED_FILE_HEADER,
7980
bundleInfo: true,
8081
out: 'out-editor'

build/lib/bundle.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ function bundle(entryPoints, config, callback) {
3131
var loader = loaderModule.exports;
3232
config.isBuild = true;
3333
loader.config(config);
34+
loader(['require'], function (localRequire) {
35+
var resolvePath = function (path) {
36+
var r = localRequire.toUrl(path);
37+
if (!/\.js/.test(r)) {
38+
return r + '.js';
39+
}
40+
return r;
41+
};
42+
for (var moduleId in entryPointsMap) {
43+
var entryPoint = entryPointsMap[moduleId];
44+
if (entryPoint.append) {
45+
entryPoint.append = entryPoint.append.map(resolvePath);
46+
}
47+
if (entryPoint.prepend) {
48+
entryPoint.prepend = entryPoint.prepend.map(resolvePath);
49+
}
50+
}
51+
});
3452
loader(Object.keys(allMentionedModulesMap), function () {
3553
var modules = loader.getBuildInfo();
3654
var partialResult = emitEntryPoints(modules, entryPointsMap);
@@ -74,7 +92,7 @@ function emitEntryPoints(modules, entryPoints) {
7492
return allDependencies[module];
7593
});
7694
bundleData.bundles[moduleToBundle] = includedModules;
77-
var res = emitEntryPoint(modulesMap, modulesGraph, moduleToBundle, includedModules);
95+
var res = emitEntryPoint(modulesMap, modulesGraph, moduleToBundle, includedModules, info.prepend, info.append, info.dest);
7896
result = result.concat(res.files);
7997
for (var pluginName in res.usedPlugins) {
8098
usedPlugins[pluginName] = usedPlugins[pluginName] || res.usedPlugins[pluginName];
@@ -235,10 +253,13 @@ function removeDuplicateTSBoilerplate(destFiles) {
235253
});
236254
return destFiles;
237255
}
238-
function emitEntryPoint(modulesMap, deps, entryPoint, includedModules) {
256+
function emitEntryPoint(modulesMap, deps, entryPoint, includedModules, prepend, append, dest) {
257+
if (!dest) {
258+
dest = entryPoint + '.js';
259+
}
239260
var mainResult = {
240261
sources: [],
241-
dest: entryPoint + '.js'
262+
dest: dest
242263
}, results = [mainResult];
243264
var usedPlugins = {};
244265
var getLoaderPlugin = function (pluginName) {
@@ -286,6 +307,16 @@ function emitEntryPoint(modulesMap, deps, entryPoint, includedModules) {
286307
plugin.writeFile(pluginName, entryPoint, req, write, {});
287308
}
288309
});
310+
var toIFile = function (path) {
311+
var contents = readFileAndRemoveBOM(path);
312+
return {
313+
path: path,
314+
contents: contents
315+
};
316+
};
317+
var toPrepend = (prepend || []).map(toIFile);
318+
var toAppend = (append || []).map(toIFile);
319+
mainResult.sources = toPrepend.concat(mainResult.sources).concat(toAppend);
289320
return {
290321
files: results,
291322
usedPlugins: usedPlugins

build/lib/bundle.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export interface IEntryPoint {
4646
name: string;
4747
include: string[];
4848
exclude: string[];
49+
prepend: string[];
50+
append: string[];
51+
dest: string;
4952
}
5053

5154
interface IEntryPointMap {
@@ -120,6 +123,25 @@ export function bundle(entryPoints:IEntryPoint[], config:ILoaderConfig, callback
120123
config.isBuild = true;
121124
loader.config(config);
122125

126+
loader(['require'], (localRequire) => {
127+
let resolvePath = (path:string) => {
128+
let r = localRequire.toUrl(path);
129+
if (!/\.js/.test(r)) {
130+
return r + '.js';
131+
}
132+
return r;
133+
};
134+
for (let moduleId in entryPointsMap) {
135+
let entryPoint = entryPointsMap[moduleId];
136+
if (entryPoint.append) {
137+
entryPoint.append = entryPoint.append.map(resolvePath);
138+
}
139+
if (entryPoint.prepend) {
140+
entryPoint.prepend = entryPoint.prepend.map(resolvePath);
141+
}
142+
}
143+
});
144+
123145
loader(Object.keys(allMentionedModulesMap), () => {
124146
let modules = <IBuildModuleInfo[]>loader.getBuildInfo();
125147
let partialResult = emitEntryPoints(modules, entryPointsMap);
@@ -171,7 +193,15 @@ function emitEntryPoints(modules:IBuildModuleInfo[], entryPoints:IEntryPointMap)
171193

172194
bundleData.bundles[moduleToBundle] = includedModules;
173195

174-
let res = emitEntryPoint(modulesMap, modulesGraph, moduleToBundle, includedModules);
196+
let res = emitEntryPoint(
197+
modulesMap,
198+
modulesGraph,
199+
moduleToBundle,
200+
includedModules,
201+
info.prepend,
202+
info.append,
203+
info.dest
204+
);
175205

176206
result = result.concat(res.files);
177207
for (let pluginName in res.usedPlugins) {
@@ -355,10 +385,21 @@ interface IEmitEntryPointResult {
355385
usedPlugins: IPluginMap;
356386
}
357387

358-
function emitEntryPoint(modulesMap:IBuildModuleInfoMap, deps:IGraph, entryPoint:string, includedModules:string[]): IEmitEntryPointResult {
388+
function emitEntryPoint(
389+
modulesMap:IBuildModuleInfoMap,
390+
deps:IGraph,
391+
entryPoint:string,
392+
includedModules:string[],
393+
prepend: string[],
394+
append: string[],
395+
dest: string
396+
): IEmitEntryPointResult {
397+
if (!dest) {
398+
dest = entryPoint + '.js';
399+
}
359400
let mainResult: IConcatFile = {
360401
sources: [],
361-
dest: entryPoint + '.js'
402+
dest: dest
362403
},
363404
results: IConcatFile[] = [mainResult];
364405

@@ -416,6 +457,19 @@ function emitEntryPoint(modulesMap:IBuildModuleInfoMap, deps:IGraph, entryPoint:
416457
}
417458
});
418459

460+
let toIFile = (path):IFile => {
461+
let contents = readFileAndRemoveBOM(path);
462+
return {
463+
path: path,
464+
contents: contents
465+
};
466+
};
467+
468+
let toPrepend = (prepend||[]).map(toIFile);
469+
let toAppend = (append||[]).map(toIFile);
470+
471+
mainResult.sources = toPrepend.concat(mainResult.sources).concat(toAppend);
472+
419473
return {
420474
files: results,
421475
usedPlugins: usedPlugins

build/monaco/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "monaco-editor-core",
33
"private": true,
4-
"version": "0.7.0-next.5",
4+
"version": "0.7.1",
55
"description": "A browser based code editor",
66
"author": "Microsoft Corporation",
77
"license": "MIT",

src/vs/base/worker/workerMain.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
let MonacoEnvironment = (<any>self).MonacoEnvironment;
1010
let monacoBaseUrl = MonacoEnvironment && MonacoEnvironment.baseUrl ? MonacoEnvironment.baseUrl : '../../../';
1111

12-
importScripts(monacoBaseUrl + 'vs/loader.js');
12+
if (typeof (<any>self).define !== 'function' || !(<any>self).define.amd) {
13+
importScripts(monacoBaseUrl + 'vs/loader.js');
14+
}
1315

1416
require.config({
1517
baseUrl: monacoBaseUrl,

src/vs/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,7 @@ var AMDLoader;
19261926
RequireFunc.config(global.require);
19271927
}
19281928
if (!isElectronRenderer) {
1929-
global.define = DefineFunc;
1929+
global.define = define = DefineFunc;
19301930
}
19311931
else {
19321932
define = function () {

0 commit comments

Comments
 (0)