Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load source map only from last directive #31

Merged
merged 2 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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