Skip to content

Commit b92b236

Browse files
committed
fix(sourcemaps): try to improve the source maps by fixing the path
1 parent 657a87a commit b92b236

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

lib/broccoli/broccoli-typescript.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class BroccoliTypeScriptCompiler extends Plugin {
195195

196196
_outputFile(absoluteFilePath, fileContent, registry) {
197197
absoluteFilePath = path.resolve(this.cachePath, absoluteFilePath);
198+
let inputFilePath = absoluteFilePath;
198199
// Replace the input path by the output.
199200
absoluteFilePath = absoluteFilePath.replace(this.inputPaths[0], this.cachePath);
200201
const outputFilePath = absoluteFilePath.replace(this.cachePath, this.outputPath);
@@ -204,7 +205,7 @@ class BroccoliTypeScriptCompiler extends Plugin {
204205
}
205206

206207
fse.mkdirsSync(path.dirname(absoluteFilePath));
207-
const content = this.fixSourceMapSources(fileContent);
208+
const content = this.fixSourceMapSources(fileContent, inputFilePath);
208209
fs.writeFileSync(absoluteFilePath, content, FS_OPTS);
209210

210211
fse.mkdirsSync(path.dirname(outputFilePath));
@@ -242,12 +243,31 @@ class BroccoliTypeScriptCompiler extends Plugin {
242243
* This issue is fixed in https://github.com/Microsoft/TypeScript/pull/5620.
243244
* Once we switch to TypeScript 1.8, we can remove this method.
244245
*/
245-
fixSourceMapSources(content) {
246+
fixSourceMapSources(content, inputFilePath) {
246247
try {
247-
var marker = '//# sourceMappingURL=data:application/json;base64,';
248-
var index = content.indexOf(marker);
249-
if (index == -1)
250-
return content;
248+
const marker = '//# sourceMappingURL=data:application/json;base64,';
249+
250+
let index = content.indexOf(marker);
251+
if (index == -1) {
252+
const pathMarker = '//# sourceMappingURL=';
253+
index = content.indexOf(pathMarker);
254+
if (index == -1) {
255+
return content;
256+
}
257+
258+
// We have a regular path, make it relative to the input path.
259+
let base = content.substring(0, index + pathMarker.length);
260+
let mapPath = content.substring(index + pathMarker.length);
261+
if (mapPath.startsWith(this.outputPath)) {
262+
mapPath = mapPath.replace(this.outputPath, this.inputPaths[0]);
263+
} else if (!mapPath.startsWith(this.inputPaths[0])) {
264+
mapPath = path.join(this.inputPaths[0], path.dirname(this._tsConfigPath), mapPath);
265+
}
266+
267+
mapPath = path.relative(path.dirname(inputFilePath), mapPath);
268+
return '' + base + mapPath;
269+
}
270+
251271
var base = content.substring(0, index + marker.length);
252272
var sourceMapBit = new Buffer(content.substring(index + marker.length), 'base64').toString('utf8');
253273
var sourceMaps = JSON.parse(sourceMapBit);

0 commit comments

Comments
 (0)