build(broccoli-typescript): do a global emit during the first run
reduces the time spent in TSC for the initial build to 1 sec (down from 23sec).
This commit is contained in:
parent
37a8f1037e
commit
c54f5e0ba2
@ -29,6 +29,7 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
|
|||||||
private rootFilePaths: string[];
|
private rootFilePaths: string[];
|
||||||
private tsServiceHost: ts.LanguageServiceHost;
|
private tsServiceHost: ts.LanguageServiceHost;
|
||||||
private tsService: ts.LanguageService;
|
private tsService: ts.LanguageService;
|
||||||
|
private firstRun: boolean = true;
|
||||||
|
|
||||||
static includeExtensions = ['.ts'];
|
static includeExtensions = ['.ts'];
|
||||||
static excludeExtensions = ['.d.ts'];
|
static excludeExtensions = ['.d.ts'];
|
||||||
@ -76,6 +77,30 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
|
|||||||
fs.unlinkSync(path.join(this.cachePath, dtsFilePath));
|
fs.unlinkSync(path.join(this.cachePath, dtsFilePath));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (this.firstRun) {
|
||||||
|
this.firstRun = false;
|
||||||
|
let program = this.tsService.getProgram();
|
||||||
|
let emitResult = program.emit(undefined, function(absoluteFilePath, fileContent) {
|
||||||
|
fse.mkdirsSync(path.dirname(absoluteFilePath));
|
||||||
|
fs.writeFileSync(absoluteFilePath, fileContent, FS_OPTS);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (emitResult.emitSkipped) {
|
||||||
|
let allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
||||||
|
let errorMessages = [];
|
||||||
|
|
||||||
|
allDiagnostics.forEach(diagnostic => {
|
||||||
|
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
||||||
|
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
||||||
|
errorMessages.push(` ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (errorMessages.length) {
|
||||||
|
console.log(errorMessages.join('\n'));
|
||||||
|
throw new Error('Typescript found errors listed above...');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
pathsToEmit.forEach((tsFilePath) => {
|
pathsToEmit.forEach((tsFilePath) => {
|
||||||
let output = this.tsService.getEmitOutput(tsFilePath);
|
let output = this.tsService.getEmitOutput(tsFilePath);
|
||||||
|
|
||||||
@ -97,6 +122,7 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
|
|||||||
throw new Error('Typescript found errors listed above...');
|
throw new Error('Typescript found errors listed above...');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private logError(tsFilePath) {
|
private logError(tsFilePath) {
|
||||||
@ -140,9 +166,24 @@ class CustomLanguageServiceHost implements ts.LanguageServiceHost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called quite a bit to lookup 3 kinds of paths:
|
||||||
|
* 1/ files in the fileRegistry
|
||||||
|
* - these are the files in our project that we are watching for changes
|
||||||
|
* - in the future we could add caching for these files and invalidate the cache when
|
||||||
|
* the file is changed lazily during lookup
|
||||||
|
* 2/ .d.ts and library files not in the fileRegistry
|
||||||
|
* - these are not our files, they come from tsd or typescript itself
|
||||||
|
* - these files change only rarely but since we need them very rarely, it's not worth the
|
||||||
|
* cache invalidation hassle to cache them
|
||||||
|
* 3/ bogus paths that typescript compiler tries to lookup during import resolution
|
||||||
|
* - these paths are tricky to cache since files come and go and paths that was bogus in the
|
||||||
|
* past might not be bogus later
|
||||||
|
*
|
||||||
|
* In the initial experiments the impact of this caching was insignificant (single digit %) and
|
||||||
|
* not worth the potential issues with stale cache records.
|
||||||
|
*/
|
||||||
getScriptSnapshot(tsFilePath: string): ts.IScriptSnapshot {
|
getScriptSnapshot(tsFilePath: string): ts.IScriptSnapshot {
|
||||||
// TODO: this method is called a lot, add cache
|
|
||||||
|
|
||||||
let absoluteTsFilePath = (tsFilePath == this.defaultLibFilePath) ?
|
let absoluteTsFilePath = (tsFilePath == this.defaultLibFilePath) ?
|
||||||
tsFilePath :
|
tsFilePath :
|
||||||
path.join(this.treeInputPath, tsFilePath);
|
path.join(this.treeInputPath, tsFilePath);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user