From 1eb1e78c4a0ad2ca4573da7422a61dc233da515c Mon Sep 17 00:00:00 2001 From: Zuo Haocheng Date: Tue, 7 Mar 2017 10:56:57 +0800 Subject: [PATCH 1/2] Load source map only from last directive --- index.js | 3 ++- test/fixtures/multi-source-map.js | 4 ++++ test/index.test.js | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/multi-source-map.js diff --git a/index.js b/index.js index e2e38b2..49a0960 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,8 @@ var path = require("path"); var async = require("async"); var loaderUtils = require("loader-utils"); -var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)", +// Matches only the last occurrence of sourceMappingURL +var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)(?![\S\s]*sourceMappingURL)", // Matches /* ... */ comments regex1 = new RegExp("/\\*"+baseRegex+"\\s*\\*/"), // Matches // .... comments diff --git a/test/fixtures/multi-source-map.js b/test/fixtures/multi-source-map.js new file mode 100644 index 0000000..9abb042 --- /dev/null +++ b/test/fixtures/multi-source-map.js @@ -0,0 +1,4 @@ +with SourceMap +anInvalidDirective = "\n/*# sourceMappingURL=data:application/json;base64," + btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))) + " */"; +// @ sourceMappingURL = data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ== +// comment \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 3083bb6..198b402 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -107,6 +107,24 @@ describe("source-map-loader", function() { done(); }); }); + it("should use last SourceMap directive", function (done) { + execLoader(path.join(__dirname, "fixtures", "multi-source-map.js"), function (err, res, map, deps, warns) { + should.equal(err, null); + warns.should.be.eql([]); + should.equal(res, "with SourceMap\nanInvalidDirective = \"\\n/*# sourceMappingURL=data:application/json;base64,\" + btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))) + \" */\";\n// comment"), + map.should.be.eql({ + "version": 3, + "file": "inline-source-map.js", + "sources": [ + "inline-source-map.txt" + ], + "sourcesContent": ["with SourceMap"], + "mappings": "AAAA" + }); + deps.should.be.eql([]); + 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); From a19a9b6fc5af84d615afef487574fb219b5fc5d3 Mon Sep 17 00:00:00 2001 From: Zuo Haocheng Date: Tue, 7 Mar 2017 12:42:13 +0800 Subject: [PATCH 2/2] Remove \r in test input, avoid issue running test with CRLF fixtures --- test/index.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index 198b402..2ea48cd 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -33,7 +33,9 @@ function execLoader(filename, callback) { return this.callback; } }; - var res = loader.call(context, fs.readFileSync(filename, "utf-8")); + // Remove CRs to make test line ending invariant + var fixtureContent = fs.readFileSync(filename, "utf-8").replace(/\r/g, ''); + var res = loader.call(context, fixtureContent); if(!async) return callback(null, res, null, deps, warns); }