Skip to content

Commit eabfc7e

Browse files
zuohaochengjoshwiens
authored andcommitted
fix: Load source map only from last directive (#31)
* Load source map only from last directive * Remove \r in test input, avoid issue running test with CRLF fixtures
1 parent d2da8e6 commit eabfc7e

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
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

+21-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ function execLoader(filename, callback) {
3333
return this.callback;
3434
}
3535
};
36-
var res = loader.call(context, fs.readFileSync(filename, "utf-8"));
36+
// Remove CRs to make test line ending invariant
37+
var fixtureContent = fs.readFileSync(filename, "utf-8").replace(/\r/g, '');
38+
var res = loader.call(context, fixtureContent);
3739
if(!async) return callback(null, res, null, deps, warns);
3840
}
3941

@@ -107,6 +109,24 @@ describe("source-map-loader", function() {
107109
done();
108110
});
109111
});
112+
it("should use last SourceMap directive", function (done) {
113+
execLoader(path.join(__dirname, "fixtures", "multi-source-map.js"), function (err, res, map, deps, warns) {
114+
should.equal(err, null);
115+
warns.should.be.eql([]);
116+
should.equal(res, "with SourceMap\nanInvalidDirective = \"\\n/*# sourceMappingURL=data:application/json;base64,\" + btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))) + \" */\";\n// comment"),
117+
map.should.be.eql({
118+
"version": 3,
119+
"file": "inline-source-map.js",
120+
"sources": [
121+
"inline-source-map.txt"
122+
],
123+
"sourcesContent": ["with SourceMap"],
124+
"mappings": "AAAA"
125+
});
126+
deps.should.be.eql([]);
127+
done();
128+
});
129+
});
110130
it("should warn on missing SourceMap", function(done) {
111131
execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) {
112132
should.equal(err, null);

0 commit comments

Comments
 (0)