Skip to content

Commit cc60e75

Browse files
committed
added warnings for missing SourceMaps and source files
1 parent dd43247 commit cc60e75

File tree

5 files changed

+99
-23
lines changed

5 files changed

+99
-23
lines changed

index.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = function(input, inputMap) {
2020
this.cacheable && this.cacheable();
2121
var resolve = this.resolve;
2222
var addDependency = this.addDependency;
23+
var emitWarning = this.emitWarning || function() {};
2324
var match = input.match(regex1) || input.match(regex2);
2425
if(match) {
2526
var url = match[1];
@@ -29,41 +30,63 @@ module.exports = function(input, inputMap) {
2930
processMap(JSON.parse((new Buffer(dataUrlMatch[1], "base64")).toString()), this.context, callback);
3031
} else {
3132
resolve(this.context, loaderUtils.urlToRequest(url), function(err, result) {
32-
if(err) return callback(err);
33+
if(err) {
34+
emitWarning("Cannot find SourceMap '" + url + "': " + err);
35+
return untouched();
36+
}
3337
addDependency(result);
3438
fs.readFile(result, "utf-8", function(err, content) {
35-
if(err) return callback(err);
39+
if(err) {
40+
emitWarning("Cannot open SourceMap '" + result + "': " + err);
41+
return untouched();
42+
}
3643
processMap(JSON.parse(content), path.dirname(result), callback);
3744
});
3845
}.bind(this));
3946
return;
4047
}
4148
} else {
42-
this.callback(null, input, inputMap);
49+
var callback = this.callback;
50+
return untouched();
51+
}
52+
function untouched() {
53+
callback(null, input, inputMap);
4354
}
4455
function processMap(map, context, callback) {
4556
if(!map.sourcesContent || map.sourcesContent.length < map.sources.length) {
4657
var missingSources = map.sourcesContent ? map.sources.slice(map.sourcesContent.length) : map.sources;
4758
async.map(missingSources, function(source, callback) {
4859
resolve(context, loaderUtils.urlToRequest(source), function(err, result) {
49-
if(err) return callback(null, null);
60+
if(err) {
61+
emitWarning("Cannot find source file '" + source + "': " + err);
62+
return callback(null, null);
63+
}
5064
addDependency(result);
5165
fs.readFile(result, "utf-8", function(err, content) {
52-
if(err) return callback(null, null);
53-
callback(null, content);
66+
if(err) {
67+
emitWarning("Cannot open source file '" + result + "': " + err);
68+
return callback(null, null);
69+
}
70+
callback(null, {
71+
source: result,
72+
content: content
73+
});
5474
});
5575
});
56-
}, function(err, sourcesContent) {
57-
map.sourcesContent = map.sourcesContent ? map.sourcesContent.concat(sourcesContent) : sourcesContent;
76+
}, function(err, info) {
77+
map.sourcesContent = map.sourcesContent || [];
78+
info.forEach(function(res) {
79+
if(res) {
80+
map.sources[map.sourcesContent.length] = res.source;
81+
map.sourcesContent.push(res.content);
82+
} else {
83+
map.sourcesContent.push(null);
84+
}
85+
});
5886
processMap(map, context, callback);
5987
});
6088
return;
6189
}
62-
async.map(map.sources, function(url, callback) {
63-
resolve(context, loaderUtils.urlToRequest(url), callback);
64-
}, function(err, sources) {
65-
map.sources = sources;
66-
callback(null, input.replace(match[0], match[2]), map);
67-
});
90+
callback(null, input.replace(match[0], match[2]), map);
6891
}
6992
}

test/fixtures/missing-source-map.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
//#sourceMappingURL=missing-source-map.map
3+
// comment

test/fixtures/missing-source-map2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
//#sourceMappingURL=missing-source-map2.map
3+
// comment

test/fixtures/missing-source-map2.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.test.js

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,58 @@ var loader = require("../");
66
function execLoader(filename, callback) {
77
var async = false;
88
var deps = [];
9+
var warns = [];
910
var context = {
1011
context: path.dirname(filename),
1112
resolve: function(context, request, callback) {
1213
process.nextTick(function() {
13-
callback(null, path.join(context, request));
14+
var p = path.join(context, request);
15+
if(fs.existsSync(p))
16+
callback(null, p);
17+
else
18+
callback(new Error("File not found"));
1419
});
1520
},
1621
addDependency: function(dep) {
1722
deps.push(dep);
1823
},
24+
emitWarning: function(warn) {
25+
warns.push(warn);
26+
},
1927
callback: function(err, res, map) {
2028
async = true;
21-
callback(err, res, map, deps);
29+
callback(err, res, map, deps, warns);
2230
},
2331
async: function() {
2432
async = true;
2533
return this.callback;
2634
}
2735
};
2836
var res = loader.call(context, fs.readFileSync(filename, "utf-8"));
29-
if(!async) return callback(null, res, null, deps);
37+
if(!async) return callback(null, res, null, deps, warns);
3038
}
3139

