Skip to content

Commit e2fdbfd

Browse files
iclantonmichael-ciniawsky
authored andcommitted
fix(index): resolve source maps with root-relative paths correctly (#68)
1 parent 78ad469 commit e2fdbfd

8 files changed

+121
-21
lines changed

index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ module.exports = function(input, inputMap) {
3535
map = JSON.parse(mapStr)
3636
} catch (e) {
3737
emitWarning("Cannot parse inline SourceMap '" + mapBase64.substr(0, 50) + "': " + e);
38-
return untouched();
38+
return untouched();
3939
}
4040
processMap(map, this.context, callback);
4141
} else {
42-
resolve(this.context, loaderUtils.urlToRequest(url), function(err, result) {
42+
resolve(this.context, loaderUtils.urlToRequest(url, true), function(err, result) {
4343
if(err) {
4444
emitWarning("Cannot find SourceMap '" + url + "': " + err);
4545
return untouched();
@@ -76,7 +76,7 @@ module.exports = function(input, inputMap) {
7676
delete map.sourceRoot;
7777
var missingSources = map.sourcesContent ? map.sources.slice(map.sourcesContent.length) : map.sources;
7878
async.map(missingSources, function(source, callback) {
79-
resolve(context, loaderUtils.urlToRequest(source), function(err, result) {
79+
resolve(context, loaderUtils.urlToRequest(source, true), function(err, result) {
8080
if(err) {
8181
emitWarning("Cannot find source file '" + source + "': " + err);
8282
return callback(null, null);

test/fixtures/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
absolute-sourceRoot-source-map.map
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
//#sourceMappingURL=absolute-sourceRoot-source-map.map
3+
// comment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
with SourceMap
2+
// comment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
with SourceMap
2+
// comment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
//#sourceMappingURL=relative-sourceRoot-source-map.map
3+
// comment

test/fixtures/relative-sourceRoot-source-map.map

+1
Original file line numberDiff line numberDiff line change

test/index.test.js

+106-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function execLoader(filename, callback) {
1111
context: path.dirname(filename),
1212
resolve: function(context, request, callback) {
1313
process.nextTick(function() {
14-
var p = path.join(context, request);
14+
var p = path.isAbsolute(request) ? request : path.resolve(context, request);
1515
if(fs.existsSync(p))
1616
callback(null, p);
1717
else
@@ -40,8 +40,11 @@ function execLoader(filename, callback) {
4040
}
4141

4242
describe("source-map-loader", function() {
43+
const fixturesPath = path.join(__dirname, "fixtures");
44+
const dataPath = path.join(fixturesPath, "data");
45+
4346
it("should leave normal files untouched", function(done) {
44-
execLoader(path.join(__dirname, "fixtures", "normal-file.js"), function(err, res, map, deps, warns) {
47+
execLoader(path.join(fixturesPath, "normal-file.js"), function(err, res, map, deps, warns) {
4548
should.equal(err, null);
4649
warns.should.be.eql([]);
4750
should.equal(res, "without SourceMap"),
@@ -50,8 +53,9 @@ describe("source-map-loader", function() {
5053
done();
5154
});
5255
});
56+
5357
it("should process inlined SourceMaps", function(done) {
54-
execLoader(path.join(__dirname, "fixtures", "inline-source-map.js"), function(err, res, map, deps, warns) {
58+
execLoader(path.join(fixturesPath, "inline-source-map.js"), function(err, res, map, deps, warns) {
5559
should.equal(err, null);
5660
warns.should.be.eql([]);
5761
should.equal(res, "with SourceMap\n// comment"),
@@ -68,8 +72,9 @@ describe("source-map-loader", function() {
6872
done();
6973
});
7074
});
75+
7176
it("should process external SourceMaps", function(done) {
72-
execLoader(path.join(__dirname, "fixtures", "external-source-map.js"), function(err, res, map, deps, warns) {
77+
execLoader(path.join(fixturesPath, "external-source-map.js"), function(err, res, map, deps, warns) {
7378
should.equal(err, null);
7479
warns.should.be.eql([]);
7580
should.equal(res, "with SourceMap\n// comment"),
@@ -83,34 +88,36 @@ describe("source-map-loader", function() {
8388
"mappings":"AAAA"
8489
});
8590
deps.should.be.eql([
86-
path.join(__dirname, "fixtures", "external-source-map.map")
91+
path.join(fixturesPath, "external-source-map.map")
8792
]);
8893
done();
8994
});
9095
});
96+
9197
it("should process external SourceMaps (external sources)", function(done) {
92-
execLoader(path.join(__dirname, "fixtures", "external-source-map2.js"), function(err, res, map, deps, warns) {
98+
execLoader(path.join(fixturesPath, "external-source-map2.js"), function(err, res, map, deps, warns) {
9399
should.equal(err, null);
94100
warns.should.be.eql([]);
95101
should.equal(res, "with SourceMap\n// comment"),
96102
map.should.be.eql({
97103
"version":3,
98104
"file":"external-source-map2.js",
99105
"sources":[
100-
path.join(__dirname, "fixtures", "external-source-map2.txt")
106+
path.join(fixturesPath, "external-source-map2.txt")
101107
],
102108
"sourcesContent":["with SourceMap"],
103109
"mappings":"AAAA"
104110
});
105111
deps.should.be.eql([
106-
path.join(__dirname, "fixtures", "data", "external-source-map2.map"),
107-
path.join(__dirname, "fixtures", "external-source-map2.txt")
112+
path.join(dataPath, "external-source-map2.map"),
113+
path.join(fixturesPath, "external-source-map2.txt")
108114
]);
109115
done();
110116
});
111117
});
118+
112119
it("should use last SourceMap directive", function (done) {
113-
execLoader(path.join(__dirname, "fixtures", "multi-source-map.js"), function (err, res, map, deps, warns) {
120+
execLoader(path.join(fixturesPath, "multi-source-map.js"), function (err, res, map, deps, warns) {
114121
should.equal(err, null);
115122
warns.should.be.eql([]);
116123
should.equal(res, "with SourceMap\nanInvalidDirective = \"\\n/*# sourceMappingURL=data:application/json;base64,\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\" */\";\n// comment"),
@@ -127,8 +134,9 @@ describe("source-map-loader", function() {
127134
done();
128135
});
129136
});
137+
130138
it("should skip invalid base64 SourceMap", function (done) {
131-
execLoader(path.join(__dirname, "fixtures", "invalid-inline-source-map.js"), function (err, res, map, deps, warns) {
139+
execLoader(path.join(fixturesPath, "invalid-inline-source-map.js"), function (err, res, map, deps, warns) {
132140
should.equal(err, null);
133141
warns.should.be.eql([]);
134142
should.equal(res, "without SourceMap\n// @sourceMappingURL=data:application/source-map;base64,\"something invalid\"\n// comment");
@@ -138,7 +146,7 @@ describe("source-map-loader", function() {
138146
});
139147
});
140148
it("should warn on invalid base64 SourceMap", function (done) {
141-
execLoader(path.join(__dirname, "fixtures", "invalid-inline-source-map2.js"), function (err, res, map, deps, warns) {
149+
execLoader(path.join(fixturesPath, "invalid-inline-source-map2.js"), function (err, res, map, deps, warns) {
142150
should.equal(err, null);
143151
warns.should.matchEach(
144152
new RegExp("Cannot parse inline SourceMap 'invalid\/base64=': SyntaxError: Unexpected token")
@@ -149,22 +157,24 @@ describe("source-map-loader", function() {
149157
done();
150158
});
151159
});
160+
152161
it("should warn on invalid SourceMap", function (done) {
153-
execLoader(path.join(__dirname, "fixtures", "invalid-source-map.js"), function (err, res, map, deps, warns) {
162+
execLoader(path.join(fixturesPath, "invalid-source-map.js"), function (err, res, map, deps, warns) {
154163
should.equal(err, null);
155164
warns.should.matchEach(
156165
new RegExp("Cannot parse SourceMap 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102")
157166
);
158167
should.equal(res, "with SourceMap\n//#sourceMappingURL=invalid-source-map.map\n// comment");
159168
should.equal(map, null);
160169
deps.should.be.eql([
161-
path.join(__dirname, "fixtures", "invalid-source-map.map")
170+
path.join(fixturesPath, "invalid-source-map.map")
162171
]);
163172
done();
164173
});
165174
});
175+
166176
it("should warn on missing SourceMap", function(done) {
167-
execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) {
177+
execLoader(path.join(fixturesPath, "missing-source-map.js"), function(err, res, map, deps, warns) {
168178
should.equal(err, null);
169179
warns.should.be.eql([
170180
"Cannot find SourceMap 'missing-source-map.map': Error: File not found"
@@ -175,8 +185,9 @@ describe("source-map-loader", function() {
175185
done();
176186
});
177187
});
188+
178189
it("should warn on missing source file", function(done) {
179-
execLoader(path.join(__dirname, "fixtures", "missing-source-map2.js"), function(err, res, map, deps, warns) {
190+
execLoader(path.join(fixturesPath, "missing-source-map2.js"), function(err, res, map, deps, warns) {
180191
should.equal(err, null);
181192
warns.should.be.eql([
182193
"Cannot find source file 'missing-source-map2.txt': Error: File not found"
@@ -192,14 +203,14 @@ describe("source-map-loader", function() {
192203
"mappings":"AAAA"
193204
});
194205
deps.should.be.eql([
195-
path.join(__dirname, "fixtures", "missing-source-map2.map")
206+
path.join(fixturesPath, "missing-source-map2.map")
196207
]);
197208
done();
198209
});
199210
});
200211

201212
it("should process inlined SourceMaps with charset", function(done) {
202-
execLoader(path.join(__dirname, "fixtures", "charset-inline-source-map.js"), function(err, res, map, deps, warns) {
213+
execLoader(path.join(fixturesPath, "charset-inline-source-map.js"), function(err, res, map, deps, warns) {
203214
should.equal(err, null);
204215
warns.should.be.eql([]);
205216
should.equal(res, "with SourceMap\n// comment"),
@@ -216,4 +227,81 @@ describe("source-map-loader", function() {
216227
done();
217228
});
218229
});
230+
231+
it("should support absolute sourceRoot paths in sourcemaps", (done) => {
232+
const sourceRoot = path.join(fixturesPath);
233+
const javaScriptFilename = "absolute-sourceRoot-source-map.js";
234+
const sourceFilename = "absolute-sourceRoot-source-map.txt";
235+
const rootRelativeSourcePath = path.join(sourceRoot, sourceFilename);
236+
const sourceMapPath = path.join(sourceRoot, "absolute-sourceRoot-source-map.map");
237+
238+
// Create the sourcemap file
239+
const rawSourceMap = {
240+
"version": 3,
241+
"file": javaScriptFilename,
242+
"sourceRoot": sourceRoot,
243+
"sources": [
244+
sourceFilename
245+
],
246+
"mappings": "AAAA"
247+
};
248+
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
249+
250+
execLoader(
251+
path.join(fixturesPath, javaScriptFilename),
252+
(err, res, map, deps, warns) => {
253+
should.equal(err, null);
254+
warns.should.be.eql([]);
255+
should.equal(res, "with SourceMap\n// comment"),
256+
map.should.be.eql({
257+
"version": 3,
258+
"file": javaScriptFilename,
259+
"sources": [
260+
rootRelativeSourcePath
261+
],
262+
"sourcesContent": [
263+
"with SourceMap\n// comment"
264+
],
265+
"mappings": "AAAA"
266+
});
267+
deps.should.be.eql([
268+
sourceMapPath,
269+
rootRelativeSourcePath
270+
]);
271+
done();
272+
}
273+
);
274+
});
275+
276+
it("should support relative sourceRoot paths in sourcemaps", (done) => {
277+
const javaScriptFilename = "relative-sourceRoot-source-map.js";
278+
const sourceFilename = "relative-sourceRoot-source-map.txt";
279+
const rootRelativeSourcePath = path.join(dataPath, sourceFilename);
280+
const sourceMapPath = path.join(fixturesPath, "relative-sourceRoot-source-map.map");
281+
282+
execLoader(
283+
path.join(fixturesPath, javaScriptFilename),
284+
(err, res, map, deps, warns) => {
285+
should.equal(err, null);
286+
warns.should.be.eql([]);
287+
should.equal(res, "with SourceMap\n// comment"),
288+
map.should.be.eql({
289+
"version": 3,
290+
"file": javaScriptFilename,
291+
"sources": [
292+
rootRelativeSourcePath
293+
],
294+
"sourcesContent": [
295+
"with SourceMap\n// comment"
296+
],
297+
"mappings": "AAAA"
298+
});
299+
deps.should.be.eql([
300+
sourceMapPath,
301+
rootRelativeSourcePath
302+
]);
303+
done();
304+
}
305+
);
306+
});
219307
});

0 commit comments

Comments
 (0)