gulpfile.editor.js 11.1 KB
Newer Older
I
isidor 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

A
Alex Dima 已提交
6 7 8 9 10 11 12 13 14
const gulp = require('gulp');
const path = require('path');
const util = require('./lib/util');
const common = require('./lib/optimize');
const es = require('event-stream');
const File = require('vinyl');
const i18n = require('./lib/i18n');
const standalone = require('./lib/standalone');
const cp = require('child_process');
A
Alex Dima 已提交
15
const compilation = require('./lib/compilation');
A
Alex Dima 已提交
16 17
const monacoapi = require('./monaco/api');
const fs = require('fs');
I
isidor 已提交
18 19

var root = path.dirname(__dirname);
A
Alex Dima 已提交
20 21 22
var sha1 = util.getVersion(root);
var semver = require('./monaco/package.json').version;
var headerVersion = semver + '(' + sha1 + ')';
I
isidor 已提交
23 24 25

// Build

26 27 28 29
var editorEntryPoints = [
	{
		name: 'vs/editor/editor.main',
		include: [],
J
Joao Moreno 已提交
30
		exclude: ['vs/css', 'vs/nls'],
A
Alex Dima 已提交
31
		prepend: ['out-editor-build/vs/css.js', 'out-editor-build/vs/nls.js'],
32 33 34
	},
	{
		name: 'vs/base/common/worker/simpleWorker',
J
Joao Moreno 已提交
35 36 37
		include: ['vs/editor/common/services/editorSimpleWorker'],
		prepend: ['vs/loader.js'],
		append: ['vs/base/worker/workerMain'],
A
Alex Dima 已提交
38
		dest: 'vs/base/worker/workerMain.js'
39
	}
A
Alex Dima 已提交
40
];
I
isidor 已提交
41 42 43

var editorResources = [
	'out-build/vs/{base,editor}/**/*.{svg,png}',
44 45 46
	'!out-build/vs/base/browser/ui/splitview/**/*',
	'!out-build/vs/base/browser/ui/toolbar/**/*',
	'!out-build/vs/base/browser/ui/octiconLabel/**/*',
I
isidor 已提交
47 48 49 50 51 52
	'!out-build/vs/workbench/**',
	'!**/test/**'
];

var BUNDLED_FILE_HEADER = [
	'/*!-----------------------------------------------------------',
A
Alex Dima 已提交
53
	' * Copyright (c) Microsoft Corporation. All rights reserved.',
A
Alex Dima 已提交
54
	' * Version: ' + headerVersion,
I
isidor 已提交
55 56 57 58 59 60
	' * Released under the MIT license',
	' * https://github.com/Microsoft/vscode/blob/master/LICENSE.txt',
	' *-----------------------------------------------------------*/',
	''
].join('\n');

A
Alex Dima 已提交
61 62
const languages = i18n.defaultLanguages.concat([]);  // i18n.defaultLanguages.concat(process.env.VSCODE_QUALITY !== 'stable' ? i18n.extraLanguages : []);

63
const extractEditorSrcTask = function () {
A
Alex Dima 已提交
64 65 66
	console.log(`If the build fails, consider tweaking shakeLevel below to a lower value.`);
	const apiusages = monacoapi.execute().usageContent;
	const extrausages = fs.readFileSync(path.join(root, 'build', 'monaco', 'monaco.usage.recipe')).toString();
67 68 69 70 71
	standalone.extractEditor({
		sourcesRoot: path.join(root, 'src'),
		entryPoints: [
			'vs/editor/editor.main',
			'vs/editor/editor.worker',
72
			'vs/base/worker/workerMain',
A
Alex Dima 已提交
73 74 75 76
		],
		inlineEntryPoints: [
			apiusages,
			extrausages
77
		],
A
Alex Dima 已提交
78 79 80 81 82
		typings: [
			'typings/lib.ie11_safe_es6.d.ts',
			'typings/thenable.d.ts',
			'typings/es6-promise.d.ts',
			'typings/require-monaco.d.ts',
83
			"typings/lib.es2018.promise.d.ts",
A
Alex Dima 已提交
84 85
			'vs/monaco.d.ts'
		],
86
		libs: [
A
Alex Dima 已提交
87 88 89
			`lib.es5.d.ts`,
			`lib.dom.d.ts`,
			`lib.webworker.importscripts.d.ts`
90 91 92 93
		],
		redirects: {
			'vs/base/browser/ui/octiconLabel/octiconLabel': 'vs/base/browser/ui/octiconLabel/octiconLabel.mock',
		},
A
Alex Dima 已提交
94
		shakeLevel: 2, // 0-Files, 1-InnerFile, 2-ClassMembers
A
Alex Dima 已提交
95
		importIgnorePattern: /(^vs\/css!)|(promise-polyfill\/polyfill)/,
96 97
		destRoot: path.join(root, 'out-editor-src')
	});
98 99
};
extractEditorSrcTask.displayName = 'extract-editor-src';
100

