Skip to content

Commit 0ce1def

Browse files
committed
Load source map only from last directive
1 parent d2da8e6 commit 0ce1def

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var path = require("path");
88
var async = require("async");
99
var loaderUtils = require("loader-utils");
1010

11-
var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)",
11+
// Matches only the last occurrence of sourceMappingURL
12+
var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)(?![\S\s]*sourceMappingURL)",
1213
// Matches /* ... */ comments
1314
regex1 = new RegExp("/\\*"+baseRegex+"\\s*\\*/"),
1415
// Matches // .... comments

test/fixtures/multi-source-map.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
with SourceMap
2+
anInvalidDirective = "\n/*# sourceMappingURL=data:application/json;base64," + btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))) + " */";
3+
// @ sourceMappingURL = data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==
4+
// comment

test/index.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ describe("source-map-loader", function() {
107107
done();
108108
});
109109
});
110+
it("should use last SourceMap directive", function (done) {
111+
execLoader(path.join(__dirname, "fixtures", "multi-source-map.js"), function (err, res, map, deps, warns) {
112+
should.equal(err, null);
113+
warns.should.be.eql([]);
114+
should.equal(res, "with SourceMap\nanInvalidDirective = \"\\n/*# sourceMappingURL=data:application/json;base64,\" + btoa(unescape(encodeURICompone == 'with SourceMap\n// comment"),
115+
map.should.be.eql({
116+
"version": 3,
117+
"file": "inline-source-map.js",
118+
"sources": [
119+
"inline-source-map.txt"
120+
],
121+
"sourcesContent": ["with SourceMap"],
122+
"mappings": "AAAA"
123+
});
124+
deps.should.be.eql([]);
125+
done();
126+
});
127+
});
110128
it("should warn on missing SourceMap", function(done) {
111129
execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) {
112130
should.equal(err, null);

0 commit comments

Comments
 (0)