diff --git a/packages/vite/src/client/overlay.ts b/packages/vite/src/client/overlay.ts index cb9ec0fefe6f8b..b0c649b7836138 100644 --- a/packages/vite/src/client/overlay.ts +++ b/packages/vite/src/client/overlay.ts @@ -7,9 +7,22 @@ declare const __HMR_CONFIG_NAME__: string const hmrConfigName = __HMR_CONFIG_NAME__ const base = __BASE__ || '/' +// Create an element with provided attributes and optional children +function h( + e: string, + attrs: Record = {}, + ...children: (string | Node)[] +) { + const elem = document.createElement(e) + for (const [k, v] of Object.entries(attrs)) { + elem.setAttribute(k, v) + } + elem.append(...children) + return elem +} + // set :host styles to make playwright detect the element as visible -const template = /*html*/ ` - -
-
-
-

-    

-    

-    
- Click outside, press Esc key, or fix the code to dismiss.
- You can also disable this overlay by setting - server.hmr.overlay to false in ${hmrConfigName}. -
-
-
` +// Error Template +const template = h( + 'div', + { class: 'backdrop', part: 'backdrop' }, + h( + 'div', + { class: 'window', part: 'window' }, + h( + 'pre', + { class: 'message', part: 'message' }, + h('span', { class: 'plugin', part: 'plugin' }), + h('span', { class: 'message-body', part: 'message-body' }), + ), + h('pre', { class: 'file', part: 'file' }), + h('pre', { class: 'frame', part: 'frame' }), + h('pre', { class: 'stack', part: 'stack' }), + h( + 'div', + { class: 'tip', part: 'tip' }, + 'Click outside, press ', + h('kbd', {}, 'Esc'), + ' key, or fix the code to dismiss.', + h('br'), + 'You can also disable this overlay by setting ', + h('code', { part: 'config-option-name' }, 'server.hmr.overlay'), + ' to ', + h('code', { part: 'config-option-value' }, 'false'), + ' in ', + h('code', { part: 'config-file-name' }, hmrConfigName), + '.', + ), + ), + h('style', {}, templateStyle), +) + const fileRE = /(?:[a-zA-Z]:\\|\/).*?:\d+:\d+/g const codeframeRE = /^(?:>?\s*\d+\s+\|.*|\s+\|\s*\^.*)\r?\n/gm @@ -177,7 +211,8 @@ export class ErrorOverlay extends HTMLElement { constructor(err: ErrorPayload['err'], links = true) { super() this.root = this.attachShadow({ mode: 'open' }) - this.root.innerHTML = template + + this.root.appendChild(template) codeframeRE.lastIndex = 0 const hasFrame = err.frame && codeframeRE.test(err.frame)