101 102
const compileEditorAMDTask = compilation.compileTask('out-editor-src', 'out-editor-build', true);
compileEditorAMDTask.displayName = 'compile-editor-amd';
A
Alex Dima 已提交
103

104
const optimizeEditorAMDTask = common.optimizeTask({
105 106 107 108 109 110
	src: 'out-editor-build',
	entryPoints: editorEntryPoints,
	resources: editorResources,
	loaderConfig: {
		paths: {
			'vs': 'out-editor-build/vs',
A
Alex Dima 已提交
111 112
			'vs/css': 'out-editor-build/vs/css.build',
			'vs/nls': 'out-editor-build/vs/nls.build',
113 114 115 116 117 118 119 120
			'vscode': 'empty:'
		}
	},
	bundleLoader: false,
	header: BUNDLED_FILE_HEADER,
	bundleInfo: true,
	out: 'out-editor',
	languages: languages
121 122
});
optimizeEditorAMDTask.displayName = 'optimize-editor-amd';
123

124 125
const minifyEditorAMDTask = common.minifyTask('out-editor');
minifyEditorAMDTask.displayName = 'minify-editor-amd';
A
Alex Dima 已提交
126

127
const createESMSourcesAndResourcesTask = function () {
128 129
	standalone.createESMSourcesAndResources2({
		srcFolder: './out-editor-src',
A
Alex Dima 已提交
130
		outFolder: './out-editor-esm',
A
Alex Dima 已提交
131
		outResourcesFolder: './out-monaco-editor-core/esm',
132 133 134
		ignores: [
			'inlineEntryPoint:0.ts',
			'inlineEntryPoint:1.ts',
135
			'vs/loader.js',
136
			'vs/nls.ts',
137
			'vs/nls.build.js',
138
			'vs/nls.d.ts',
139 140
			'vs/css.js',
			'vs/css.build.js',
141 142 143 144 145
			'vs/css.d.ts',
			'vs/base/worker/workerMain.ts',
		],
		renames: {
			'vs/nls.mock.ts': 'vs/nls.ts'
A
Alex Dima 已提交
146 147
		}
	});
148 149 150 151
};
createESMSourcesAndResourcesTask.displayName = 'extract-editor-esm';

const compileEditorESMTask = function () {
152 153 154 155 156 157 158 159 160 161 162 163 164
	if (process.platform === 'win32') {
		const result = cp.spawnSync(`..\\node_modules\\.bin\\tsc.cmd`, {
			cwd: path.join(__dirname, '../out-editor-esm')
		});
		console.log(result.stdout.toString());
		console.log(result.stderr.toString());
	} else {
		const result = cp.spawnSync(`node`, [`../node_modules/.bin/tsc`], {
			cwd: path.join(__dirname, '../out-editor-esm')
		});
		console.log(result.stdout.toString());
		console.log(result.stderr.toString());
	}
165 166
};
compileEditorESMTask.displayName = 'compile-editor-esm';
A
Alex Dima 已提交
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
function toExternalDTS(contents) {
	let lines = contents.split('\n');
	let killNextCloseCurlyBrace = false;
	for (let i = 0; i < lines.length; i++) {
		let line = lines[i];

		if (killNextCloseCurlyBrace) {
			if ('}' === line) {
				lines[i] = '';
				killNextCloseCurlyBrace = false;
				continue;
			}

			if (line.indexOf('    ') === 0) {
				lines[i] = line.substr(4);
			} else if (line.charAt(0) === '\t') {
				lines[i] = line.substr(1);
			}

			continue;
		}

		if ('declare namespace monaco {' === line) {
			lines[i] = '';
			killNextCloseCurlyBrace = true;
			continue;
		}

		if (line.indexOf('declare namespace monaco.') === 0) {
			lines[i] = line.replace('declare namespace monaco.', 'export namespace ');
		}
	}
	return lines.join('\n');
}

