Extract client compilation code out of gulpfile.js
This commit is contained in:
parent
3d726b59c1
commit
9f0f868c65
134
build/lib/compilation.js
Normal file
134
build/lib/compilation.js
Normal file
|
@ -0,0 +1,134 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
var gulp = require('gulp');
|
||||
var tsb = require('gulp-tsb');
|
||||
var es = require('event-stream');
|
||||
var watch = require('./watch');
|
||||
var nls = require('./nls');
|
||||
var util = require('./util');
|
||||
var reporter_1 = require('./reporter');
|
||||
var path = require('path');
|
||||
var bom = require('gulp-bom');
|
||||
var sourcemaps = require('gulp-sourcemaps');
|
||||
var _ = require('underscore');
|
||||
var monacodts = require('../monaco/api');
|
||||
var fs = require('fs');
|
||||
var reporter = reporter_1.createReporter();
|
||||
var rootDir = path.join(__dirname, '../../src');
|
||||
var options = require('../../src/tsconfig.json').compilerOptions;
|
||||
options.verbose = false;
|
||||
options.sourceMap = true;
|
||||
options.rootDir = rootDir;
|
||||
options.sourceRoot = util.toFileUri(rootDir);
|
||||
function createCompile(build, emitError) {
|
||||
var opts = _.clone(options);
|
||||
opts.inlineSources = !!build;
|
||||
opts.noFilesystemLookup = true;
|
||||
var ts = tsb.create(opts, null, null, function (err) { return reporter(err.toString()); });
|
||||
return function (token) {
|
||||
var utf8Filter = util.filter(function (data) { return /(\/|\\)test(\/|\\).*utf8/.test(data.path); });
|
||||
var tsFilter = util.filter(function (data) { return /\.ts$/.test(data.path); });
|
||||
var noDeclarationsFilter = util.filter(function (data) { return !(/\.d\.ts$/.test(data.path)); });
|
||||
var input = es.through();
|
||||
var 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,
|
||||
sourceRoot: options.sourceRoot
|
||||
}))
|
||||
.pipe(tsFilter.restore)
|
||||
.pipe(reporter.end(emitError));
|
||||
return es.duplex(input, output);
|
||||
};
|
||||
}
|
||||
function compileTask(out, build) {
|
||||
var compile = createCompile(build, true);
|
||||
return function () {
|
||||
var src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src('node_modules/typescript/lib/lib.d.ts'));
|
||||
return src
|
||||
.pipe(compile())
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, false));
|
||||
};
|
||||
}
|
||||
exports.compileTask = compileTask;
|
||||
function watchTask(out, build) {
|
||||
var compile = createCompile(build);
|
||||
return function () {
|
||||
var src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src('node_modules/typescript/lib/lib.d.ts'));
|
||||
var watchSrc = watch('src/**', { base: 'src' });
|
||||
return watchSrc
|
||||
.pipe(util.incremental(compile, src, true))
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, true));
|
||||
};
|
||||
}
|
||||
exports.watchTask = watchTask;
|
||||
function monacodtsTask(out, isWatch) {
|
||||
var timer = null;
|
||||
var runSoon = function (howSoon) {
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
timer = setTimeout(function () {
|
||||
timer = null;
|
||||
runNow();
|
||||
}, howSoon);
|
||||
};
|
||||
var runNow = function () {
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
// if (reporter.hasErrors()) {
|
||||
// monacodts.complainErrors();
|
||||
// return;
|
||||
// }
|
||||
var result = monacodts.run(out);
|
||||
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.');
|
||||
}
|
||||
}
|
||||
};
|
||||
var resultStream;
|
||||
if (isWatch) {
|
||||
var filesToWatchMap_1 = {};
|
||||
monacodts.getFilesToWatch(out).forEach(function (filePath) {
|
||||
filesToWatchMap_1[path.normalize(filePath)] = true;
|
||||
});
|
||||
watch('build/monaco/*').pipe(es.through(function () {
|
||||
runSoon(5000);
|
||||
}));
|
||||
resultStream = es.through(function (data) {
|
||||
var filePath = path.normalize(data.path);
|
||||
if (filesToWatchMap_1[filePath]) {
|
||||
runSoon(5000);
|
||||
}
|
||||
this.emit('data', data);
|
||||
});
|
||||
}
|
||||
else {
|
||||
resultStream = es.through(null, function () {
|
||||
runNow();
|
||||
this.emit('end');
|
||||
});
|
||||
}
|
||||
return resultStream;
|
||||
}
|
163
build/lib/compilation.ts
Normal file
163
build/lib/compilation.ts
Normal file
|
@ -0,0 +1,163 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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;
|
||||
options.rootDir = rootDir;
|
||||
options.sourceRoot = util.toFileUri(rootDir);
|
||||
|
||||
function createCompile(build:boolean, emitError?:boolean): (token?:util.ICancellationToken) => NodeJS.ReadWriteStream {
|
||||
const opts = _.clone(options);
|
||||
opts.inlineSources = !!build;
|
||||
opts.noFilesystemLookup = true;
|
||||
|
||||
const ts = tsb.create(opts, null, null, err => reporter(err.toString()));
|
||||
|
||||
return function (token?:util.ICancellationToken) {
|
||||
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,
|
||||
sourceRoot: options.sourceRoot
|
||||
}))
|
||||
.pipe(tsFilter.restore)
|
||||
.pipe(reporter.end(emitError));
|
||||
|
||||
return es.duplex(input, output);
|
||||
};
|
||||
}
|
||||
|
||||
export function compileTask(out:string, build:boolean): () => NodeJS.ReadWriteStream {
|
||||
const compile = createCompile(build, true);
|
||||
|
||||
return function () {
|
||||
const src = es.merge(
|
||||
gulp.src('src/**', { base: 'src' }),
|
||||
gulp.src('node_modules/typescript/lib/lib.d.ts')
|
||||
);
|
||||
|
||||
return src
|
||||
.pipe(compile())
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, false));
|
||||
};
|
||||
}
|
||||
|
||||
export function watchTask(out:string, build:boolean): () => NodeJS.ReadWriteStream {
|
||||
const compile = createCompile(build);
|
||||
|
||||
return function () {
|
||||
const src = es.merge(
|
||||
gulp.src('src/**', { base: 'src' }),
|
||||
gulp.src('node_modules/typescript/lib/lib.d.ts')
|
||||
);
|
||||
const watchSrc = watch('src/**', { base: 'src' });
|
||||
|
||||
return watchSrc
|
||||
.pipe(util.incremental(compile, src, true))
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, true));
|
||||
};
|
||||
}
|
||||
|
||||
function monacodtsTask(out:string, isWatch:boolean): NodeJS.ReadWriteStream {
|
||||
let timer:NodeJS.Timer = null;
|
||||
|
||||
const runSoon = function(howSoon:number) {
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
timer = setTimeout(function() {
|
||||
timer = null;
|
||||
runNow();
|
||||
}, howSoon);
|
||||
};
|
||||
|
||||
const runNow = function() {
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
// if (reporter.hasErrors()) {
|
||||
// monacodts.complainErrors();
|
||||
// return;
|
||||
// }
|
||||
const result = monacodts.run(out);
|
||||
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) {
|
||||
|
||||
const filesToWatchMap: {[file:string]:boolean;} = {};
|
||||
monacodts.getFilesToWatch(out).forEach(function(filePath) {
|
||||
filesToWatchMap[path.normalize(filePath)] = true;
|
||||
});
|
||||
|
||||
watch('build/monaco/*').pipe(es.through(function() {
|
||||
runSoon(5000);
|
||||
}));
|
||||
|
||||
resultStream = es.through(function(data) {
|
||||
const filePath = path.normalize(data.path);
|
||||
if (filesToWatchMap[filePath]) {
|
||||
runSoon(5000);
|
||||
}
|
||||
this.emit('data', data);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
resultStream = es.through(null, function() {
|
||||
runNow();
|
||||
this.emit('end');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
return resultStream;
|
||||
}
|
|
@ -62,7 +62,7 @@ define([], [${ wrap + lines.map(l => indent + l).join(',\n') + wrap }]);`;
|
|||
/**
|
||||
* Returns a stream containing the patched JavaScript and source maps.
|
||||
*/
|
||||
function nls(): Stream {
|
||||
function nls(): NodeJS.ReadWriteStream {
|
||||
var input = through();
|
||||
var output = input.pipe(through(function (f: FileSourceMap) {
|
||||
if (!f.sourceMap) {
|
||||
|
|
|
@ -24,7 +24,7 @@ function onEnd() {
|
|||
errors.map(function (err) { return util.log(util.colors.red('Error') + ": " + err); });
|
||||
util.log("Finished " + util.colors.green('compilation') + " with " + errors.length + " errors after " + util.colors.magenta((new Date().getTime() - startTime) + ' ms'));
|
||||
}
|
||||
exports.createReporter = function () {
|
||||
function createReporter() {
|
||||
var errors = [];
|
||||
allErrors.push(errors);
|
||||
var ReportFunc = (function () {
|
||||
|
@ -50,4 +50,6 @@ exports.createReporter = function () {
|
|||
return ReportFunc;
|
||||
}());
|
||||
return ReportFunc;
|
||||
};
|
||||
}
|
||||
exports.createReporter = createReporter;
|
||||
;
|
||||
|
|
|
@ -39,7 +39,7 @@ export interface IReporter {
|
|||
end(emitError:boolean): NodeJS.ReadWriteStream;
|
||||
}
|
||||
|
||||
exports.createReporter = () => {
|
||||
export function createReporter(): IReporter {
|
||||
const errors:Error[] = [];
|
||||
allErrors.push(errors);
|
||||
|
||||
|
@ -68,5 +68,5 @@ exports.createReporter = () => {
|
|||
}
|
||||
}
|
||||
|
||||
return ReportFunc;
|
||||
return <IReporter><any>ReportFunc;
|
||||
};
|
||||
|
|
12
build/lib/typings/gulp-bom.d.ts
vendored
Normal file
12
build/lib/typings/gulp-bom.d.ts
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
declare module "gulp-bom" {
|
||||
function f(): NodeJS.ReadWriteStream;
|
||||
|
||||
/**
|
||||
* This is required as per:
|
||||
* https://github.com/Microsoft/TypeScript/issues/5073
|
||||
*/
|
||||
namespace f {}
|
||||
|
||||
export = f;
|
||||
}
|
16
build/lib/typings/gulp-tsb.d.ts
vendored
Normal file
16
build/lib/typings/gulp-tsb.d.ts
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
|
||||
declare module "gulp-tsb" {
|
||||
|
||||
export interface ICancellationToken {
|
||||
isCancellationRequested(): boolean;
|
||||
}
|
||||
|
||||
export interface IncrementalCompiler {
|
||||
(token?:ICancellationToken): NodeJS.ReadWriteStream;
|
||||
// program?: ts.Program;
|
||||
}
|
||||
|
||||
export function create(configOrName: { [option: string]: string | number | boolean; } | string, verbose?: boolean, json?: boolean, onError?: (message: any) => void): IncrementalCompiler;
|
||||
|
||||
}
|
159
gulpfile.js
159
gulpfile.js
|
@ -11,176 +11,27 @@ require('events').EventEmitter.defaultMaxListeners = 100;
|
|||
const gulp = require('gulp');
|
||||
const json = require('gulp-json-editor');
|
||||
const buffer = require('gulp-buffer');
|
||||
const tsb = require('gulp-tsb');
|
||||
const filter = require('gulp-filter');
|
||||
const mocha = require('gulp-mocha');
|
||||
const es = require('event-stream');
|
||||
const watch = require('./build/lib/watch');
|
||||
const nls = require('./build/lib/nls');
|
||||
const util = require('./build/lib/util');
|
||||
const reporter = require('./build/lib/reporter').createReporter();
|
||||
const remote = require('gulp-remote-src');
|
||||
const zip = require('gulp-vinyl-zip');
|
||||
const path = require('path');
|
||||
const bom = require('gulp-bom');
|
||||
const sourcemaps = require('gulp-sourcemaps');
|
||||
const _ = require('underscore');
|
||||
const assign = require('object-assign');
|
||||
const monacodts = require('./build/monaco/api');
|
||||
const fs = require('fs');
|
||||
const glob = require('glob');
|
||||
const pkg = require('./package.json');
|
||||
|
||||
const rootDir = path.join(__dirname, 'src');
|
||||
const options = require('./src/tsconfig.json').compilerOptions;
|
||||
options.verbose = false;
|
||||
options.sourceMap = true;
|
||||
options.rootDir = rootDir;
|
||||
options.sourceRoot = util.toFileUri(rootDir);
|
||||
|
||||
function createCompile(build, emitError) {
|
||||
const opts = _.clone(options);
|
||||
opts.inlineSources = !!build;
|
||||
opts.noFilesystemLookup = true;
|
||||
|
||||
const ts = tsb.create(opts, null, null, err => reporter(err.toString()));
|
||||
|
||||
return function (token) {
|
||||
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,
|
||||
sourceRoot: options.sourceRoot
|
||||
}))
|
||||
.pipe(tsFilter.restore)
|
||||
.pipe(reporter.end(emitError));
|
||||
|
||||
return es.duplex(input, output);
|
||||
};
|
||||
}
|
||||
|
||||
function compileTask(out, build) {
|
||||
const compile = createCompile(build, true);
|
||||
|
||||
return function () {
|
||||
const src = es.merge(
|
||||
gulp.src('src/**', { base: 'src' }),
|
||||
gulp.src('node_modules/typescript/lib/lib.d.ts')
|
||||
);
|
||||
|
||||
return src
|
||||
.pipe(compile())
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, false));
|
||||
};
|
||||
}
|
||||
|
||||
function watchTask(out, build) {
|
||||
const compile = createCompile(build);
|
||||
|
||||
return function () {
|
||||
const src = es.merge(
|
||||
gulp.src('src/**', { base: 'src' }),
|
||||
gulp.src('node_modules/typescript/lib/lib.d.ts')
|
||||
);
|
||||
const watchSrc = watch('src/**', { base: 'src' });
|
||||
|
||||
return watchSrc
|
||||
.pipe(util.incremental(compile, src, true))
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, true));
|
||||
};
|
||||
}
|
||||
|
||||
function monacodtsTask(out, isWatch) {
|
||||
let timer = -1;
|
||||
|
||||
const runSoon = function(howSoon) {
|
||||
if (timer !== -1) {
|
||||
clearTimeout(timer);
|
||||
timer = -1;
|
||||
}
|
||||
timer = setTimeout(function() {
|
||||
timer = -1;
|
||||
runNow();
|
||||
}, howSoon);
|
||||
};
|
||||
|
||||
const runNow = function() {
|
||||
if (timer !== -1) {
|
||||
clearTimeout(timer);
|
||||
timer = -1;
|
||||
}
|
||||
// if (reporter.hasErrors()) {
|
||||
// monacodts.complainErrors();
|
||||
// return;
|
||||
// }
|
||||
const result = monacodts.run(out);
|
||||
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;
|
||||
|
||||
if (isWatch) {
|
||||
|
||||
const filesToWatchMap = {};
|
||||
monacodts.getFilesToWatch(out).forEach(function(filePath) {
|
||||
filesToWatchMap[path.normalize(filePath)] = true;
|
||||
});
|
||||
|
||||
watch('build/monaco/*').pipe(es.through(function() {
|
||||
runSoon(5000);
|
||||
}));
|
||||
|
||||
resultStream = es.through(function(data) {
|
||||
const filePath = path.normalize(data.path);
|
||||
if (filesToWatchMap[filePath]) {
|
||||
runSoon(5000);
|
||||
}
|
||||
this.emit('data', data);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
resultStream = es.through(null, function() {
|
||||
runNow();
|
||||
this.emit('end');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
return resultStream;
|
||||
}
|
||||
const compilation = require('./build/lib/compilation');
|
||||
|
||||
// Fast compile for development time
|
||||
gulp.task('clean-client', util.rimraf('out'));
|
||||
gulp.task('compile-client', ['clean-client'], compileTask('out', false));
|
||||
gulp.task('watch-client', ['clean-client'], watchTask('out', false));
|
||||
gulp.task('compile-client', ['clean-client'], compilation.compileTask('out', false));
|
||||
gulp.task('watch-client', ['clean-client'], compilation.watchTask('out', false));
|
||||
|
||||
// Full compile, including nls and inline sources in sourcemaps, for build
|
||||
gulp.task('clean-client-build', util.rimraf('out-build'));
|
||||
gulp.task('compile-client-build', ['clean-client-build'], compileTask('out-build', true));
|
||||
gulp.task('watch-client-build', ['clean-client-build'], watchTask('out-build', true));
|
||||
gulp.task('compile-client-build', ['clean-client-build'], compilation.compileTask('out-build', true));
|
||||
gulp.task('watch-client-build', ['clean-client-build'], compilation.watchTask('out-build', true));
|
||||
|
||||
// Default
|
||||
gulp.task('default', ['compile']);
|
||||
|
|
Loading…
Reference in a new issue