gulpfile.editor.js 9.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');
I
isidor 已提交
15 16

var root = path.dirname(__dirname);
A
Alex Dima 已提交
17
var sha1 = util.getVersion(root);
E
Erich Gamma 已提交
18
// @ts-ignore Microsoft/TypeScript#21262 complains about a require of a JSON file
A
Alex Dima 已提交
19 20
var semver = require('./monaco/package.json').version;
var headerVersion = semver + '(' + sha1 + ')';
I
isidor 已提交
21 22 23

// Build

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

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

var editorOtherSources = [
];

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

62
function editorLoaderConfig() {
I
isidor 已提交
63 64
	var result = common.loaderConfig();

65 66
	// never ship octicons in editor
	result.paths['vs/base/browser/ui/octiconLabel/octiconLabel'] = 'out-build/vs/base/browser/ui/octiconLabel/octiconLabel.mock';
I
isidor 已提交
67

68
	// force css inlining to use base64 -- see https://github.com/Microsoft/monaco-editor/issues/148
69 70 71 72
	result['vs/css'] = {
		inlineResources: 'base64',
		inlineResourcesLimit: 3000 // see https://github.com/Microsoft/monaco-editor/issues/336
	};
73

I
isidor 已提交
74 75 76
	return result;
}

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

I
isidor 已提交
79
gulp.task('clean-optimized-editor', util.rimraf('out-editor'));
80
gulp.task('optimize-editor', ['clean-optimized-editor', 'compile-client-build'], common.optimizeTask({
I
isidor 已提交
81 82 83
	entryPoints: editorEntryPoints,
	otherSources: editorOtherSources,
	resources: editorResources,
84
	loaderConfig: editorLoaderConfig(),
A
Alex Dima 已提交
85
	bundleLoader: false,
I
isidor 已提交
86
	header: BUNDLED_FILE_HEADER,
A
Alex Dima 已提交
87
	bundleInfo: true,
E
Erich Gamma 已提交
88
	out: 'out-editor',
A
Alex Dima 已提交
89
	languages: languages
I
isidor 已提交
90 91 92
}));

gulp.task('clean-minified-editor', util.rimraf('out-editor-min'));
J
Joao Moreno 已提交
93
gulp.task('minify-editor', ['clean-minified-editor', 'optimize-editor'], common.minifyTask('out-editor'));
A
Alex Dima 已提交
94

A
Alex Dima 已提交
95
gulp.task('clean-editor-esm', util.rimraf('out-editor-esm'));
96
gulp.task('extract-editor-esm', ['clean-editor-esm', 'clean-editor-distro'], function () {
A
Alex Dima 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
	standalone.createESMSourcesAndResources({
		entryPoints: [
			'vs/editor/editor.main',
			'vs/editor/editor.worker'
		],
		outFolder: './out-editor-esm/src',
		outResourcesFolder: './out-monaco-editor-core/esm',
		redirects: {
			'vs/base/browser/ui/octiconLabel/octiconLabel': 'vs/base/browser/ui/octiconLabel/octiconLabel.mock',
			'vs/nls': 'vs/nls.mock',
		}
	});
});
110
gulp.task('compile-editor-esm', ['extract-editor-esm', 'clean-editor-distro'], function () {
A
Alex Dima 已提交
111 112 113 114 115 116
	const result = cp.spawnSync(`node`, [`../node_modules/.bin/tsc`], {
		cwd: path.join(__dirname, '../out-editor-esm')
	});
	console.log(result.stdout.toString());
});