3240
describe("source-map-loader", function() {
3341
it("should leave normal files untouched", function(done) {
34-
execLoader(path.join(__dirname, "fixtures", "normal-file.js"), function(err, res, map, deps) {
42+
execLoader(path.join(__dirname, "fixtures", "normal-file.js"), function(err, res, map, deps, warns) {
3543
should.equal(err, null);
44+
warns.should.be.eql([]);
3645
should.equal(res, "without SourceMap"),
3746
should.equal(map, null);
3847
deps.should.be.eql([]);
3948
done();
4049
});
4150
});
4251
it("should process inlined SourceMaps", function(done) {
43-
execLoader(path.join(__dirname, "fixtures", "inline-source-map.js"), function(err, res, map, deps) {
52+
execLoader(path.join(__dirname, "fixtures", "inline-source-map.js"), function(err, res, map, deps, warns) {
4453
should.equal(err, null);
54+
warns.should.be.eql([]);
4555
should.equal(res, "with SourceMap\n\n// comment"),
4656
map.should.be.eql({
4757
"version":3,
4858
"file":"inline-source-map.js",
4959
"sources":[
50-
path.join(__dirname, "fixtures", "inline-source-map.txt")
60+
"inline-source-map.txt"
5161
],
5262
"sourcesContent":["with SourceMap"],
5363
"mappings":"AAAA"
@@ -57,14 +67,15 @@ describe("source-map-loader", function() {
5767
});
5868
});
5969
it("should process external SourceMaps", function(done) {
60-
execLoader(path.join(__dirname, "fixtures", "external-source-map.js"), function(err, res, map, deps) {
70+
execLoader(path.join(__dirname, "fixtures", "external-source-map.js"), function(err, res, map, deps, warns) {
6171
should.equal(err, null);
72+
warns.should.be.eql([]);
6273
should.equal(res, "with SourceMap\n\n// comment"),
6374
map.should.be.eql({
6475
"version":3,
6576
"file":"external-source-map.js",
6677
"sources":[
67-
path.join(__dirname, "fixtures", "external-source-map.txt")
78+
"external-source-map.txt"
6879
],
6980
"sourcesContent":["with SourceMap"],
7081
"mappings":"AAAA"
@@ -76,8 +87,9 @@ describe("source-map-loader", function() {
7687
});
7788
});
7889
it("should process external SourceMaps (external sources)", function(done) {
79-
execLoader(path.join(__dirname, "fixtures", "external-source-map2.js"), function(err, res, map, deps) {
90+
execLoader(path.join(__dirname, "fixtures", "external-source-map2.js"), function(err, res, map, deps, warns) {
8091
should.equal(err, null);
92+
warns.should.be.eql([]);
8193
should.equal(res, "with SourceMap\n\n// comment"),
8294
map.should.be.eql({
8395
"version":3,
@@ -95,4 +107,38 @@ describe("source-map-loader", function() {
95107
done();
96108
});
97109
});
110+
it("should warn on missing SourceMap", function(done) {
111+
execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) {
112+
should.equal(err, null);
113+
warns.should.be.eql([
114+
"Cannot find SourceMap 'missing-source-map.map': Error: File not found"
115+
]);
116+
should.equal(res, "with SourceMap\n//#sourceMappingURL=missing-source-map.map\n// comment"),
117+
should.equal(map, null);
118+
deps.should.be.eql([]);
119+
done();
120+
});
121+
});
122+
it("should warn on missing source file", function(done) {
123+
execLoader(path.join(__dirname, "fixtures", "missing-source-map2.js"), function(err, res, map, deps, warns) {
124+
should.equal(err, null);
125+
warns.should.be.eql([
126+
"Cannot find source file 'missing-source-map2.txt': Error: File not found"
127+
]);
128+
should.equal(res, "with SourceMap\n\n// comment"),
129+
map.should.be.eql({
130+
"version":3,
131+
"file":"missing-source-map2.js",
132+
"sources":[
133+
"missing-source-map2.txt"
134+
],
135+
"sourcesContent":[null],
136+
"mappings":"AAAA"
137+
});
138+
deps.should.be.eql([
139+
path.join(__dirname, "fixtures", "missing-source-map2.map")
140+
]);
141+
done();
142+
});
143+
});
98144
});

0 commit comments

Comments
 (0)