From dd49505cfd623cf547e957f18f4af99e64bf7c92 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 12 Mar 2024 09:12:10 -0400 Subject: [PATCH] feat: convert overlay template to DOM (#15852) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green Co-authored-by: patak --- packages/vite/src/client/overlay.ts | 69 ++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/packages/vite/src/client/overlay.ts b/packages/vite/src/client/overlay.ts index 17dbf00de7a288..dffdafd9197ddb 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 @@ -178,7 +212,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)