Skip to content

Commit dd49505

Browse files
Snugugsapphi-redpatak-dev
authored
feat: convert overlay template to DOM (#15852)
Co-authored-by: 翠 / green <[email protected]> Co-authored-by: patak <[email protected]>
1 parent 3efb1a1 commit dd49505

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

packages/vite/src/client/overlay.ts

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@ declare const __HMR_CONFIG_NAME__: string
77
const hmrConfigName = __HMR_CONFIG_NAME__
88
const base = __BASE__ || '/'
99

10+
// Create an element with provided attributes and optional children
11+
function h(
12+
e: string,
13+
attrs: Record<string, string> = {},
14+
...children: (string | Node)[]
15+
) {
16+
const elem = document.createElement(e)
17+
for (const [k, v] of Object.entries(attrs)) {
18+
elem.setAttribute(k, v)
19+
}
20+
elem.append(...children)
21+
return elem
22+
}
23+
1024
// set :host styles to make playwright detect the element as visible
11-
const template = /*html*/ `
12-
<style>
25+
const templateStyle = /*css*/ `
1326
:host {
1427
position: fixed;
1528
top: 0;
@@ -149,22 +162,43 @@ kbd {
149162
border-color: rgb(54, 57, 64);
150163
border-image: initial;
151164
}
152-
</style>
153-
<div class="backdrop" part="backdrop">
154-
<div class="window" part="window">
155-
<pre class="message" part="message"><span class="plugin" part="plugin"></span><span class="message-body" part="message-body"></span></pre>
156-
<pre class="file" part="file"></pre>
157-
<pre class="frame" part="frame"></pre>
158-
<pre class="stack" part="stack"></pre>
159-
<div class="tip" part="tip">
160-
Click outside, press <kbd>Esc</kbd> key, or fix the code to dismiss.<br>
161-
You can also disable this overlay by setting
162-
<code part="config-option-name">server.hmr.overlay</code> to <code part="config-option-value">false</code> in <code part="config-file-name">${hmrConfigName}.</code>
163-
</div>
164-
</div>
165-
</div>
166165
`
167166

167+
// Error Template
168+
const template = h(
169+
'div',
170+
{ class: 'backdrop', part: 'backdrop' },
171+
h(
172+
'div',
173+
{ class: 'window', part: 'window' },
174+
h(
175+
'pre',
176+
{ class: 'message', part: 'message' },
177+
h('span', { class: 'plugin', part: 'plugin' }),
178+
h('span', { class: 'message-body', part: 'message-body' }),
179+
),
180+
h('pre', { class: 'file', part: 'file' }),
181+
h('pre', { class: 'frame', part: 'frame' }),
182+
h('pre', { class: 'stack', part: 'stack' }),
183+
h(
184+
'div',
185+
{ class: 'tip', part: 'tip' },
186+
'Click outside, press ',
187+
h('kbd', {}, 'Esc'),
188+
' key, or fix the code to dismiss.',
189+
h('br'),
190+
'You can also disable this overlay by setting ',
191+
h('code', { part: 'config-option-name' }, 'server.hmr.overlay'),
192+
' to ',
193+
h('code', { part: 'config-option-value' }, 'false'),
194+
' in ',
195+
h('code', { part: 'config-file-name' }, hmrConfigName),
196+
'.',
197+
),
198+
),
199+
h('style', {}, templateStyle),
200+
)
201+
168202
const fileRE = /(?:[a-zA-Z]:\\|\/).*?:\d+:\d+/g
169203
const codeframeRE = /^(?:>?\s*\d+\s+\|.*|\s+\|\s*\^.*)\r?\n/gm
170204

@@ -178,7 +212,8 @@ export class ErrorOverlay extends HTMLElement {
178212
constructor(err: ErrorPayload['err'], links = true) {
179213
super()
180214
this.root = this.attachShadow({ mode: 'open' })
181-
this.root.innerHTML = template
215+
216+
this.root.appendChild(template)
182217

183218
codeframeRE.lastIndex = 0
184219
const hasFrame = err.frame && codeframeRE.test(err.frame)

0 commit comments

Comments
 (0)