Skip to content
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

feat: convert overlay template to DOM #15852

Merged
merged 9 commits into from
Mar 12, 2024
85 changes: 67 additions & 18 deletions packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,36 @@ declare const __HMR_CONFIG_NAME__: string
const hmrConfigName = __HMR_CONFIG_NAME__
const base = __BASE__ || '/'

// set :host styles to make playwright detect the element as visible
const template = /*html*/ `
<style>
// Create an element with provided attributes and optional children
function h(
e: string,
attrs: Record<string, string> = {},
...children: (string | Node)[]
) {
const elem = document.createElement(e)
for (const [k, v] of Object.entries(attrs)) {
if (k === 'class') {
elem.className = v
} else {
elem.setAttribute(k, v)
}
Snugug marked this conversation as resolved.
Show resolved Hide resolved
}

// Append children
for (const child of children) {
// If a child is a string, create a text node
if (typeof child === 'string') {
elem.appendChild(document.createTextNode(child))
} else {
// Otherwise, append the child node
elem.appendChild(child)
}
}

Snugug marked this conversation as resolved.
Show resolved Hide resolved
return elem
}

const templateStyle = `
Snugug marked this conversation as resolved.
Show resolved Hide resolved
:host {
position: fixed;
top: 0;
Expand Down Expand Up @@ -148,22 +175,43 @@ kbd {
border-color: rgb(54, 57, 64);
border-image: initial;
}
</style>
<div class="backdrop" part="backdrop">
<div class="window" part="window">
<pre class="message" part="message"><span class="plugin" part="plugin"></span><span class="message-body" part="message-body"></span></pre>
<pre class="file" part="file"></pre>
<pre class="frame" part="frame"></pre>
<pre class="stack" part="stack"></pre>
<div class="tip" part="tip">
Click outside, press <kbd>Esc</kbd> key, or fix the code to dismiss.<br>
You can also disable this overlay by setting
<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>
</div>
</div>
</div>
`

// 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

Expand All @@ -177,7 +225,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)
Expand Down