Skip to content

Commit c0d5387

Browse files
authored
Merge pull request webpack#8168 from webpack/feature/serialization/errors
feat(serialization): serialize errors
2 parents 8ddcfb5 + ad308f9 commit c0d5387

21 files changed

+297
-74
lines changed

lib/CommentCompilationWarning.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,30 @@
66
"use strict";
77

88
const WebpackError = require("./WebpackError");
9-
10-
/** @typedef {import("./Module")} Module */
9+
const makeSerializable = require("./util/makeSerializable");
1110

1211
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
1312

1413
class CommentCompilationWarning extends WebpackError {
1514
/**
1615
*
1716
* @param {string} message warning message
18-
* @param {Module} module affected module
1917
* @param {DependencyLocation} loc affected lines of code
2018
*/
21-
constructor(message, module, loc) {
19+
constructor(message, loc) {
2220
super(message);
2321

2422
this.name = "CommentCompilationWarning";
2523

26-
this.module = module;
2724
this.loc = loc;
2825

2926
Error.captureStackTrace(this, this.constructor);
3027
}
3128
}
3229

30+
makeSerializable(
31+
CommentCompilationWarning,
32+
"webpack/lib/CommentCompilationWarning"
33+
);
34+
3335
module.exports = CommentCompilationWarning;

lib/Compilation.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,6 @@ class Compilation {
594594
if (err) return callback(err);
595595

596596
if (!needBuild) {
597-
for (const err of module.errors) {
598-
this.errors.push(err);
599-
}
600-
for (const err of module.warnings) {
601-
this.warnings.push(err);
602-
}
603597
if (currentProfile !== undefined) {
604598
currentProfile.markBuildingEnd();
605599
}

lib/JavascriptParserHelpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ exports.expressionIsUnsupported = (parser, message) => {
7272
parser.state.current.addDependency(dep);
7373
if (!parser.state.module) return;
7474
parser.state.module.warnings.push(
75-
new UnsupportedFeatureWarning(parser.state.module, message, expr.loc)
75+
new UnsupportedFeatureWarning(message, expr.loc)
7676
);
7777
return true;
7878
};

lib/ModuleBuildError.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@
77

88
const { cutOffLoaderExecution } = require("./ErrorHelpers");
99
const WebpackError = require("./WebpackError");
10+
const makeSerializable = require("./util/makeSerializable");
1011

1112
class ModuleBuildError extends WebpackError {
12-
constructor(module, err, { from = null } = {}) {
13+
constructor(err, { from = null } = {}) {
1314
let message = "Module build failed";
1415
let details = undefined;
16+
1517
if (from) {
1618
message += ` (from ${from}):\n`;
1719
} else {
1820
message += ": ";
1921
}
22+
2023
if (err !== null && typeof err === "object") {
2124
if (typeof err.stack === "string" && err.stack) {
2225
const stack = cutOffLoaderExecution(err.stack);
26+
2327
if (!err.hideStack) {
2428
message += stack;
2529
} else {
2630
details = stack;
31+
2732
if (typeof err.message === "string" && err.message) {
2833
message += err.message;
2934
} else {
@@ -43,11 +48,28 @@ class ModuleBuildError extends WebpackError {
4348

4449
this.name = "ModuleBuildError";
4550
this.details = details;
46-
this.module = module;
4751
this.error = err;
4852

4953
Error.captureStackTrace(this, this.constructor);
5054
}
55+
56+
serialize(context) {
57+
const { write } = context;
58+
59+
write(this.error);
60+
61+
super.serialize(context);
62+
}
63+
64+
deserialize(context) {
65+
const { read } = context;
66+
67+
this.error = read();
68+
69+
super.deserialize(context);
70+
}
5171
}
5272

73+
makeSerializable(ModuleBuildError, "webpack/lib/ModuleBuildError");
74+
5375
module.exports = ModuleBuildError;

lib/ModuleError.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77

88
const { cleanUp } = require("./ErrorHelpers");
99
const WebpackError = require("./WebpackError");
10+
const makeSerializable = require("./util/makeSerializable");
1011

1112
class ModuleError extends WebpackError {
12-
constructor(module, err, { from = null } = {}) {
13+
constructor(err, { from = null } = {}) {
1314
let message = "Module Error";
15+
1416
if (from) {
1517
message += ` (from ${from}):\n`;
1618
} else {
1719
message += ": ";
1820
}
21+
1922
if (err && typeof err === "object" && err.message) {
2023
message += err.message;
2124
} else if (err) {
2225
message += err;
2326
}
27+
2428
super(message);
29+
2530
this.name = "ModuleError";
26-
this.module = module;
2731
this.error = err;
2832
this.details =
2933
err && typeof err === "object" && err.stack
@@ -32,6 +36,24 @@ class ModuleError extends WebpackError {
3236

3337
Error.captureStackTrace(this, this.constructor);
3438
}
39+
40+
serialize(context) {
41+
const { write } = context;
42+
43+
write(this.error);
44+
45+
super.serialize(context);
46+
}
47+
48+
deserialize(context) {
49+
const { read } = context;
50+
51+
this.error = read();
52+
53+
super.deserialize(context);
54+
}
3555
}
3656

57+
makeSerializable(ModuleError, "webpack/lib/ModuleError");
58+
3759
module.exports = ModuleError;

lib/ModuleParseError.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@
66
"use strict";
77

88
const WebpackError = require("./WebpackError");
9-
10-
/** @typedef {import("./Module")} Module */
9+
const makeSerializable = require("./util/makeSerializable");
1110

1211
class ModuleParseError extends WebpackError {
1312
/**
14-
* @param {Module} module the errored module
1513
* @param {string} source source code
1614
* @param {Error&any} err the parse error
1715
*/
18-
constructor(module, source, err) {
19-
let message = "Module parse failed: " + err.message;
16+
constructor(source, err) {
17+
let message = "Module parse failed: " + (err && err.message);
2018
let loc = undefined;
19+
2120
message += "\nYou may need an appropriate loader to handle this file type.";
21+
2222
if (
23+
err &&
2324
err.loc &&
2425
typeof err.loc === "object" &&
2526
typeof err.loc.line === "number"
2627
) {
2728
var lineNumber = err.loc.line;
29+
2830
if (/[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)) {
2931
// binary file
3032
message += "\n(Source code omitted for this binary file)";
@@ -34,25 +36,44 @@ class ModuleParseError extends WebpackError {
3436
const linesBefore = sourceLines.slice(start, lineNumber - 1);
3537
const theLine = sourceLines[lineNumber - 1];
3638
const linesAfter = sourceLines.slice(lineNumber, lineNumber + 2);
39+
3740
message +=
3841
linesBefore.map(l => `\n| ${l}`).join("") +
3942
`\n> ${theLine}` +
4043
linesAfter.map(l => `\n| ${l}`).join("");
4144
}
45+
4246
loc = { start: err.loc };
43-
} else {
47+
} else if (err && err.stack) {
4448
message += "\n" + err.stack;
4549
}
4650

4751
super(message);
4852

4953
this.name = "ModuleParseError";
50-
this.module = module;
5154
this.loc = loc;
5255
this.error = err;
5356

5457
Error.captureStackTrace(this, this.constructor);
5558
}
59+
60+
serialize(context) {
61+
const { write } = context;
62+
63+
write(this.error);
64+
65+
super.serialize(context);
66+
}
67+
68+
deserialize(context) {
69+
const { read } = context;
70+
71+
this.error = read();
72+
73+
super.deserialize(context);
74+
}
5675
}
5776

77+
makeSerializable(ModuleParseError, "webpack/lib/ModuleParseError");
78+
5879
module.exports = ModuleParseError;

lib/ModuleWarning.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77

88
const { cleanUp } = require("./ErrorHelpers");
99
const WebpackError = require("./WebpackError");
10+
const makeSerializable = require("./util/makeSerializable");
1011

1112
class ModuleWarning extends WebpackError {
12-
constructor(module, warning, { from = null } = {}) {
13+
constructor(warning, { from = null } = {}) {
1314
let message = "Module Warning";
15+
1416
if (from) {
1517
message += ` (from ${from}):\n`;
1618
} else {
1719
message += ": ";
1820
}
21+
1922
if (warning && typeof warning === "object" && warning.message) {
2023
message += warning.message;
2124
} else if (warning) {
2225
message += warning;
2326
}
27+
2428
super(message);
29+
2530
this.name = "ModuleWarning";
26-
this.module = module;
2731
this.warning = warning;
2832
this.details =
2933
warning && typeof warning === "object" && warning.stack
@@ -32,6 +36,24 @@ class ModuleWarning extends WebpackError {
3236

3337
Error.captureStackTrace(this, this.constructor);
3438
}
39+
40+
serialize(context) {
41+
const { write } = context;
42+
43+
write(this.warning);
44+
45+
super.serialize(context);
46+
}
47+
48+
deserialize(context) {
49+
const { read } = context;
50+
51+
this.warning = read();
52+
53+
super.deserialize(context);
54+
}
3555
}
3656

57+
makeSerializable(ModuleWarning, "webpack/lib/ModuleWarning");
58+
3759
module.exports = ModuleWarning;

lib/NormalModule.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const makeSerializable = require("./util/makeSerializable");
3939
/** @typedef {import("./Module").SourceContext} SourceContext */
4040
/** @typedef {import("./RequestShortener")} RequestShortener */
4141
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
42-
/** @typedef {import("./WebpackError")} WebpackError */
4342
/** @typedef {import("./util/createHash").Hash} Hash */
4443

4544
const EARLY_RETURN_ERROR = new Error("flags early return is not an error");
@@ -69,6 +68,12 @@ class NonErrorEmittedError extends WebpackError {
6968
}
7069
}
7170

71+
makeSerializable(
72+
NonErrorEmittedError,
73+
"webpack/lib/NormalModule",
74+
"NonErrorEmittedError"
75+
);
76+
7277
/**
7378
* @typedef {Object} CachedSourceEntry
7479
* @property {TODO} source the generated source
@@ -195,7 +200,7 @@ class NormalModule extends Module {
195200
}
196201
const currentLoader = this.getCurrentLoader(loaderContext);
197202
this.warnings.push(
198-
new ModuleWarning(this, warning, {
203+
new ModuleWarning(warning, {
199204
from: requestShortener.shorten(currentLoader.loader)
200205
})
201206
);
@@ -206,7 +211,7 @@ class NormalModule extends Module {
206211
}
207212
const currentLoader = this.getCurrentLoader(loaderContext);
208213
this.errors.push(
209-
new ModuleError(this, error, {
214+
new ModuleError(error, {
210215
from: requestShortener.shorten(currentLoader.loader)
211216
})
212217
);
@@ -313,7 +318,7 @@ class NormalModule extends Module {
313318
err = new NonErrorEmittedError(err);
314319
}
315320
const currentLoader = this.getCurrentLoader(loaderContext);
316-
const error = new ModuleBuildError(this, err, {
321+
const error = new ModuleBuildError(err, {
317322
from:
318323
currentLoader &&
319324
compilation.runtimeTemplate.requestShortener.shorten(
@@ -339,7 +344,7 @@ class NormalModule extends Module {
339344
: "unknown"
340345
}) didn't return a Buffer or String`
341346
);
342-
const error = new ModuleBuildError(this, err);
347+
const error = new ModuleBuildError(err);
343348
return callback(error);
344349
}
345350

@@ -469,7 +474,7 @@ class NormalModule extends Module {
469474

470475
const handleParseError = e => {
471476
const source = this._source.source();
472-
const error = new ModuleParseError(this, source, e);
477+
const error = new ModuleParseError(source, e);
473478
this.markModuleAsErrored(error);
474479
this._initBuildHash(compilation);
475480
return callback();

0 commit comments

Comments
 (0)