This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix incorrect concatenation of source maps
- Loading branch information
1 parent
2649e18
commit a49584b
Showing
2 changed files
with
48 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,36 @@ | ||
var sourceMap = require('source-map'); | ||
var SourceNode = sourceMap.SourceNode; | ||
var SourceMapConsumer = sourceMap.SourceMapConsumer; | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
|
||
var toFileURL = require('./utils').toFileURL; | ||
var fromFileURL = require('./utils').fromFileURL; | ||
exports.concatenate = function(files, outFile) { | ||
var concatenated = new SourceNode(); | ||
|
||
var wrapSourceMap = function(map) { | ||
return new sourceMap.SourceMapConsumer(map); | ||
}; | ||
|
||
var sourceMapRegEx = /\/\/[@#] ?(sourceURL|sourceMappingURL)=([^\n'"]+)/; | ||
exports.removeSourceMaps = function(source) { | ||
return source.replace(sourceMapRegEx, ''); | ||
}; | ||
|
||
function getMapObject(map) { | ||
if (typeof map != 'string') | ||
return map; | ||
|
||
try { | ||
return JSON.parse(map); | ||
} | ||
catch(error) { | ||
throw new Error('Invalid JSON: ' + map); | ||
} | ||
} | ||
|
||
function isFileURL(url) { | ||
return url.substr(0, 8) == 'file:///'; | ||
} | ||
|
||
exports.concatenateSourceMaps = function(outFile, mapsWithOffsets, basePath, sourceMapContents) { | ||
var generated = new sourceMap.SourceMapGenerator({ | ||
file: path.basename(outFile) | ||
}); | ||
|
||
var outPath = path.dirname(outFile); | ||
|
||
var contentsBySource = sourceMapContents ? {} : null; | ||
|
||
mapsWithOffsets.forEach(function(pair) { | ||
var offset = pair[0]; | ||
var map = getMapObject(pair[1]); | ||
|
||
if (sourceMapContents && map.sourcesContent) { | ||
for (var i=0; i<map.sources.length; i++) { | ||
var source = (map.sourceRoot || '') + map.sources[i]; | ||
if (!source.match(/\/@traceur/)) { | ||
if (!contentsBySource[source]) { | ||
contentsBySource[source] = map.sourcesContent[i]; | ||
} else { | ||
if (contentsBySource[source] != map.sourcesContent[i]) { | ||
throw new Error("Mismatched sourcesContent for: " + source); | ||
} | ||
} | ||
} | ||
} | ||
files.forEach(function (file, index) { | ||
if (index !== 0) { | ||
concatenated.add("\n"); | ||
} | ||
|
||
wrapSourceMap(map).eachMapping(function(mapping) { | ||
if (!mapping.originalLine || !mapping.originalColumn || !mapping.source || mapping.source.match(/(\/|^)@traceur/)) | ||
return; | ||
|
||
generated.addMapping({ | ||
generated: { | ||
line: offset + mapping.generatedLine, | ||
column: mapping.generatedColumn | ||
}, | ||
original: { | ||
line: mapping.originalLine, | ||
column: mapping.originalColumn | ||
}, | ||
source: mapping.source, | ||
name: mapping.name | ||
}); | ||
}); | ||
}); | ||
|
||
// normalize source paths and inject sourcesContent if necessary | ||
var normalized = JSON.parse(JSON.stringify(generated)); | ||
|
||
if (sourceMapContents) { | ||
normalized.sourcesContent = normalized.sources.map(function(source) { | ||
if (contentsBySource[source]) | ||
return contentsBySource[source]; | ||
|
||
try { | ||
return fs.readFileSync(path.resolve(basePath, source)).toString(); | ||
} | ||
catch (e) { | ||
return ""; | ||
var node; | ||
var map = file.map; | ||
if (map) { | ||
if (typeof map.toJSON === "function") { | ||
map = map.toJSON(); | ||
} | ||
}); | ||
} | ||
|
||
normalized.sources = normalized.sources.map(function(source) { | ||
if (isFileURL(source)) | ||
source = fromFileURL(source); | ||
node = SourceNode.fromStringWithSourceMap( | ||
file.code, | ||
new SourceMapConsumer(map), | ||
path.relative( | ||
path.dirname(outFile + ".map"), | ||
path.dirname(".") | ||
) | ||
); | ||
} else { | ||
node = new SourceNode(null, null, null, file.code); | ||
} | ||
|
||
return path.relative(outPath, path.resolve(basePath, source)).replace(/\\/g, '/'); | ||
concatenated.add(node); | ||
}); | ||
|
||
return JSON.stringify(normalized); | ||
return concatenated; | ||
}; |