compilation.ts 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as gulp from 'gulp';
import * as tsb from 'gulp-tsb';
import * as es from 'event-stream';
const watch = require('./watch');
import * as nls from './nls';
import * as util from './util';
import { createReporter } from './reporter';
import * as path from 'path';
import * as bom from 'gulp-bom';
import * as sourcemaps from 'gulp-sourcemaps';
import * as _ from 'underscore';
import * as monacodts from '../monaco/api';
import * as fs from 'fs';

const reporter = createReporter();

const rootDir = path.join(__dirname, '../../src');
const options = require('../../src/tsconfig.json').compilerOptions;
options.verbose = false;
options.sourceMap = true;
28 29 30
if (process.env['VSCODE_NO_SOURCEMAP']) { // To be used by developers in a hurry
	options.sourceMap = false;
}
31 32
options.rootDir = rootDir;
options.sourceRoot = util.toFileUri(rootDir);
J
Joao Moreno 已提交
33
options.newLine = /\r\n/.test(fs.readFileSync(__filename, 'utf8')) ? 'CRLF' : 'LF';
34

J
Joao Moreno 已提交
35
function createCompile(build: boolean, emitError?: boolean): (token?: util.ICancellationToken) => NodeJS.ReadWriteStream {
36 37 38 39
	const opts = _.clone(options);
	opts.inlineSources = !!build;
	opts.noFilesystemLookup = true;

A
Alex Dima 已提交
40
	const ts = tsb.create(opts, null, null, err => reporter(err.toString()));
41

J
Joao Moreno 已提交
42
	return function (token?: util.ICancellationToken) {
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
		const utf8Filter = util.filter(data => /(\/|\\)test(\/|\\).*utf8/.test(data.path));
		const tsFilter = util.filter(data => /\.ts$/.test(data.path));
		const noDeclarationsFilter = util.filter(data => !(/\.d\.ts$/.test(data.path)));

		const input = es.through();
		const output = input
			.pipe(utf8Filter)
			.pipe(bom())
			.pipe(utf8Filter.restore)
			.pipe(tsFilter)
			.pipe(util.loadSourcemaps())
			.pipe(ts(token))
			.pipe(noDeclarationsFilter)
			.pipe(build ? nls() : es.through())
			.pipe(noDeclarationsFilter.restore)
			.pipe(sourcemaps.write('.', {
				addComment: false,
				includeContent: !!build,
B
Benjamin Pasero 已提交
62
				sourceRoot: options.sourceRoot
63 64 65 66 67 68 69 70
			}))
			.pipe(tsFilter.restore)
			.pipe(reporter.end(emitError));

		return es.duplex(input, output);
	};
}

J
Joao Moreno 已提交
71
export function compileTask(out: string, build: boolean): () => NodeJS.ReadWriteStream {
72 73

	return function () {
74 75
		const compile = createCompile(build, true);

76 77
		const src = es.merge(
			gulp.src('src/**', { base: 'src' }),
78
			gulp.src('node_modules/typescript/lib/lib.d.ts'),
79 80
		);

81 82 83
		// Do not write .d.ts files to disk, as they are not needed there.
		const dtsFilter = util.filter(data => !/\.d\.ts$/.test(data.path));

84 85
		return src
			.pipe(compile())
86
			.pipe(dtsFilter)
87
			.pipe(gulp.dest(out))
88
			.pipe(dtsFilter.restore)
89 90 91 92
			.pipe(monacodtsTask(out, false));
	};
}

J
Joao Moreno 已提交
93
export function watchTask(out: string, build: boolean): () => NodeJS.ReadWriteStream {
94 95

	return function () {
96 97
		const compile = createCompile(build);

98 99
		const src = es.merge(
			gulp.src('src/**', { base: 'src' }),
100
			gulp.src('node_modules/typescript/lib/lib.d.ts'),
101 102 103
		);
		const watchSrc = watch('src/**', { base: 'src' });

104 105 106
		// Do not write .d.ts files to disk, as they are not needed there.
		const dtsFilter = util.filter(data => !/\.d\.ts$/.test(data.path));

107 108
		return watchSrc
			.pipe(util.incremental(compile, src, true))
109
			.pipe(dtsFilter)
110
			.pipe(gulp.dest(out))
111
			.pipe(dtsFilter.restore)
112 113 114 115
			.pipe(monacodtsTask(out, true));
	};
}

J
Joao Moreno 已提交
116
function monacodtsTask(out: string, isWatch: boolean): NodeJS.ReadWriteStream {
117

118 119
	const basePath = path.resolve(process.cwd(), out);

120 121 122 123 124 125 126 127 128 129 130
	const neededFiles: { [file: string]: boolean; } = {};
	monacodts.getFilesToWatch(out).forEach(function (filePath) {
		filePath = path.normalize(filePath);
		neededFiles[filePath] = true;
	});

	const inputFiles: { [file: string]: string; } = {};
	for (let filePath in neededFiles) {
		if (/\bsrc(\/|\\)vs\b/.test(filePath)) {
			// This file is needed from source => simply read it now
			inputFiles[filePath] = fs.readFileSync(filePath).toString();
131
		}
132
	}
133

134
	const setInputFile = (filePath: string, contents: string) => {
135 136 137 138 139 140 141 142 143
		if (inputFiles[filePath] === contents) {
			// no change
			return;
		}
		inputFiles[filePath] = contents;
		const neededInputFilesCount = Object.keys(neededFiles).length;
		const availableInputFilesCount = Object.keys(inputFiles).length;
		if (neededInputFilesCount === availableInputFilesCount) {
			run();
144
		}
145 146 147 148
	};

	const run = () => {
		const result = monacodts.run(out, inputFiles);
149 150 151 152 153 154 155 156 157 158 159 160
		if (!result.isTheSame) {
			if (isWatch) {
				fs.writeFileSync(result.filePath, result.content);
			} else {
				resultStream.emit('error', 'monaco.d.ts is no longer up to date. Please run gulp watch and commit the new file.');
			}
		}
	};

	let resultStream: NodeJS.ReadWriteStream;

	if (isWatch) {
J
Joao Moreno 已提交
161
		watch('build/monaco/*').pipe(es.through(function () {
162
			run();
163 164 165
		}));
	}

166
	resultStream = es.through(function (data) {
167
		const filePath = path.normalize(path.resolve(basePath, data.relative));
168 169 170 171 172 173
		if (neededFiles[filePath]) {
			setInputFile(filePath, data.contents.toString());
		}
		this.emit('data', data);
	});

174 175
	return resultStream;
}