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..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); } @@ -107,6 +109,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);