Skip to content

Commit

Permalink
fix: Load source map only from last directive (#31)
Browse files Browse the repository at this point in the history
* Load source map only from last directive

* Remove \r in test input, avoid issue running test with CRLF fixtures
  • Loading branch information
zuohaocheng authored and joshwiens committed Mar 7, 2017
1 parent d2da8e6 commit eabfc7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/multi-source-map.js
Original file line number Diff line number Diff line change
@@ -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
22 changes: 21 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit eabfc7e

Please sign in to comment.