diff --git a/packages/@ngtools/webpack/src/refactor.ts b/packages/@ngtools/webpack/src/refactor.ts index 57fc1449fb21..b1af6647d799 100644 --- a/packages/@ngtools/webpack/src/refactor.ts +++ b/packages/@ngtools/webpack/src/refactor.ts @@ -1,5 +1,6 @@ // TODO: move this in its own package. import * as path from 'path'; +import * as fs from 'fs'; import * as ts from 'typescript'; import {SourceMapConsumer, SourceMapGenerator} from 'source-map'; @@ -205,6 +206,10 @@ export class TypeScriptFileRefactor { } transpile(compilerOptions: ts.CompilerOptions): TranspileOutput { + if (this._fileName.endsWith('.d.ts') + && this._fileName.split(path.sep).indexOf('node_modules') >= 0) { + return this._getCompiledFromPackage(); + } const source = this.sourceText; const result = ts.transpileModule(source, { compilerOptions: Object.assign({}, compilerOptions, { @@ -247,4 +252,17 @@ export class TypeScriptFileRefactor { }; } } + + _getCompiledFromPackage(): TranspileOutput { + const basePath: string = path.dirname(this._fileName), + packageJson = require(path.join(basePath, 'package.json')), + precompiledFileName = path.join(basePath, packageJson.es2015), + output = fs.readFileSync(precompiledFileName, 'utf8'), + sourceMap = fs.readFileSync(precompiledFileName + '.map', 'utf8'); + + return { + outputText: output, + sourceMap: sourceMap + }; + } }