-
-
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 all 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] | ||||||||||||||||
*/ | ||||||||||||||||
|
@@ -27,6 +35,30 @@ import createSocketURL from "./utils/createSocketURL.js"; | |||||||||||||||
* @property {string} [previousHash] | ||||||||||||||||
*/ | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* @param {boolean | { warnings?: boolean | string; errors?: boolean | string; runtimeErrors?: boolean | string; }} overlayOptions | ||||||||||||||||
*/ | ||||||||||||||||
const decodeOverlayOptions = (overlayOptions) => { | ||||||||||||||||
if (typeof overlayOptions === "object") { | ||||||||||||||||
["warnings", "errors", "runtimeErrors"].forEach((property) => { | ||||||||||||||||
if (typeof overlayOptions[property] === "string") { | ||||||||||||||||
const overlayFilterFunctionString = decodeURIComponent( | ||||||||||||||||
overlayOptions[property] | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
// eslint-disable-next-line no-new-func | ||||||||||||||||
const overlayFilterFunction = new Function( | ||||||||||||||||
"message", | ||||||||||||||||
`var callback = ${overlayFilterFunctionString} | ||||||||||||||||
return callback(message)` | ||||||||||||||||
); | ||||||||||||||||
Comment on lines
+50
to
+54
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. this
it could be fixed by adding 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. @wentokay We can optionally inject this, can you open an issue? |
||||||||||||||||
|
||||||||||||||||
overlayOptions[property] = overlayFilterFunction; | ||||||||||||||||
} | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* @type {Status} | ||||||||||||||||
*/ | ||||||||||||||||
|
@@ -83,6 +115,8 @@ if (parsedResourceQuery.overlay) { | |||||||||||||||
runtimeErrors: true, | ||||||||||||||||
...options.overlay, | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
decodeOverlayOptions(options.overlay); | ||||||||||||||||
} | ||||||||||||||||
enabledFeatures.Overlay = true; | ||||||||||||||||
} | ||||||||||||||||
|
@@ -173,6 +207,7 @@ const onSocketMessage = { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
options.overlay = value; | ||||||||||||||||
decodeOverlayOptions(options.overlay); | ||||||||||||||||
}, | ||||||||||||||||
/** | ||||||||||||||||
* @param {number} value | ||||||||||||||||
|
@@ -266,17 +301,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 +345,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; | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.