Skip to content

Commit 9c13d51

Browse files
committed
improve message format; prevent destructure crash; improve tests;
1 parent ce63b53 commit 9c13d51

File tree

8 files changed

+49
-41
lines changed

8 files changed

+49
-41
lines changed

lib/ModuleBuildError.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ const WebpackError = require("./WebpackError");
88
const cutOffLoaderExecution = require("./ErrorHelpers").cutOffLoaderExecution;
99

1010
class ModuleBuildError extends WebpackError {
11-
constructor(module, err, { from }) {
11+
constructor(module, err, { from } = {}) {
1212
super();
1313

1414
this.name = "ModuleBuildError";
1515
this.message = "Module build failed";
1616
if (from) {
17-
this.message += ` (@${from})`;
17+
this.message += ` (from ${from})`;
1818
}
1919

20-
var message;
20+
let message;
2121
if (err !== null && typeof err === "object") {
2222
if (typeof err.stack === "string" && err.stack) {
23-
var stack = cutOffLoaderExecution(err.stack);
23+
const stack = cutOffLoaderExecution(err.stack);
2424
if (!err.hideStack) {
2525
message = stack;
2626
} else {
@@ -41,7 +41,7 @@ class ModuleBuildError extends WebpackError {
4141
}
4242

4343
if (message !== "") {
44-
this.message += `: ${message}`;
44+
this.message += `:\n${message}`;
4545
}
4646

4747
this.module = module;

lib/ModuleError.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ const WebpackError = require("./WebpackError");
88
const cleanUp = require("./ErrorHelpers").cleanUp;
99

1010
class ModuleError extends WebpackError {
11-
constructor(module, err, { from }) {
11+
constructor(module, err, { from } = {}) {
1212
super();
1313

1414
this.name = "ModuleError";
1515
this.module = module;
1616
this.message = "Module Error";
1717
if (from) {
18-
this.message += ` (@${from})`;
18+
this.message += ` (from ${from})`;
1919
}
2020
if (err && typeof err === "object" && err.message) {
21-
this.message += `: ${err.message}`;
21+
this.message += `:\n${err.message}`;
2222
} else if (err) {
23-
this.message += `: ${err}`;
23+
this.message += `:\n${err}`;
2424
}
2525
this.error = err;
2626
this.details =

lib/ModuleWarning.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ const WebpackError = require("./WebpackError");
88
const cleanUp = require("./ErrorHelpers").cleanUp;
99

1010
class ModuleWarning extends WebpackError {
11-
constructor(module, warning, { from }) {
11+
constructor(module, warning, { from } = {}) {
1212
super();
1313

1414
this.name = "ModuleWarning";
1515
this.module = module;
1616
this.message = "Module Warning";
1717
if (from) {
18-
this.message += ` (@${from})`;
18+
this.message += ` (from ${from})`;
1919
}
2020
if (warning && typeof warning === "object" && warning.message) {
21-
this.message += `: ${warning.message}`;
21+
this.message += `:\n${warning.message}`;
2222
} else if (warning) {
23-
this.message += `: ${warning}`;
23+
this.message += `:\n${warning}`;
2424
}
2525
this.warning = warning;
2626
this.details =

lib/NormalModule.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ class NormalModule extends Module {
136136
}
137137

138138
createLoaderContext(resolver, options, compilation, fs) {
139-
const requestShorten = compilation.runtimeTemplate.requestShortener.shorten.bind(
140-
compilation.runtimeTemplate.requestShortener
141-
);
139+
const requestShortener = compilation.runtimeTemplate.requestShortener;
142140
const loaderContext = {
143141
version: 2,
144142
emitWarning: warning => {
@@ -147,7 +145,7 @@ class NormalModule extends Module {
147145
const currentLoader = this.getCurrentLoader(loaderContext);
148146
this.warnings.push(
149147
new ModuleWarning(this, warning, {
150-
from: requestShorten(currentLoader.loader)
148+
from: requestShortener.shorten(currentLoader.loader)
151149
})
152150
);
153151
},
@@ -156,7 +154,7 @@ class NormalModule extends Module {
156154
const currentLoader = this.getCurrentLoader(loaderContext);
157155
this.errors.push(
158156
new ModuleError(this, error, {
159-
from: requestShorten(currentLoader.loader)
157+
from: requestShortener.shorten(currentLoader.loader)
160158
})
161159
);
162160
},

test/Errors.test.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,11 @@ describe("Errors", () => {
207207
warnings.length.should.be.eql(1);
208208
warnings[0]
209209
.split("\n")[1]
210-
.should.match(
211-
/^Module Warning \(@.\/emit-error-loader.js\): [^\s]+/
212-
);
210+
.should.match(/^Module Warning \(from .\/emit-error-loader.js\):$/);
213211
errors.length.should.be.eql(1);
214212
errors[0]
215213
.split("\n")[1]
216-
.should.match(/^Module Error \(@.\/emit-error-loader.js\): [^\s]+/);
214+
.should.match(/^Module Error \(from .\/emit-error-loader.js\):$/);
217215
}
218216
),
219217
getErrorsPromise(
@@ -225,13 +223,11 @@ describe("Errors", () => {
225223
warnings.length.should.be.eql(1);
226224
warnings[0]
227225
.split("\n")[1]
228-
.should.match(
229-
/^Module Warning \(@.\/emit-error-loader.js\): [^\s]+/
230-
);
226+
.should.match(/^Module Warning \(from .\/emit-error-loader.js\):$/);
231227
errors.length.should.be.eql(1);
232228
errors[0]
233229
.split("\n")[1]
234-
.should.match(/^Module Error \(@.\/emit-error-loader.js\): [^\s]+/);
230+
.should.match(/^Module Error \(from .\/emit-error-loader.js\):$/);
235231
}
236232
),
237233
getErrorsPromise(
@@ -256,17 +252,15 @@ describe("Errors", () => {
256252
warnings.length.should.be.eql(1);
257253
warnings[0]
258254
.split("\n")[1]
259-
.should.match(
260-
/^Module Warning \(@.\/emit-error-loader.js\): [^\s]+/
261-
);
255+
.should.match(/^Module Warning \(from .\/emit-error-loader.js\):$/);
262256
errors.length.should.be.eql(2);
263257
errors[0]
264258
.split("\n")[1]
265-
.should.match(/^Module Error \(@.\/emit-error-loader.js\): [^\s]+/);
259+
.should.match(/^Module Error \(from .\/emit-error-loader.js\):$/);
266260
errors[1]
267261
.split("\n")[1]
268262
.should.match(
269-
/^Module build failed \(@\(webpack\)\/node_modules\/json-loader\/index.js\): [^\s]+/
263+
/^Module build failed \(from \(webpack\)\/node_modules\/json-loader\/index.js\):$/
270264
);
271265
}
272266
),
@@ -288,7 +282,7 @@ describe("Errors", () => {
288282
errors[0]
289283
.split("\n")[1]
290284
.should.match(
291-
/^Module build failed \(@.\/async-error-loader.js\): [^\s]+/
285+
/^Module build failed \(from .\/async-error-loader.js\):$/
292286
);
293287
}
294288
),
@@ -310,7 +304,7 @@ describe("Errors", () => {
310304
errors[0]
311305
.split("\n")[1]
312306
.should.match(
313-
/^Module build failed \(@.\/throw-error-loader.js\): [^\s]+/
307+
/^Module build failed \(from .\/throw-error-loader.js\):$/
314308
);
315309
}
316310
),
@@ -332,32 +326,46 @@ describe("Errors", () => {
332326
warnings[0]
333327
.split("\n")[1]
334328
.should.match(
335-
/^Module Warning \(@.\/irregular-error-loader.js\): [^\s]+/
329+
/^Module Warning \(from .\/irregular-error-loader.js\):$/
336330
);
337331
warnings[1]
338332
.split("\n")[1]
339333
.should.match(
340-
/^Module Warning \(@.\/irregular-error-loader.js\): [^\s]+/
334+
/^Module Warning \(from .\/irregular-error-loader.js\):$/
341335
);
342336

343337
errors.length.should.be.eql(3);
344338
errors[0]
345339
.split("\n")[1]
346340
.should.match(
347-
/^Module Error \(@.\/irregular-error-loader.js\): [^\s]+/
341+
/^Module Error \(from .\/irregular-error-loader.js\):$/
348342
);
349343
errors[1]
350344
.split("\n")[1]
351345
.should.match(
352-
/^Module Error \(@.\/irregular-error-loader.js\): [^\s]+/
346+
/^Module Error \(from .\/irregular-error-loader.js\):$/
353347
);
354348
errors[2]
355349
.split("\n")[1]
356350
.should.match(
357-
/^Module build failed \(@.\/irregular-error-loader.js\): [^\s]+/
351+
/^Module build failed \(from .\/irregular-error-loader.js\):$/
358352
);
359353
}
360354
)
361355
]);
362356
});
357+
it("should throw a build error if no source be returned after run loaders", () => {
358+
getErrors(
359+
{
360+
mode: "development",
361+
entry: path.resolve(base, "./no-return-loader") + "!./entry-point.js"
362+
},
363+
(errors, warnings) => {
364+
errors.length.should.be.eql(1);
365+
const messages = errors[0].split("\n");
366+
messages[1].should.match(/^Module build failed:$/);
367+
messages[2].should.match(/didn't return/);
368+
}
369+
);
370+
});
363371
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = [
2-
[/Module build failed( \(@[^)]+\))?: Message\nStack/]
2+
[/Module build failed( \(from [^)]+\))?:\nMessage\nStack/]
33
];

test/fixtures/errors/irregular-error-loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = function(source) {
2-
var empty = null;
3-
var emptyError = new Error();
2+
const empty = null;
3+
const emptyError = new Error();
44
this.emitWarning(empty);
55
this.emitWarning(emptyError);
66
this.emitError(empty);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = function(){
2+
}

0 commit comments

Comments
 (0)