|
| 1 | +/// <reference path="../typings/node/node.d.ts" /> |
| 2 | +/// <reference path="../../node_modules/typescript/bin/typescript.d.ts" /> |
| 3 | + |
| 4 | +import fs = require('fs'); |
| 5 | +import fse = require('fs-extra'); |
| 6 | +import path = require('path'); |
| 7 | +import ts = require('typescript'); |
| 8 | +import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin'; |
| 9 | + |
| 10 | + |
| 11 | +type FileRegistry = ts.Map<{version: number}>; |
| 12 | + |
| 13 | +const FS_OPTS = {encoding: 'utf-8'}; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Broccoli plugin that implements incremental Typescript compiler. |
| 18 | + * |
| 19 | + * It instantiates a typescript compiler instance that keeps all the state about the project and |
| 20 | + * can reemit only the files that actually changed. |
| 21 | + * |
| 22 | + * Limitations: only files that map directly to the changed source file via naming conventions are |
| 23 | + * reemited. This primarily affects code that uses `const enum`s, because changing the enum value |
| 24 | + * requires global emit, which can affect many files. |
| 25 | + */ |
| 26 | +class DiffingTSCompiler implements DiffingBroccoliPlugin { |
| 27 | + private tsOpts: ts.CompilerOptions; |
| 28 | + private fileRegistry: FileRegistry = Object.create(null); |
| 29 | + private rootFilePaths: string[]; |
| 30 | + private tsServiceHost: ts.LanguageServiceHost; |
| 31 | + private tsService: ts.LanguageService; |
| 32 | + |
| 33 | + |
| 34 | + constructor(public inputPath: string, public cachePath: string, public options) { |
| 35 | + this.tsOpts = Object.create(options); |
| 36 | + this.tsOpts.outDir = this.cachePath; |
| 37 | + this.tsOpts.target = (<any>ts).ScriptTarget[options.target]; |
| 38 | + this.rootFilePaths = options.rootFilePaths ? options.rootFilePaths.splice(0) : []; |
| 39 | + this.tsServiceHost = new CustomLanguageServiceHost(this.tsOpts, this.rootFilePaths, |
| 40 | + this.fileRegistry, this.inputPath); |
| 41 | + this.tsService = ts.createLanguageService(this.tsServiceHost, ts.createDocumentRegistry()) |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + rebuild(treeDiff: DiffResult) { |
| 46 | + let pathsToEmit = []; |
| 47 | + let pathsWithErrors = []; |
| 48 | + |
| 49 | + treeDiff.changedPaths.filter((changedPath) => |
| 50 | + changedPath.match(/\.ts/) && !changedPath.match(/\.d\.ts/)) |
| 51 | + .forEach((tsFilePath) => { |
| 52 | + if (!this.fileRegistry[tsFilePath]) { |
| 53 | + this.fileRegistry[tsFilePath] = {version: 0}; |
| 54 | + this.rootFilePaths.push(tsFilePath); |
| 55 | + } else { |
| 56 | + this.fileRegistry[tsFilePath].version++; |
| 57 | + } |
| 58 | + |
| 59 | + pathsToEmit.push(tsFilePath); |
| 60 | + }); |
| 61 | + |
| 62 | + treeDiff.removedPaths.filter((changedPath) => |
| 63 | + changedPath.match(/\.ts/) && !changedPath.match(/\.d\.ts/)) |
| 64 | + .forEach((tsFilePath) => { |
| 65 | + console.log('removing outputs for', tsFilePath); |
| 66 | + |
| 67 | + this.rootFilePaths.splice(this.rootFilePaths.indexOf(tsFilePath), 1); |
| 68 | + this.fileRegistry[tsFilePath] = null; |
| 69 | + |
| 70 | + let jsFilePath = tsFilePath.replace(/\.ts$/, '.js'); |
| 71 | + let mapFilePath = tsFilePath.replace(/.ts$/, '.js.map'); |
| 72 | + let dtsFilePath = tsFilePath.replace(/\.ts$/, '.d.ts'); |
| 73 | + |
| 74 | + fs.unlinkSync(path.join(this.cachePath, jsFilePath)); |
| 75 | + fs.unlinkSync(path.join(this.cachePath, mapFilePath)); |
| 76 | + fs.unlinkSync(path.join(this.cachePath, dtsFilePath)); |
| 77 | + }); |
| 78 | + |
| 79 | + pathsToEmit.forEach((tsFilePath) => { |
| 80 | + let output = this.tsService.getEmitOutput(tsFilePath); |
| 81 | + |
| 82 | + if (output.emitSkipped) { |
| 83 | + let errorFound = this.logError(tsFilePath); |
| 84 | + if (errorFound) { |
| 85 | + pathsWithErrors.push(tsFilePath); |
| 86 | + } |
| 87 | + } else { |
| 88 | + output.outputFiles.forEach(o => { |
| 89 | + let destDirPath = path.dirname(o.name); |
| 90 | + fse.mkdirsSync(destDirPath); |
| 91 | + fs.writeFileSync(o.name, o.text, FS_OPTS); |
| 92 | + }); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + if (pathsWithErrors.length) { |
| 97 | + throw new Error('Typescript found errors listed above...'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + private logError(tsFilePath) { |
| 103 | + let allDiagnostics = this.tsService.getCompilerOptionsDiagnostics() |
| 104 | + .concat(this.tsService.getSyntacticDiagnostics(tsFilePath)) |
| 105 | + .concat(this.tsService.getSemanticDiagnostics(tsFilePath)); |
| 106 | + |
| 107 | + allDiagnostics.forEach(diagnostic => { |
| 108 | + let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); |
| 109 | + if (diagnostic.file) { |
| 110 | + let{line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); |
| 111 | + console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ` + |
| 112 | + `${message}`); |
| 113 | + } else { |
| 114 | + console.log(` Error: ${message}`); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + return !!allDiagnostics.length; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +class CustomLanguageServiceHost implements ts.LanguageServiceHost { |
| 124 | + private currentDirectory: string; |
| 125 | + private defaultLibFilePath: string; |
| 126 | + |
| 127 | + |
| 128 | + constructor(private compilerOptions: ts.CompilerOptions, private fileNames: string[], |
| 129 | + private fileRegistry: FileRegistry, private treeInputPath: string) { |
| 130 | + this.currentDirectory = process.cwd(); |
| 131 | + this.defaultLibFilePath = ts.getDefaultLibFilePath(compilerOptions); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + getScriptFileNames(): string[] { return this.fileNames; } |
| 136 | + |
| 137 | + |
| 138 | + getScriptVersion(fileName: string): string { |
| 139 | + return this.fileRegistry[fileName] && this.fileRegistry[fileName].version.toString(); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + getScriptSnapshot(tsFilePath: string): ts.IScriptSnapshot { |
| 144 | + // TODO: this method is called a lot, add cache |
| 145 | + |
| 146 | + let absoluteTsFilePath = (tsFilePath == this.defaultLibFilePath) ? |
| 147 | + tsFilePath : |
| 148 | + path.join(this.treeInputPath, tsFilePath); |
| 149 | + |
| 150 | + if (!fs.existsSync(absoluteTsFilePath)) { |
| 151 | + // TypeScript seems to request lots of bogus paths during import path lookup and resolution, |
| 152 | + // so we we just return undefined when the path is not correct. |
| 153 | + return undefined; |
| 154 | + } |
| 155 | + return ts.ScriptSnapshot.fromString(fs.readFileSync(absoluteTsFilePath, FS_OPTS)); |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + getCurrentDirectory(): string { return this.currentDirectory; } |
| 160 | + |
| 161 | + |
| 162 | + getCompilationSettings(): ts.CompilerOptions { return this.compilerOptions; } |
| 163 | + |
| 164 | + |
| 165 | + getDefaultLibFileName(options: ts.CompilerOptions): string { |
| 166 | + // ignore options argument, options should not change during the lifetime of the plugin |
| 167 | + return this.defaultLibFilePath; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +export default wrapDiffingPlugin(DiffingTSCompiler); |
0 commit comments