-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: allow filter overlay message with function #4813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
4658fde
9e63422
9c6e408
6c017eb
2132c30
f7386f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,12 +10,20 @@ import sendMessage from "./utils/sendMessage.js"; | |||||||||||||||
import reloadApp from "./utils/reloadApp.js"; | ||||||||||||||||
import createSocketURL from "./utils/createSocketURL.js"; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* @typedef {Object} OverlayOptions | ||||||||||||||||
* @property {boolean | (error: Error) => boolean} [warnings] | ||||||||||||||||
* @property {boolean | (error: Error) => boolean} [errors] | ||||||||||||||||
* @property {boolean | (error: Error) => boolean} [runtimeErrors] | ||||||||||||||||
* @property {string} [trustedTypesPolicyName] | ||||||||||||||||
*/ | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* @typedef {Object} Options | ||||||||||||||||
* @property {boolean} hot | ||||||||||||||||
* @property {boolean} liveReload | ||||||||||||||||
* @property {boolean} progress | ||||||||||||||||
* @property {boolean | { warnings?: boolean, errors?: boolean, runtimeErrors?: boolean, trustedTypesPolicyName?: string }} overlay | ||||||||||||||||
* @property {boolean | OverlayOptions} overlay | ||||||||||||||||
* @property {string} [logging] | ||||||||||||||||
* @property {number} [reconnect] | ||||||||||||||||
*/ | ||||||||||||||||
|
@@ -83,6 +91,23 @@ if (parsedResourceQuery.overlay) { | |||||||||||||||
runtimeErrors: true, | ||||||||||||||||
...options.overlay, | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
["errors", "warnings", "runtimeErrors"].forEach((property) => { | ||||||||||||||||
if (typeof options.overlay[property] === "string") { | ||||||||||||||||
malcolm-kee marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
const overlayFilterFunctionString = decodeURIComponent( | ||||||||||||||||
options.overlay[property] | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
// eslint-disable-next-line no-new-func | ||||||||||||||||
const overlayFilterFunction = new Function( | ||||||||||||||||
"message", | ||||||||||||||||
`var callback = ${overlayFilterFunctionString} | ||||||||||||||||
return callback(message)` | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
options.overlay[property] = overlayFilterFunction; | ||||||||||||||||
} | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
enabledFeatures.Overlay = true; | ||||||||||||||||
} | ||||||||||||||||
|
@@ -266,17 +291,24 @@ const onSocketMessage = { | |||||||||||||||
log.warn(printableWarnings[i]); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const needShowOverlayForWarnings = | ||||||||||||||||
const overlayWarningsSetting = | ||||||||||||||||
typeof options.overlay === "boolean" | ||||||||||||||||
? options.overlay | ||||||||||||||||
: options.overlay && options.overlay.warnings; | ||||||||||||||||
|
||||||||||||||||
if (needShowOverlayForWarnings) { | ||||||||||||||||
overlay.send({ | ||||||||||||||||
type: "BUILD_ERROR", | ||||||||||||||||
level: "warning", | ||||||||||||||||
messages: warnings, | ||||||||||||||||
}); | ||||||||||||||||
if (overlayWarningsSetting) { | ||||||||||||||||
const warningsToDisplay = | ||||||||||||||||
typeof overlayWarningsSetting === "function" | ||||||||||||||||
? warnings.filter(overlayWarningsSetting) | ||||||||||||||||
: warnings; | ||||||||||||||||
|
||||||||||||||||
if (warningsToDisplay.length) { | ||||||||||||||||
overlay.send({ | ||||||||||||||||
type: "BUILD_ERROR", | ||||||||||||||||
level: "warning", | ||||||||||||||||
messages: warnings, | ||||||||||||||||
Comment on lines
+317
to
+319
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the event type purpose is to determine how to transition the state: webpack-dev-server/client-src/overlay/state-machine.js Lines 32 to 38 in f7386f6
I don't mind refactor to add event type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine 👍🏻 |
||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if (params && params.preventReloading) { | ||||||||||||||||
|
@@ -303,17 +335,24 @@ const onSocketMessage = { | |||||||||||||||
log.error(printableErrors[i]); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const needShowOverlayForErrors = | ||||||||||||||||
const overlayErrorsSettings = | ||||||||||||||||
typeof options.overlay === "boolean" | ||||||||||||||||
? options.overlay | ||||||||||||||||
: options.overlay && options.overlay.errors; | ||||||||||||||||
|
||||||||||||||||
if (needShowOverlayForErrors) { | ||||||||||||||||
overlay.send({ | ||||||||||||||||
type: "BUILD_ERROR", | ||||||||||||||||
level: "error", | ||||||||||||||||
messages: errors, | ||||||||||||||||
}); | ||||||||||||||||
if (overlayErrorsSettings) { | ||||||||||||||||
const errorsToDisplay = | ||||||||||||||||
typeof overlayErrorsSettings === "function" | ||||||||||||||||
? errors.filter(overlayErrorsSettings) | ||||||||||||||||
: errors; | ||||||||||||||||
|
||||||||||||||||
if (errorsToDisplay.length) { | ||||||||||||||||
overlay.send({ | ||||||||||||||||
type: "BUILD_ERROR", | ||||||||||||||||
level: "error", | ||||||||||||||||
messages: errors, | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
}, | ||||||||||||||||
/** | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
"use strict"; | ||
|
||
function unsafeOperation() { | ||
throw new Error("Error message thrown from JS"); | ||
} | ||
/** | ||
* | ||
* @param {string} label | ||
* @param {string} errorMessage | ||
* @returns HTMLButtonElement | ||
*/ | ||
module.exports = function createErrorButton(label, errorMessage) { | ||
function unsafeOperation() { | ||
throw new Error(errorMessage); | ||
} | ||
|
||
function handleButtonClick() { | ||
unsafeOperation(); | ||
} | ||
function handleButtonClick() { | ||
unsafeOperation(); | ||
} | ||
|
||
module.exports = function createErrorButton() { | ||
const errorBtn = document.createElement("button"); | ||
|
||
errorBtn.addEventListener("click", handleButtonClick); | ||
errorBtn.innerHTML = "Click to throw error"; | ||
errorBtn.innerHTML = label; | ||
|
||
return errorBtn; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,10 +154,14 @@ const schema = require("./options.json"); | |
* @property {string} [username] | ||
*/ | ||
|
||
/** | ||
* @typedef {boolean | ((error: Error) => void)} OverlayMessageOptions | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} ClientConfiguration | ||
* @property {"log" | "info" | "warn" | "error" | "none" | "verbose"} [logging] | ||
* @property {boolean | { warnings?: boolean, errors?: boolean, runtimeErrors?: boolean }} [overlay] | ||
* @property {boolean | { warnings?: OverlayMessageOptions, errors?: OverlayMessageOptions, runtimeErrors?: OverlayMessageOptions }} [overlay] | ||
* @property {boolean} [progress] | ||
* @property {boolean | number} [reconnect] | ||
* @property {"ws" | "sockjs" | string} [webSocketTransport] | ||
|
@@ -654,12 +658,28 @@ class Server { | |
} | ||
|
||
if (typeof client.overlay !== "undefined") { | ||
searchParams.set( | ||
"overlay", | ||
/** | ||
* | ||
* @param {OverlayMessageOptions} [setting] | ||
* @returns | ||
*/ | ||
const encodeOverlaySettings = (setting) => | ||
typeof setting === "function" | ||
? encodeURIComponent(setting.toString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usage of encodeURIComponent here is intentional, because without it Alternative is to use |
||
: setting; | ||
|
||
const overlayString = | ||
typeof client.overlay === "boolean" | ||
? String(client.overlay) | ||
: JSON.stringify(client.overlay) | ||
); | ||
: JSON.stringify({ | ||
errors: encodeOverlaySettings(client.overlay.errors), | ||
warnings: encodeOverlaySettings(client.overlay.warnings), | ||
runtimeErrors: encodeOverlaySettings( | ||
client.overlay.runtimeErrors | ||
), | ||
}); | ||
|
||
searchParams.set("overlay", overlayString); | ||
} | ||
|
||
if (typeof client.reconnect !== "undefined") { | ||
|
Uh oh!
There was an error while loading. Please reload this page.