From b92b236d5e27821c2829647ad3afec3be2675543 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Mon, 6 Jun 2016 17:10:43 -0700 Subject: [PATCH] fix(sourcemaps): try to improve the source maps by fixing the path --- lib/broccoli/broccoli-typescript.js | 32 +++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/broccoli/broccoli-typescript.js b/lib/broccoli/broccoli-typescript.js index 8ab914c3ef0f..3b01217802fc 100644 --- a/lib/broccoli/broccoli-typescript.js +++ b/lib/broccoli/broccoli-typescript.js @@ -195,6 +195,7 @@ class BroccoliTypeScriptCompiler extends Plugin { _outputFile(absoluteFilePath, fileContent, registry) { absoluteFilePath = path.resolve(this.cachePath, absoluteFilePath); + let inputFilePath = absoluteFilePath; // Replace the input path by the output. absoluteFilePath = absoluteFilePath.replace(this.inputPaths[0], this.cachePath); const outputFilePath = absoluteFilePath.replace(this.cachePath, this.outputPath); @@ -204,7 +205,7 @@ class BroccoliTypeScriptCompiler extends Plugin { } fse.mkdirsSync(path.dirname(absoluteFilePath)); - const content = this.fixSourceMapSources(fileContent); + const content = this.fixSourceMapSources(fileContent, inputFilePath); fs.writeFileSync(absoluteFilePath, content, FS_OPTS); fse.mkdirsSync(path.dirname(outputFilePath)); @@ -242,12 +243,31 @@ class BroccoliTypeScriptCompiler extends Plugin { * This issue is fixed in https://github.com/Microsoft/TypeScript/pull/5620. * Once we switch to TypeScript 1.8, we can remove this method. */ - fixSourceMapSources(content) { + fixSourceMapSources(content, inputFilePath) { try { - var marker = '//# sourceMappingURL=data:application/json;base64,'; - var index = content.indexOf(marker); - if (index == -1) - return content; + const marker = '//# sourceMappingURL=data:application/json;base64,'; + + let index = content.indexOf(marker); + if (index == -1) { + const pathMarker = '//# sourceMappingURL='; + index = content.indexOf(pathMarker); + if (index == -1) { + return content; + } + + // We have a regular path, make it relative to the input path. + let base = content.substring(0, index + pathMarker.length); + let mapPath = content.substring(index + pathMarker.length); + if (mapPath.startsWith(this.outputPath)) { + mapPath = mapPath.replace(this.outputPath, this.inputPaths[0]); + } else if (!mapPath.startsWith(this.inputPaths[0])) { + mapPath = path.join(this.inputPaths[0], path.dirname(this._tsConfigPath), mapPath); + } + + mapPath = path.relative(path.dirname(inputFilePath), mapPath); + return '' + base + mapPath; + } + var base = content.substring(0, index + marker.length); var sourceMapBit = new Buffer(content.substring(index + marker.length), 'base64').toString('utf8'); var sourceMaps = JSON.parse(sourceMapBit);