diff --git a/index.js b/index.js index 5f9fe91..5419ac3 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,14 @@ module.exports = function(input, inputMap) { emitWarning("Cannot open SourceMap '" + result + "': " + err); return untouched(); } - processMap(JSON.parse(content), path.dirname(result), callback); + var map; + try { + map = JSON.parse(content); + } catch (e) { + emitWarning("Cannot parse SourceMap '" + url + "': " + e); + return untouched(); + } + processMap(map, path.dirname(result), callback); }); }.bind(this)); return; diff --git a/test/fixtures/invalid-source-map.js b/test/fixtures/invalid-source-map.js new file mode 100644 index 0000000..bd24048 --- /dev/null +++ b/test/fixtures/invalid-source-map.js @@ -0,0 +1,3 @@ +with SourceMap +//#sourceMappingURL=invalid-source-map.map +// comment \ No newline at end of file diff --git a/test/fixtures/invalid-source-map.map b/test/fixtures/invalid-source-map.map new file mode 100644 index 0000000..cdcf017 --- /dev/null +++ b/test/fixtures/invalid-source-map.map @@ -0,0 +1 @@ +{"version":3,"file":"invalid-source-map.js","sources":["../invalid-source-map.txt"],"mappings":"AAAA"}"} \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 0504647..819f036 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -149,6 +149,20 @@ describe("source-map-loader", function() { done(); }); }); + it("should warn on invalid SourceMap", function (done) { + execLoader(path.join(__dirname, "fixtures", "invalid-source-map.js"), function (err, res, map, deps, warns) { + should.equal(err, null); + warns.should.matchEach( + new RegExp("Cannot parse SourceMap 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102") + ); + should.equal(res, "with SourceMap\n//#sourceMappingURL=invalid-source-map.map\n// comment"); + should.equal(map, null); + deps.should.be.eql([ + path.join(__dirname, "fixtures", "invalid-source-map.map") + ]); + done(); + }); + }); it("should warn on missing SourceMap", function(done) { execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) { should.equal(err, null);