Skip to content

Commit dca2366

Browse files
authoredMar 17, 2023
feat: added client.overlay.runtimeErrors option to control runtime errors (#4773)
1 parent 46e02f3 commit dca2366

11 files changed

+251
-221
lines changed
 

‎bin/cli-flags.js

+17
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,23 @@ module.exports = {
153153
simpleType: "boolean",
154154
multiple: false,
155155
},
156+
"client-overlay-runtime-errors": {
157+
configs: [
158+
{
159+
type: "boolean",
160+
multiple: false,
161+
description:
162+
"Enables a full-screen overlay in the browser when there are uncaught runtime errors.",
163+
negatedDescription:
164+
"Disables the full-screen overlay in the browser when there are uncaught runtime errors.",
165+
path: "client.overlay.runtimeErrors",
166+
},
167+
],
168+
description:
169+
"Enables a full-screen overlay in the browser when there are uncaught runtime errors.",
170+
simpleType: "boolean",
171+
multiple: false,
172+
},
156173
"client-progress": {
157174
configs: [
158175
{

‎client-src/index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import createSocketURL from "./utils/createSocketURL.js";
1515
* @property {boolean} hot
1616
* @property {boolean} liveReload
1717
* @property {boolean} progress
18-
* @property {boolean | { warnings?: boolean, errors?: boolean, trustedTypesPolicyName?: string }} overlay
18+
* @property {boolean | { warnings?: boolean, errors?: boolean, runtimeErrors?: boolean, trustedTypesPolicyName?: string }} overlay
1919
* @property {string} [logging]
2020
* @property {number} [reconnect]
2121
*/
@@ -80,6 +80,7 @@ if (parsedResourceQuery.overlay) {
8080
options.overlay = {
8181
errors: true,
8282
warnings: true,
83+
runtimeErrors: true,
8384
...options.overlay,
8485
};
8586
}
@@ -115,12 +116,20 @@ self.addEventListener("beforeunload", () => {
115116
status.isUnloading = true;
116117
});
117118

118-
const trustedTypesPolicyName =
119-
typeof options.overlay === "object" && options.overlay.trustedTypesPolicyName;
120-
121-
const overlay = createOverlay({
122-
trustedTypesPolicyName,
123-
});
119+
const overlay =
120+
typeof window !== "undefined"
121+
? createOverlay(
122+
typeof options.overlay === "object"
123+
? {
124+
trustedTypesPolicyName: options.overlay.trustedTypesPolicyName,
125+
catchRuntimeError: options.overlay.runtimeErrors,
126+
}
127+
: {
128+
trustedTypesPolicyName: false,
129+
catchRuntimeError: options.overlay,
130+
}
131+
)
132+
: { send: () => {} };
124133

125134
const onSocketMessage = {
126135
hot() {

‎client-src/overlay.js

+23-17
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function formatProblem(type, item) {
7878
/**
7979
* @typedef {Object} CreateOverlayOptions
8080
* @property {string | null} trustedTypesPolicyName
81+
* @property {boolean} [catchRuntimeError]
8182
*/
8283

8384
/**
@@ -271,24 +272,29 @@ const createOverlay = (options) => {
271272
hideOverlay: hide,
272273
});
273274

274-
listenToRuntimeError((errorEvent) => {
275-
// error property may be empty in older browser like IE
276-
const { error, message } = errorEvent;
277-
if (!error && !message) {
278-
return;
279-
}
280-
const errorObject =
281-
error instanceof Error ? error : new Error(error || message);
282-
overlayService.send({
283-
type: "RUNTIME_ERROR",
284-
messages: [
285-
{
286-
message: errorObject.message,
287-
stack: parseErrorToStacks(errorObject),
288-
},
289-
],
275+
if (options.catchRuntimeError) {
276+
listenToRuntimeError((errorEvent) => {
277+
// error property may be empty in older browser like IE
278+
const { error, message } = errorEvent;
279+
280+
if (!error && !message) {
281+
return;
282+
}
283+
284+
const errorObject =
285+
error instanceof Error ? error : new Error(error || message);
286+
287+
overlayService.send({
288+
type: "RUNTIME_ERROR",
289+
messages: [
290+
{
291+
message: errorObject.message,
292+
stack: parseErrorToStacks(errorObject),
293+
},
294+
],
295+
});
290296
});
291-
});
297+
}
292298

293299
return overlayService;
294300
};

‎lib/Server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const schema = require("./options.json");
159159
/**
160160
* @typedef {Object} ClientConfiguration
161161
* @property {"log" | "info" | "warn" | "error" | "none" | "verbose"} [logging]
162-
* @property {boolean | { warnings?: boolean, errors?: boolean }} [overlay]
162+
* @property {boolean | { warnings?: boolean, errors?: boolean, runtimeErrors?: boolean }} [overlay]
163163
* @property {boolean} [progress]
164164
* @property {boolean | number} [reconnect]
165165
* @property {"ws" | "sockjs" | string} [webSocketTransport]

‎lib/options.json

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@
111111
"negatedDescription": "Disables the full-screen overlay in the browser when there are compiler warnings."
112112
}
113113
},
114+
"runtimeErrors": {
115+
"description": "Enables a full-screen overlay in the browser when there are uncaught runtime errors.",
116+
"type": "boolean",
117+
"cli": {
118+
"negatedDescription": "Disables the full-screen overlay in the browser when there are uncaught runtime errors."
119+
}
120+
},
114121
"trustedTypesPolicyName": {
115122
"description": "The name of a Trusted Types policy for the overlay. Defaults to 'webpack-dev-server#overlay'.",
116123
"type": "string"

‎test/__snapshots__/validate-options.test.js.snap.webpack4

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ exports[`options validate should throw an error on the "client" option with '{"o
9090
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverclient
9191
Details:
9292
* options.client.overlay should be one of these:
93-
boolean | object { errors?, warnings?, trustedTypesPolicyName? }
93+
boolean | object { errors?, warnings?, runtimeErrors?, trustedTypesPolicyName? }
9494
Details:
9595
* options.client.overlay should be a boolean.
9696
-> Enables a full-screen overlay in the browser when there are compiler errors or warnings.
9797
-> Read more at https://webpack.js.org/configuration/dev-server/#overlay
9898
* options.client.overlay should be an object:
99-
object { errors?, warnings?, trustedTypesPolicyName? }"
99+
object { errors?, warnings?, runtimeErrors?, trustedTypesPolicyName? }"
100100
`;
101101

102102
exports[`options validate should throw an error on the "client" option with '{"overlay":{"arbitrary":""}}' value 1`] = `
103103
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
104104
- options.client.overlay has an unknown property 'arbitrary'. These properties are valid:
105-
object { errors?, warnings?, trustedTypesPolicyName? }"
105+
object { errors?, warnings?, runtimeErrors?, trustedTypesPolicyName? }"
106106
`;
107107

108108
exports[`options validate should throw an error on the "client" option with '{"overlay":{"errors":""}}' value 1`] = `

‎test/__snapshots__/validate-options.test.js.snap.webpack5

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ exports[`options validate should throw an error on the "client" option with '{"o
9090
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverclient
9191
Details:
9292
* options.client.overlay should be one of these:
93-
boolean | object { errors?, warnings?, trustedTypesPolicyName? }
93+
boolean | object { errors?, warnings?, runtimeErrors?, trustedTypesPolicyName? }
9494
Details:
9595
* options.client.overlay should be a boolean.
9696
-> Enables a full-screen overlay in the browser when there are compiler errors or warnings.
9797
-> Read more at https://webpack.js.org/configuration/dev-server/#overlay
9898
* options.client.overlay should be an object:
99-
object { errors?, warnings?, trustedTypesPolicyName? }"
99+
object { errors?, warnings?, runtimeErrors?, trustedTypesPolicyName? }"
100100
`;
101101

102102
exports[`options validate should throw an error on the "client" option with '{"overlay":{"arbitrary":""}}' value 1`] = `
103103
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
104104
- options.client.overlay has an unknown property 'arbitrary'. These properties are valid:
105-
object { errors?, warnings?, trustedTypesPolicyName? }"
105+
object { errors?, warnings?, runtimeErrors?, trustedTypesPolicyName? }"
106106
`;
107107

108108
exports[`options validate should throw an error on the "client" option with '{"overlay":{"errors":""}}' value 1`] = `

‎test/cli/__snapshots__/basic.test.js.snap.webpack4

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Options:
6767
--client-overlay-trusted-types-policy-name <value> The name of a Trusted Types policy for the overlay. Defaults to 'webpack-dev-server#overlay'.
6868
--client-overlay-warnings Enables a full-screen overlay in the browser when there are compiler warnings.
6969
--no-client-overlay-warnings Disables the full-screen overlay in the browser when there are compiler warnings.
70+
--client-overlay-runtime-errors Enables a full-screen overlay in the browser when there are uncaught runtime errors.
71+
--no-client-overlay-runtime-errors Disables the full-screen overlay in the browser when there are uncaught runtime errors.
7072
--client-progress Prints compilation progress in percentage in the browser.
7173
--no-client-progress Does not print compilation progress in percentage in the browser.
7274
--client-reconnect [value] Tells dev-server the number of times it should try to reconnect the client.

‎test/cli/__snapshots__/basic.test.js.snap.webpack5

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Options:
6767
--no-client-overlay-errors Disables the full-screen overlay in the browser when there are compiler errors.
6868
--client-overlay-warnings Enables a full-screen overlay in the browser when there are compiler warnings.
6969
--no-client-overlay-warnings Disables the full-screen overlay in the browser when there are compiler warnings.
70+
--client-overlay-runtime-errors Enables a full-screen overlay in the browser when there are uncaught runtime errors.
71+
--no-client-overlay-runtime-errors Disables the full-screen overlay in the browser when there are uncaught runtime errors.
7072
--client-overlay-trusted-types-policy-name <value> The name of a Trusted Types policy for the overlay. Defaults to 'webpack-dev-server#overlay'.
7173
--client-progress Prints compilation progress in percentage in the browser.
7274
--no-client-progress Does not print compilation progress in percentage in the browser.

‎types/bin/cli-flags.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ declare const _exports: {
114114
simpleType: string;
115115
multiple: boolean;
116116
};
117+
"client-overlay-runtime-errors": {
118+
configs: {
119+
type: string;
120+
multiple: boolean;
121+
description: string;
122+
negatedDescription: string;
123+
path: string;
124+
}[];
125+
description: string;
126+
simpleType: string;
127+
multiple: boolean;
128+
};
117129
"client-progress": {
118130
configs: {
119131
type: string;

‎types/lib/Server.d.ts

+165-190
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.