203 204 205 206 207 208 209 210 211 212
function filterStream(testFunc) {
	return es.through(function (data) {
		if (!testFunc(data.relative)) {
			return;
		}
		this.emit('data', data);
	});
}

const finalEditorResourcesTask = function () {
A
Alex Dima 已提交
213 214 215 216 217 218 219 220
	return es.merge(
		// other assets
		es.merge(
			gulp.src('build/monaco/LICENSE'),
			gulp.src('build/monaco/ThirdPartyNotices.txt'),
			gulp.src('src/vs/monaco.d.ts')
		).pipe(gulp.dest('out-monaco-editor-core')),

A
Alex Dima 已提交
221 222 223 224 225 226
		// place the .d.ts in the esm folder
		gulp.src('src/vs/monaco.d.ts')
			.pipe(es.through(function (data) {
				this.emit('data', new File({
					path: data.path.replace(/monaco\.d\.ts/, 'editor.api.d.ts'),
					base: data.base,
227
					contents: Buffer.from(toExternalDTS(data.contents.toString()))
A
Alex Dima 已提交
228 229 230 231
				}));
			}))
			.pipe(gulp.dest('out-monaco-editor-core/esm/vs/editor')),

232 233
		// package.json
		gulp.src('build/monaco/package.json')
J
Joao Moreno 已提交
234
			.pipe(es.through(function (data) {
235 236
				var json = JSON.parse(data.contents.toString());
				json.private = false;
237
				data.contents = Buffer.from(JSON.stringify(json, null, '  '));
238 239 240 241
				this.emit('data', data);
			}))
			.pipe(gulp.dest('out-monaco-editor-core')),

242 243
		// README.md
		gulp.src('build/monaco/README-npm.md')
J
Joao Moreno 已提交
244
			.pipe(es.through(function (data) {
245 246 247 248 249 250 251 252
				this.emit('data', new File({
					path: data.path.replace(/README-npm\.md/, 'README.md'),
					base: data.base,
					contents: data.contents
				}));
			}))
			.pipe(gulp.dest('out-monaco-editor-core')),

A
Alex Dima 已提交
253 254 255 256 257 258 259 260
		// dev folder
		es.merge(
			gulp.src('out-editor/**/*')
		).pipe(gulp.dest('out-monaco-editor-core/dev')),

		// min folder
		es.merge(
			gulp.src('out-editor-min/**/*')
J
Joao Moreno 已提交
261
		).pipe(filterStream(function (path) {
A
Alex Dima 已提交
262
			// no map files
263
			return !/(\.js\.map$)|(nls\.metadata\.json$)|(bundleInfo\.json$)/.test(path);
J
Joao Moreno 已提交
264
		})).pipe(es.through(function (data) {
A
Alex Dima 已提交
265 266 267 268 269 270 271 272 273 274
			// tweak the sourceMappingURL
			if (!/\.js$/.test(data.path)) {
				this.emit('data', data);
				return;
			}

			var relativePathToMap = path.relative(path.join(data.relative), path.join('min-maps', data.relative + '.map'));

			var strContents = data.contents.toString();
			var newStr = '//# sourceMappingURL=' + relativePathToMap.replace(/\\/g, '/');
A
Alex Dima 已提交
275
			strContents = strContents.replace(/\/\/# sourceMappingURL=[^ ]+$/, newStr);
A
Alex Dima 已提交
276

277
			data.contents = Buffer.from(strContents);
A
Alex Dima 已提交
278 279 280 281 282 283
			this.emit('data', data);
		})).pipe(gulp.dest('out-monaco-editor-core/min')),

		// min-maps folder
		es.merge(
			gulp.src('out-editor-min/**/*')
J
Joao Moreno 已提交
284
		).pipe(filterStream(function (path) {
A
Alex Dima 已提交
285
			// no map files
A
Alex Dima 已提交
286
			return /\.js\.map$/.test(path);
A
Alex Dima 已提交
287 288
		})).pipe(gulp.dest('out-monaco-editor-core/min-maps'))
	);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
};
finalEditorResourcesTask.displayName = 'final-editor-resources';

gulp.task('editor-distro',
	util.task.series(
		util.task.parallel(
			util.rimraf('out-editor-src'),
			util.rimraf('out-editor-build'),
			util.rimraf('out-editor-esm'),
			util.rimraf('out-monaco-editor-core'),
			util.rimraf('out-editor'),
			util.rimraf('out-editor-min')
		),
		extractEditorSrcTask,
		util.task.parallel(
			util.task.series(
				compileEditorAMDTask,
				optimizeEditorAMDTask,
				minifyEditorAMDTask
			),
			util.task.series(
				createESMSourcesAndResourcesTask,
				compileEditorESMTask
			)
		),
		finalEditorResourcesTask
	)
);
317 318 319 320 321 322 323 324

//#region monaco type checking

function createTscCompileTask(watch) {
	return () => {
		const createReporter = require('./lib/reporter').createReporter;

		return new Promise((resolve, reject) => {
J
Johannes Rieken 已提交
325
			const args = ['./node_modules/.bin/tsc', '-p', './src/tsconfig.monaco.json', '--noEmit'];
326 327 328
			if (watch) {
				args.push('-w');
			}
J
Johannes Rieken 已提交
329
			const child = cp.spawn(`node`, args, {
330
				cwd: path.join(__dirname, '..'),
J
Johannes Rieken 已提交
331
				// stdio: [null, 'pipe', 'inherit']
332 333 334 335
			});
			let errors = [];
			let reporter = createReporter();
			let report;
A
Alex Dima 已提交
336
			// eslint-disable-next-line no-control-regex
J
Johannes Rieken 已提交
337
			let magic = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; // https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

			child.stdout.on('data', data => {
				let str = String(data);
				str = str.replace(magic, '').trim();
				if (str.indexOf('Starting compilation') >= 0 || str.indexOf('File change detected') >= 0) {
					errors.length = 0;
					report = reporter.end(false);

				} else if (str.indexOf('Compilation complete') >= 0) {
					report.end();

				} else if (str) {
					let match = /(.*\(\d+,\d+\): )(.*: )(.*)/.exec(str);
					if (match) {
						// trying to massage the message so that it matches the gulp-tsb error messages
						// e.g. src/vs/base/common/strings.ts(663,5): error TS2322: Type '1234' is not assignable to type 'string'.
						let fullpath = path.join(root, match[1]);
						let message = match[3];
J
Johannes Rieken 已提交
356
						// @ts-ignore
357 358
						reporter(fullpath + message);
					} else {
J
Johannes Rieken 已提交
359
						// @ts-ignore
360 361 362 363 364 365 366 367 368 369
						reporter(str);
					}
				}
			});
			child.on('exit', resolve);
			child.on('error', reject);
		});
	};
}

A
Alex Dima 已提交
370 371 372 373 374 375 376
const monacoTypecheckWatchTask = createTscCompileTask(true);
monacoTypecheckWatchTask.displayName = 'monaco-typecheck-watch';
exports.monacoTypecheckWatchTask = monacoTypecheckWatchTask;

const monacoTypecheckTask = createTscCompileTask(false);
monacoTypecheckTask.displayName = 'monaco-typecheck';
exports.monacoTypecheckTask = monacoTypecheckTask;
377 378

//#endregion