Skip to content

Commit

Permalink
fix(sourcemaps): try to improve the source maps by fixing the path
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Jun 7, 2016
1 parent 657a87a commit b92b236
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/broccoli/broccoli-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b92b236

Please sign in to comment.