@@ -78,7 +78,7 @@ function formatProblem(type, item) {
7878/**
7979 * @typedef {Object } CreateOverlayOptions
8080 * @property {string | null } trustedTypesPolicyName
81- * @property {boolean } [catchRuntimeError]
81+ * @property {boolean | (error: Error) => void } [catchRuntimeError]
8282 */
8383
8484/**
@@ -90,6 +90,8 @@ const createOverlay = (options) => {
9090 let iframeContainerElement ;
9191 /** @type {HTMLDivElement | null | undefined } */
9292 let containerElement ;
93+ /** @type {HTMLDivElement | null | undefined } */
94+ let headerElement ;
9395 /** @type {Array<(element: HTMLDivElement) => void> } */
9496 let onLoadQueue = [ ] ;
9597 /** @type {TrustedTypePolicy | undefined } */
@@ -124,6 +126,7 @@ const createOverlay = (options) => {
124126 iframeContainerElement . id = "webpack-dev-server-client-overlay" ;
125127 iframeContainerElement . src = "about:blank" ;
126128 applyStyle ( iframeContainerElement , iframeStyle ) ;
129+
127130 iframeContainerElement . onload = ( ) => {
128131 const contentElement =
129132 /** @type {Document } */
@@ -141,7 +144,7 @@ const createOverlay = (options) => {
141144 contentElement . id = "webpack-dev-server-client-overlay-div" ;
142145 applyStyle ( contentElement , containerStyle ) ;
143146
144- const headerElement = document . createElement ( "div" ) ;
147+ headerElement = document . createElement ( "div" ) ;
145148
146149 headerElement . innerText = "Compiled with problems:" ;
147150 applyStyle ( headerElement , headerStyle ) ;
@@ -219,9 +222,15 @@ const createOverlay = (options) => {
219222 * @param {string } type
220223 * @param {Array<string | { moduleIdentifier?: string, moduleName?: string, loc?: string, message?: string }> } messages
221224 * @param {string | null } trustedTypesPolicyName
225+ * @param {'build' | 'runtime' } messageSource
222226 */
223- function show ( type , messages , trustedTypesPolicyName ) {
227+ function show ( type , messages , trustedTypesPolicyName , messageSource ) {
224228 ensureOverlayExists ( ( ) => {
229+ headerElement . innerText =
230+ messageSource === "runtime"
231+ ? "Uncaught runtime errors:"
232+ : "Compiled with problems:" ;
233+
225234 messages . forEach ( ( message ) => {
226235 const entryElement = document . createElement ( "div" ) ;
227236 const msgStyle =
@@ -267,8 +276,8 @@ const createOverlay = (options) => {
267276 }
268277
269278 const overlayService = createOverlayMachine ( {
270- showOverlay : ( { level = "error" , messages } ) =>
271- show ( level , messages , options . trustedTypesPolicyName ) ,
279+ showOverlay : ( { level = "error" , messages, messageSource } ) =>
280+ show ( level , messages , options . trustedTypesPolicyName , messageSource ) ,
272281 hideOverlay : hide ,
273282 } ) ;
274283
@@ -284,15 +293,22 @@ const createOverlay = (options) => {
284293 const errorObject =
285294 error instanceof Error ? error : new Error ( error || message ) ;
286295
287- overlayService . send ( {
288- type : "RUNTIME_ERROR" ,
289- messages : [
290- {
291- message : errorObject . message ,
292- stack : parseErrorToStacks ( errorObject ) ,
293- } ,
294- ] ,
295- } ) ;
296+ const shouldDisplay =
297+ typeof options . catchRuntimeError === "function"
298+ ? options . catchRuntimeError ( errorObject )
299+ : true ;
300+
301+ if ( shouldDisplay ) {
302+ overlayService . send ( {
303+ type : "RUNTIME_ERROR" ,
304+ messages : [
305+ {
306+ message : errorObject . message ,
307+ stack : parseErrorToStacks ( errorObject ) ,
308+ } ,
309+ ] ,
310+ } ) ;
311+ }
296312 } ) ;
297313 }
298314
0 commit comments