Skip to content

Commit

Permalink
Resolve sources from map file path for external maps
Browse files Browse the repository at this point in the history
  • Loading branch information
gotwarlost committed Jun 30, 2015
1 parent ea0c312 commit 2b2d868
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/source-maps.js
Expand Up @@ -15,12 +15,24 @@ function loadRawMapFromFile(file) {

function rawMapFromUrl(url, base) {
url = (url || '').toString();
var match = DATA_URI_RE.exec(url);
var match = DATA_URI_RE.exec(url),
contents,
baseDir = base,
file;

if (match) {
return new Buffer(match[1], 'base64').toString();
contents = new Buffer(match[1], 'base64').toString();
} else {
file = path.resolve(base, url);
contents = loadRawMapFromFile(file);
baseDir = path.dirname(file);
}
if (contents) {
return {
contents: contents,
baseDir: baseDir
};
}
return loadRawMapFromFile(path.resolve(base, url));
}

function SourceMapCache() {
Expand All @@ -29,13 +41,21 @@ function SourceMapCache() {

SourceMapCache.prototype = {
addUrl: function (file, sourceMappingUrl) {
var contents = rawMapFromUrl(sourceMappingUrl, path.dirname(file));
if (contents) {
var obj;
var baseDir = path.dirname(file),
ret = rawMapFromUrl(sourceMappingUrl, baseDir);
if (ret) {
try {
this.addRawMap(file, JSON.parse(contents));
obj = JSON.parse(ret.contents);
} catch (ex) {
console.error('Unable to parse JSON for source map');
}
if (Array.isArray(obj.sources)) {
obj.sources = obj.sources.map(function (s) {
return path.resolve(ret.baseDir, s);
});
}
this.addRawMap(file, obj);
}
},
addRawMap: function (file, sourceMapping) {
Expand Down

0 comments on commit 2b2d868

Please sign in to comment.