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O,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,MAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IAC5E,CAAC;IAEO,2BAAQ,GAAhB,UAAiB,GAAW,EAAE,MAAc;QACxC,IAAM,WAAW,GAAG,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC/C,OAAO,WAAW,IAAI,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,WAAW,CAAC,KAAK,WAAW,CAAC;IAChF,CAAC;IACL,eAAC;AAAD,CAAC,AAlLD,IAkLC;AAlLY,4BAAQ","sourcesContent":["import * as lodash from \"lodash\";\nimport * as ts from \"typescript\";\n\nimport { Logger } from \"log4js\";\n\nimport { Benchmark } from \"../shared/benchmark\";\nimport { Configuration } from \"../shared/configuration\";\nimport { File } from \"../shared/file\";\nimport { EventType, Project } from \"../shared/project\";\nimport { CompileCallback } from \"./compile-callback\";\n\ninterface CompiledFiles { [key: string]: string; }\n\ninterface Queued {\n file: File;\n callback: CompileCallback;\n}\n\nexport class Compiler {\n\n private cachedProgram: ts.Program;\n private compiledFiles: CompiledFiles = {};\n private compilerHost: ts.CompilerHost;\n private emitQueue: Queued[] = [];\n private errors: string[] = [];\n private hostGetSourceFile: (filename: string, languageVersion: ts.ScriptTarget,\n onError?: (message: string) => void) => ts.SourceFile;\n private program: ts.Program;\n\n private compileDeferred: () => void;\n\n constructor(private config: Configuration, private log: Logger, private project: Project) {\n config.whenReady(() => {\n this.log.debug(\"Setting up deferred project compilation\");\n this.compileDeferred = lodash.debounce(() => {\n this.compileProject();\n }, this.config.compilerDelay);\n });\n }\n\n public compile(file: File, callback: CompileCallback): void {\n\n this.emitQueue.push({\n callback,\n file\n });\n\n this.compileDeferred();\n }\n\n private compileProject(): void {\n\n this.log.info(\"Compiling project using Typescript %s\", ts.version);\n\n if (this.project.handleFileEvent() === EventType.FileSystemChanged) {\n this.setupRecompile();\n }\n\n const benchmark = new Benchmark();\n const tsconfig = this.project.getTsconfig();\n\n this.outputDiagnostics(tsconfig.errors);\n\n if (+ts.version[0] >= 3) {\n this.program = ts.createProgram({\n host: this.compilerHost,\n options: tsconfig.options,\n projectReferences: tsconfig.projectReferences,\n rootNames: tsconfig.fileNames\n });\n }\n else {\n this.program = ts.createProgram(tsconfig.fileNames, tsconfig.options, this.compilerHost);\n }\n\n this.cachedProgram = this.program;\n\n this.runDiagnostics(this.program, this.compilerHost);\n this.program.emit();\n this.log.info(\"Compiled %s files in %s ms.\", tsconfig.fileNames.length, benchmark.elapsed());\n this.onProgramCompiled();\n }\n\n private setupRecompile(): void {\n this.cachedProgram = undefined;\n this.compilerHost = ts.createCompilerHost(this.project.getTsconfig().options);\n this.hostGetSourceFile = this.compilerHost.getSourceFile;\n this.compilerHost.getSourceFile = this.getSourceFile;\n this.compilerHost.writeFile = (filename, text) => {\n this.compiledFiles[filename] = text;\n };\n }\n\n private onProgramCompiled(): void {\n\n this.emitQueue.forEach((queued) => {\n\n const sourceFile = this.program.getSourceFile(queued.file.originalPath);\n\n if (!sourceFile) {\n throw new Error(\"No source found for \" + queued.file.originalPath + \"!\\n\" +\n \"Is there a mismatch between the Typescript compiler options and the Karma config?\");\n }\n\n const ambientModuleNames = (sourceFile as any).ambientModuleNames;\n\n queued.callback({\n ambientModuleNames,\n hasError: this.errors.indexOf(queued.file.originalPath) !== -1,\n isAmbientModule: ambientModuleNames && ambientModuleNames.length > 0,\n isDeclarationFile: this.fileExtensionIs(sourceFile.fileName, \".d.ts\"),\n outputText: this.compiledFiles[queued.file.path],\n sourceFile,\n sourceMapText: this.compiledFiles[queued.file.path + \".map\"]\n });\n });\n\n this.emitQueue.length = 0;\n }\n\n private getSourceFile = (\n filename: string,\n languageVersion: ts.ScriptTarget,\n onError?: (message: string) => void): ts.SourceFile => {\n\n if (this.cachedProgram && !this.isQueued(filename)) {\n const sourceFile = this.cachedProgram.getSourceFile(filename);\n if (sourceFile) {\n return sourceFile;\n }\n }\n\n return this.hostGetSourceFile(filename, languageVersion, onError);\n }\n\n private isQueued(filename: string): boolean {\n for (const queued of this.emitQueue) {\n if (queued.file.originalPath === filename) {\n return true;\n }\n }\n return false;\n }\n\n private runDiagnostics(program: ts.Program, host: ts.CompilerHost): void {\n this.errors = [];\n const diagnostics = ts.getPreEmitDiagnostics(program);\n this.outputDiagnostics(diagnostics, host);\n }\n\n private outputDiagnostics(diagnostics: ReadonlyArray<ts.Diagnostic>, host?: ts.FormatDiagnosticsHost): void {\n\n if (!diagnostics || diagnostics.length === 0) {\n return;\n }\n\n diagnostics.forEach((diagnostic) => {\n\n if (diagnostic.file) {\n this.errors.push(diagnostic.file.fileName);\n }\n\n if (ts.formatDiagnostics && host) { // v1.8+\n this.log.error(ts.formatDiagnostics([diagnostic], host));\n }\n else { // v1.6, v1.7\n\n let output = \"\";\n\n if (diagnostic.file) {\n const loc = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);\n output += diagnostic.file.fileName.replace(process.cwd(), \"\") +\n \"(\" + (loc.line + 1) + \",\" + (loc.character + 1) + \"): \";\n }\n\n const category = ts.DiagnosticCategory[diagnostic.category].toLowerCase();\n output += category + \" TS\" + diagnostic.code + \": \" +\n ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine) + ts.sys.newLine;\n\n this.log.error(output);\n }\n });\n\n if (this.project.getTsconfig().options.noEmitOnError) {\n ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);\n }\n }\n\n private fileExtensionIs(path: string, extension: string) {\n return path.length > extension.length && this.endsWith(path, extension);\n }\n\n private endsWith(str: string, suffix: string) {\n const expectedPos = str.length - suffix.length;\n return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos;\n }\n}\n"]}