From 78ad469f3b586c84bd73d4f266ce2138cc280dd4 Mon Sep 17 00:00:00 2001 From: fimius23 Date: Wed, 18 Jul 2018 11:04:04 -0500 Subject: [PATCH] fix(index): handle exception on loading invalid source maps (#67) --- index.js | 9 ++++++++- test/fixtures/invalid-source-map.js | 3 +++ test/fixtures/invalid-source-map.map | 1 + test/index.test.js | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/invalid-source-map.js create mode 100644 test/fixtures/invalid-source-map.map 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);