compilation.ts 5.6 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
/*---------------------------------------------------------------------------------------------
 *  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();

A
Alex Dima 已提交
24 25 26 27 28 29 30 31 32
function getTypeScriptCompilerOptions(src: string) {
	const rootDir = path.join(__dirname, `../../${src}`);
	const options = require(`../../${src}/tsconfig.json`).compilerOptions;
	options.verbose = false;
	options.sourceMap = true;
	if (process.env['VSCODE_NO_SOURCEMAP']) { // To be used by developers in a hurry
		options.sourceMap = false;
	}
	options.rootDir = rootDir;
33
	options.baseUrl = rootDir;
A
Alex Dima 已提交
34 35 36
	options.sourceRoot = util.toFileUri(rootDir);
	options.newLine = /\r\n/.test(fs.readFileSync(__filename, 'utf8')) ? 'CRLF' : 'LF';
	return options;
37
}
38

A
Alex Dima 已提交
39 40
function createCompile(src: string, build: boolean, emitError?: boolean): (token?: util.ICancellationToken) => NodeJS.ReadWriteStream {
	const opts = _.clone(getTypeScriptCompilerOptions(src));
41 42 43
	opts.inlineSources = !!build;
	opts.noFilesystemLookup = true;

M
Matt Bierner 已提交
44
	const ts = tsb.create(opts, true, null, err => reporter(err.toString()));
45

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

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		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,
A
Alex Dima 已提交
66
				sourceRoot: opts.sourceRoot
67 68 69 70 71 72 73 74
			}))
			.pipe(tsFilter.restore)
			.pipe(reporter.end(emitError));

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

M
Matt Bierner 已提交
75 76 77 78 79 80 81
const typesDts = [
	'node_modules/typescript/lib/*.d.ts',
	'node_modules/@types/**/*.d.ts',
	'!node_modules/@types/node/**/*',
	'!node_modules/@types/webpack/**/*',
	'!node_modules/@types/uglify-js/**/*',
];
M
Matt Bierner 已提交
82

A
Alex Dima 已提交
83
export function compileTask(src: string, out: string, build: boolean): () => NodeJS.ReadWriteStream {
84 85

	return function () {
A
Alex Dima 已提交
86
		const compile = createCompile(src, build, true);
87

A
Alex Dima 已提交
88 89
		const srcPipe = es.merge(
			gulp.src(`${src}/**`, { base: `${src}` }),
M
Matt Bierner 已提交
90
			gulp.src(typesDts),
91 92
		);

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

A
Alex Dima 已提交
96
		return srcPipe
97
			.pipe(compile())
98
			.pipe(dtsFilter)
99
			.pipe(gulp.dest(out))
100
			.pipe(dtsFilter.restore)
A
Alex Dima 已提交
101
			.pipe(src !== 'src' ? es.through() : monacodtsTask(out, false));
102 103 104
	};
}

J
Joao Moreno 已提交
105
export function watchTask(out: string, build: boolean): () => NodeJS.ReadWriteStream {
106 107

	return function () {
A
Alex Dima 已提交
108
		const compile = createCompile('src', build);
109

110 111
		const src = es.merge(
			gulp.src('src/**', { base: 'src' }),
M
Matt Bierner 已提交
112
			gulp.src(typesDts),
113 114 115
		);
		const watchSrc = watch('src/**', { base: 'src' });

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

119 120
		return watchSrc
			.pipe(util.incremental(compile, src, true))
121
			.pipe(dtsFilter)
122
			.pipe(gulp.dest(out))
123
			.pipe(dtsFilter.restore)
124 125 126 127
			.pipe(monacodtsTask(out, true));
	};
}

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

130 131
	const basePath = path.resolve(process.cwd(), out);

132 133 134 135 136 137 138 139 140 141 142
	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();
143
		}
144
	}
145

146
	const setInputFile = (filePath: string, contents: string) => {
147 148 149 150 151 152 153 154 155
		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();
156
		}
157 158 159 160
	};

	const run = () => {
		const result = monacodts.run(out, inputFiles);
161 162 163 164
		if (!result.isTheSame) {
			if (isWatch) {
				fs.writeFileSync(result.filePath, result.content);
			} else {
A
Alex Dima 已提交
165
				fs.writeFileSync(result.filePath, result.content);
166 167 168 169 170 171 172 173
				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 已提交
174
		watch('build/monaco/*').pipe(es.through(function () {
175
			run();
176 177 178
		}));
	}

179
	resultStream = es.through(function (data) {
180
		const filePath = path.normalize(path.resolve(basePath, data.relative));
181 182 183 184 185 186
		if (neededFiles[filePath]) {
			setInputFile(filePath, data.contents.toString());
		}
		this.emit('data', data);
	});

187 188
	return resultStream;
}