Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/compilation.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import es from 'event-stream';
7
import fs from 'fs';
8
import gulp from 'gulp';
9
import path from 'path';
10
import * as monacodts from './monaco-api';
11
import * as nls from './nls';
12
import { createReporter } from './reporter';
13
import * as util from './util';
14
import fancyLog from 'fancy-log';
15
import ansiColors from 'ansi-colors';
16
import os from 'os';
17
import File from 'vinyl';
18
import * as task from './task';
19
import { Mangler } from './mangle/index';
20
import { RawSourceMap } from 'source-map';
21
import ts = require('typescript');
22
const watch = require('./watch');
23
24
25
// --- gulp-tsb: compile and transpile --------------------------------
26
27
const reporter = createReporter();
28
29
function getTypeScriptCompilerOptions(src: string): ts.CompilerOptions {
30
const rootDir = path.join(__dirname, `../../${src}`);
31
const options: ts.CompilerOptions = {};
32
options.verbose = false;
33
options.sourceMap = true;
34
if (process.env['VSCODE_NO_SOURCEMAP']) { // To be used by developers in a hurry
35
options.sourceMap = false;
36
}
37
options.rootDir = rootDir;
38
options.baseUrl = rootDir;
39
options.sourceRoot = util.toFileUri(rootDir);
40
options.newLine = /\r\n/.test(fs.readFileSync(__filename, 'utf8')) ? 0 : 1;
41
return options;
42
}
43
44
interface ICompileTaskOptions {
45
readonly build: boolean;
46
readonly emitError: boolean;
47
readonly transpileOnly: boolean | { esbuild: boolean };
48
readonly preserveEnglish: boolean;
49
}
50
51
export function createCompile(src: string, { build, emitError, transpileOnly, preserveEnglish }: ICompileTaskOptions) {
52
const tsb = require('./tsb') as typeof import('./tsb');
53
const sourcemaps = require('gulp-sourcemaps') as typeof import('gulp-sourcemaps');
54
55
56
const projectPath = path.join(__dirname, '../../', src, 'tsconfig.json');
57
const overrideOptions = { ...getTypeScriptCompilerOptions(src), inlineSources: Boolean(build) };
58
if (!build) {
59
overrideOptions.inlineSourceMap = true;
60
}
61
62
const compilation = tsb.create(projectPath, overrideOptions, {
63
verbose: false,
64
transpileOnly: Boolean(transpileOnly),
65
transpileWithEsbuild: typeof transpileOnly !== 'boolean' && transpileOnly.esbuild
66
}, err => reporter(err));
67
68
function pipeline(token?: util.ICancellationToken) {
69
const bom = require('gulp-bom') as typeof import('gulp-bom');
70
71
const tsFilter = util.filter(data => /\.ts$/.test(data.path));
72
const isUtf8Test = (f: File) => /(\/|\\)test(\/|\\).*utf8/.test(f.path);
73
const isRuntimeJs = (f: File) => f.path.endsWith('.js') && !f.path.includes('fixtures');
74
const noDeclarationsFilter = util.filter(data => !(/\.d\.ts$/.test(data.path)));
75
76
const input = es.through();
77
const output = input
78
.pipe(util.$if(isUtf8Test, bom())) // this is required to preserve BOM in test files that loose it otherwise
79
.pipe(util.$if(!build && isRuntimeJs, util.appendOwnPathSourceURL()))
80
.pipe(tsFilter)
81
.pipe(util.loadSourcemaps())
82
.pipe(compilation(token))
83
.pipe(noDeclarationsFilter)
84
.pipe(util.$if(build, nls.nls({ preserveEnglish })))
85
.pipe(noDeclarationsFilter.restore)
86
.pipe(util.$if(!transpileOnly, sourcemaps.write('.', {
87
addComment: false,
88
includeContent: !!build,
89
sourceRoot: overrideOptions.sourceRoot
90
})))
91
.pipe(tsFilter.restore)
92
.pipe(reporter.end(!!emitError));
93
94
return es.duplex(input, output);
95
}
96
pipeline.tsProjectSrc = () => {
97
return compilation.src({ base: src });
98
};
99
pipeline.projectPath = projectPath;
100
return pipeline;
101
}
102
103
export function transpileTask(src: string, out: string, esbuild?: boolean): task.StreamTask {
104
105
const task = () => {
106
107
const transpile = createCompile(src, { build: false, emitError: true, transpileOnly: { esbuild: !!esbuild }, preserveEnglish: false });
108
const srcPipe = gulp.src(`${src}/**`, { base: `${src}` });
109
110
return srcPipe
111
.pipe(transpile())
112
.pipe(gulp.dest(out));
113
};
114
115
task.taskName = `transpile-${path.basename(src)}`;
116
return task;
117
}
118
119
export function compileTask(src: string, out: string, build: boolean, options: { disableMangle?: boolean; preserveEnglish?: boolean } = {}): task.StreamTask {
120
121
const task = () => {
122
123
if (os.totalmem() < 4_000_000_000) {
124
throw new Error('compilation requires 4GB of RAM');
125
}
126
127
const compile = createCompile(src, { build, emitError: true, transpileOnly: false, preserveEnglish: !!options.preserveEnglish });
128
const srcPipe = gulp.src(`${src}/**`, { base: `${src}` });
129
const generator = new MonacoGenerator(false);
130
if (src === 'src') {
131
generator.execute();
132
}
133
134
// mangle: TypeScript to TypeScript
135
let mangleStream = es.through();
136
if (build && !options.disableMangle) {
137
let ts2tsMangler = new Mangler(compile.projectPath, (...data) => fancyLog(ansiColors.blue('[mangler]'), ...data), { mangleExports: true, manglePrivateFields: true });
138
const newContentsByFileName = ts2tsMangler.computeNewFileContents(new Set(['saveState']));
139
mangleStream = es.through(async function write(data: File & { sourceMap?: RawSourceMap }) {
140
type TypeScriptExt = typeof ts & { normalizePath(path: string): string };
141
const tsNormalPath = (<TypeScriptExt>ts).normalizePath(data.path);
142
const newContents = (await newContentsByFileName).get(tsNormalPath);
143
if (newContents !== undefined) {
144
data.contents = Buffer.from(newContents.out);
145
data.sourceMap = newContents.sourceMap && JSON.parse(newContents.sourceMap);
146
}
147
this.push(data);
148
}, async function end() {
149
// free resources
150
(await newContentsByFileName).clear();
151
152
this.push(null);
153
(<any>ts2tsMangler) = undefined;
154
});
155
}
156
157
return srcPipe
158
.pipe(mangleStream)
159
.pipe(generator.stream)
160
.pipe(compile())
161
.pipe(gulp.dest(out));
162
};
163
164
task.taskName = `compile-${path.basename(src)}`;
165
return task;
166
}
167
168
export function watchTask(out: string, build: boolean, srcPath: string = 'src'): task.StreamTask {
169
170
const task = () => {
171
const compile = createCompile(srcPath, { build, emitError: false, transpileOnly: false, preserveEnglish: false });
172
173
const src = gulp.src(`${srcPath}/**`, { base: srcPath });
174
const watchSrc = watch(`${srcPath}/**`, { base: srcPath, readDelay: 200 });
175
176
const generator = new MonacoGenerator(true);
177
generator.execute();
178
179
return watchSrc
180
.pipe(generator.stream)
181
.pipe(util.incremental(compile, src, true))
182
.pipe(gulp.dest(out));
183
};
184
task.taskName = `watch-${path.basename(out)}`;
185
return task;
186
}
187
188
const REPO_SRC_FOLDER = path.join(__dirname, '../../src');
189
190
class MonacoGenerator {
191
private readonly _isWatch: boolean;
192
public readonly stream: NodeJS.ReadWriteStream;
193
194
private readonly _watchedFiles: { [filePath: string]: boolean };
195
private readonly _fsProvider: monacodts.FSProvider;
196
private readonly _declarationResolver: monacodts.DeclarationResolver;
197
198
constructor(isWatch: boolean) {
199
this._isWatch = isWatch;
200
this.stream = es.through();
201
this._watchedFiles = {};
202
const onWillReadFile = (moduleId: string, filePath: string) => {
203
if (!this._isWatch) {
204
return;
205
}
206
if (this._watchedFiles[filePath]) {
207
return;
208
}
209
this._watchedFiles[filePath] = true;
210
211
fs.watchFile(filePath, () => {
212
this._declarationResolver.invalidateCache(moduleId);
213
this._executeSoon();
214
});
215
};
216
this._fsProvider = new class extends monacodts.FSProvider {
217
public readFileSync(moduleId: string, filePath: string): Buffer {
218
onWillReadFile(moduleId, filePath);
219
return super.readFileSync(moduleId, filePath);
220
}
221
};
222
this._declarationResolver = new monacodts.DeclarationResolver(this._fsProvider);
223
224
if (this._isWatch) {
225
fs.watchFile(monacodts.RECIPE_PATH, () => {
226
this._executeSoon();
227
});
228
}
229
}
230
231
private _executeSoonTimer: NodeJS.Timeout | null = null;
232
private _executeSoon(): void {
233
if (this._executeSoonTimer !== null) {
234
clearTimeout(this._executeSoonTimer);
235
this._executeSoonTimer = null;
236
}
237
this._executeSoonTimer = setTimeout(() => {
238
this._executeSoonTimer = null;
239
this.execute();
240
}, 20);
241
}
242
243
private _run(): monacodts.IMonacoDeclarationResult | null {
244
const r = monacodts.run3(this._declarationResolver);
245
if (!r && !this._isWatch) {
246
// The build must always be able to generate the monaco.d.ts
247
throw new Error(`monaco.d.ts generation error - Cannot continue`);
248
}
249
return r;
250
}
251
252
private _log(message: any, ...rest: any[]): void {
253
fancyLog(ansiColors.cyan('[monaco.d.ts]'), message, ...rest);
254
}
255
256
public execute(): void {
257
const startTime = Date.now();
258
const result = this._run();
259
if (!result) {
260
// nothing really changed
261
return;
262
}
263
if (result.isTheSame) {
264
return;
265
}
266
267
fs.writeFileSync(result.filePath, result.content);
268
fs.writeFileSync(path.join(REPO_SRC_FOLDER, 'vs/editor/common/standalone/standaloneEnums.ts'), result.enums);
269
this._log(`monaco.d.ts is changed - total time took ${Date.now() - startTime} ms`);
270
if (!this._isWatch) {
271
this.stream.emit('error', 'monaco.d.ts is no longer up to date. Please run gulp watch and commit the new file.');
272
}
273
}
274
}
275
276
function generateApiProposalNames() {
277
let eol: string;
278
279
try {
280
const src = fs.readFileSync('src/vs/platform/extensions/common/extensionsApiProposals.ts', 'utf-8');
281
const match = /\r?\n/m.exec(src);
282
eol = match ? match[0] : os.EOL;
283
} catch {
284
eol = os.EOL;
285
}
286
287
const pattern = /vscode\.proposed\.([a-zA-Z\d]+)\.d\.ts$/;
288
const versionPattern = /^\s*\/\/\s*version\s*:\s*(\d+)\s*$/mi;
289
const proposals = new Map<string, { proposal: string; version?: number }>();
290
291
const input = es.through();
292
const output = input
293
.pipe(util.filter((f: File) => pattern.test(f.path)))
294
.pipe(es.through((f: File) => {
295
const name = path.basename(f.path);
296
const match = pattern.exec(name);
297
298
if (!match) {
299
return;
300
}
301
302
const proposalName = match[1];
303
304
const contents = f.contents!.toString('utf8');
305
const versionMatch = versionPattern.exec(contents);
306
const version = versionMatch ? versionMatch[1] : undefined;
307
308
proposals.set(proposalName, {
309
proposal: `https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.${proposalName}.d.ts`,
310
version: version ? parseInt(version) : undefined
311
});
312
}, function () {
313
const names = [...proposals.keys()].sort();
314
const contents = [
315
'/*---------------------------------------------------------------------------------------------',
316
' * Copyright (c) Microsoft Corporation. All rights reserved.',
317
' * Licensed under the MIT License. See License.txt in the project root for license information.',
318
' *--------------------------------------------------------------------------------------------*/',
319
'',
320
'// THIS IS A GENERATED FILE. DO NOT EDIT DIRECTLY.',
321
'',
322
'const _allApiProposals = {',
323
`${names.map(proposalName => {
324
const proposal = proposals.get(proposalName)!;
325
return `\t${proposalName}: {${eol}\t\tproposal: '${proposal.proposal}',${eol}${proposal.version ? `\t\tversion: ${proposal.version}${eol}` : ''}\t}`;
326
}).join(`,${eol}`)}`,
327
'};',
328
'export const allApiProposals = Object.freeze<{ [proposalName: string]: Readonly<{ proposal: string; version?: number }> }>(_allApiProposals);',
329
'export type ApiProposalName = keyof typeof _allApiProposals;',
330
'',
331
].join(eol);
332
333
this.emit('data', new File({
334
path: 'vs/platform/extensions/common/extensionsApiProposals.ts',
335
contents: Buffer.from(contents)
336
}));
337
this.emit('end');
338
}));
339
340
return es.duplex(input, output);
341
}
342
343
const apiProposalNamesReporter = createReporter('api-proposal-names');
344
345
export const compileApiProposalNamesTask = task.define('compile-api-proposal-names', () => {
346
return gulp.src('src/vscode-dts/**')
347
.pipe(generateApiProposalNames())
348
.pipe(gulp.dest('src'))
349
.pipe(apiProposalNamesReporter.end(true));
350
});
351
352
export const watchApiProposalNamesTask = task.define('watch-api-proposal-names', () => {
353
const task = () => gulp.src('src/vscode-dts/**')
354
.pipe(generateApiProposalNames())
355
.pipe(apiProposalNamesReporter.end(true));
356
357
return watch('src/vscode-dts/**', { readDelay: 200 })
358
.pipe(util.debounce(task))
359
.pipe(gulp.dest('src'));
360
});
361
362