Skip to content

Commit 78ad469

Browse files
fimius23michael-ciniawsky
authored andcommitted
fix(index): handle exception on loading invalid source maps (#67)
1 parent a0b84d4 commit 78ad469

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ module.exports = function(input, inputMap) {
5050
emitWarning("Cannot open SourceMap '" + result + "': " + err);
5151
return untouched();
5252
}
53-
processMap(JSON.parse(content), path.dirname(result), callback);
53+
var map;
54+
try {
55+
map = JSON.parse(content);
56+
} catch (e) {
57+
emitWarning("Cannot parse SourceMap '" + url + "': " + e);
58+
return untouched();
59+
}
60+
processMap(map, path.dirname(result), callback);
5461
});
5562
}.bind(this));
5663
return;

test/fixtures/invalid-source-map.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
//#sourceMappingURL=invalid-source-map.map
3+
// comment

test/fixtures/invalid-source-map.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change

test/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ describe("source-map-loader", function() {
149149
done();
150150
});
151151
});
152+
it("should warn on invalid SourceMap", function (done) {
153+
execLoader(path.join(__dirname, "fixtures", "invalid-source-map.js"), function (err, res, map, deps, warns) {
154+
should.equal(err, null);
155+
warns.should.matchEach(
156+
new RegExp("Cannot parse SourceMap 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102")
157+
);
158+
should.equal(res, "with SourceMap\n//#sourceMappingURL=invalid-source-map.map\n// comment");
159+
should.equal(map, null);
160+
deps.should.be.eql([
161+
path.join(__dirname, "fixtures", "invalid-source-map.map")
162+
]);
163+
done();
164+
});
165+
});
152166
it("should warn on missing SourceMap", function(done) {
153167
execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) {
154168
should.equal(err, null);

0 commit comments

Comments
 (0)