A
Alex Dima 已提交
117
gulp.task('clean-editor-distro', util.rimraf('out-monaco-editor-core'));
A
Alex Dima 已提交
118
gulp.task('editor-distro', ['clean-editor-distro', 'compile-editor-esm', 'minify-editor', 'optimize-editor'], function () {
A
Alex Dima 已提交
119 120 121 122 123 124 125 126
	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')),

127 128
		// package.json
		gulp.src('build/monaco/package.json')
J
Joao Moreno 已提交
129
			.pipe(es.through(function (data) {
130 131
				var json = JSON.parse(data.contents.toString());
				json.private = false;
132
				data.contents = Buffer.from(JSON.stringify(json, null, '  '));
133 134 135 136
				this.emit('data', data);
			}))
			.pipe(gulp.dest('out-monaco-editor-core')),

137 138
		// README.md
		gulp.src('build/monaco/README-npm.md')
J
Joao Moreno 已提交
139
			.pipe(es.through(function (data) {
140 141 142 143 144 145 146 147
				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 已提交
148 149 150 151 152 153 154 155
		// 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 已提交
156
		).pipe(filterStream(function (path) {
A
Alex Dima 已提交
157
			// no map files
158
			return !/(\.js\.map$)|(nls\.metadata\.json$)|(bundleInfo\.json$)/.test(path);
J
Joao Moreno 已提交
159
		})).pipe(es.through(function (data) {
A
Alex Dima 已提交
160 161 162 163 164 165 166 167 168 169 170 171
			// 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, '/');
			strContents = strContents.replace(/\/\/\# sourceMappingURL=[^ ]+$/, newStr);

172
			data.contents = Buffer.from(strContents);
A
Alex Dima 已提交
173 174 175 176 177 178
			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 已提交
179
		).pipe(filterStream(function (path) {
A
Alex Dima 已提交
180
			// no map files
A
Alex Dima 已提交
181
			return /\.js\.map$/.test(path);
A
Alex Dima 已提交
182 183 184 185
		})).pipe(gulp.dest('out-monaco-editor-core/min-maps'))
	);
});

J
Joao Moreno 已提交
186
gulp.task('analyze-editor-distro', function () {
E
Erich Gamma 已提交
187
	// @ts-ignore Microsoft/TypeScript#21262 complains about a require of a JSON file
A
Alex Dima 已提交
188 189 190 191 192
	var bundleInfo = require('../out-editor/bundleInfo.json');
	var graph = bundleInfo.graph;
	var bundles = bundleInfo.bundles;

	var inverseGraph = {};
J
Joao Moreno 已提交
193
	Object.keys(graph).forEach(function (module) {
A
Alex Dima 已提交
194
		var dependencies = graph[module];
J
Joao Moreno 已提交
195
		dependencies.forEach(function (dep) {
A
Alex Dima 已提交
196 197 198 199 200 201
			inverseGraph[dep] = inverseGraph[dep] || [];
			inverseGraph[dep].push(module);
		});
	});

	var detailed = {};
J
Joao Moreno 已提交
202
	Object.keys(bundles).forEach(function (entryPoint) {
A
Alex Dima 已提交
203 204
		var included = bundles[entryPoint];
		var includedMap = {};
J
Joao Moreno 已提交
205
		included.forEach(function (included) {
A
Alex Dima 已提交
206 207 208 209
			includedMap[included] = true;
		});

		var explanation = [];
J
Joao Moreno 已提交
210
		included.map(function (included) {
A
Alex Dima 已提交
211 212 213 214
			if (included.indexOf('!') >= 0) {
				return;
			}

J
Joao Moreno 已提交
215
			var reason = (inverseGraph[included] || []).filter(function (mod) {
A
Alex Dima 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229
				return !!includedMap[mod];
			});
			explanation.push({
				module: included,
				reason: reason
			});
		});

		detailed[entryPoint] = explanation;
	});

	console.log(JSON.stringify(detailed, null, '\t'));
});

A
Alex Dima 已提交
230
function filterStream(testFunc) {
J
Joao Moreno 已提交
231
	return es.through(function (data) {
A
Alex Dima 已提交
232 233 234 235 236 237
		if (!testFunc(data.relative)) {
			return;
		}
		this.emit('data', data);
	});
}
238 239 240 241 242 243 244 245 246


//#region monaco type checking

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

		return new Promise((resolve, reject) => {
J
Johannes Rieken 已提交
247
			const args = ['./node_modules/.bin/tsc', '-p', './src/tsconfig.monaco.json', '--noEmit'];
248 249 250
			if (watch) {
				args.push('-w');
			}
J
Johannes Rieken 已提交
251
			const child = cp.spawn(`node`, args, {
252
				cwd: path.join(__dirname, '..'),
J
Johannes Rieken 已提交
253
				// stdio: [null, 'pipe', 'inherit']
254 255 256 257
			});
			let errors = [];
			let reporter = createReporter();
			let report;
J
Johannes Rieken 已提交
258
			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
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

			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 已提交
277
						// @ts-ignore
278 279
						reporter(fullpath + message);
					} else {
J
Johannes Rieken 已提交
280
						// @ts-ignore
281 282 283 284 285 286 287 288 289 290 291 292 293 294
						reporter(str);
					}
				}
			});
			child.on('exit', resolve);
			child.on('error', reject);
		});
	};
}

gulp.task('monaco-typecheck-watch', createTscCompileTask(true));
gulp.task('monaco-typecheck', createTscCompileTask(false));

//#endregion