Skip to content

Commit

Permalink
Avoid errors when loading the overlay code in workers.
Browse files Browse the repository at this point in the history
`HTMLElement` and `customElements` are not defined in e.g. web workers. However, Vite sometimes tries to inject the overlay into such workers. Ideally, it would not do so (and would e.g. find another way to notify the developer about issues). But this PR offers a change that allows such workers to run, by avoiding crashes in `overlay.ts` when it does.
  • Loading branch information
lgarron committed Jul 13, 2022
1 parent f020066 commit 9bbeec2
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ code {
const fileRE = /(?:[a-zA-Z]:\\|\/).*?:\d+:\d+/g
const codeframeRE = /^(?:>?\s+\d+\s+\|.*|\s+\|\s*\^.*)\r?\n/gm

// Allow `ErrorOverlay` to extend `HTMLElement` even in environments where
// `HTMLElement` was not originally defined.
const { HTMLElement = class { } } = globalThis;
export class ErrorOverlay extends HTMLElement {
root: ShadowRoot

Expand Down Expand Up @@ -184,6 +187,7 @@ export class ErrorOverlay extends HTMLElement {
}

export const overlayId = 'vite-error-overlay'
const { customElements } = globalThis; // Ensure `customElements` is defined before the next line.
if (customElements && !customElements.get(overlayId)) {
customElements.define(overlayId, ErrorOverlay)
}

0 comments on commit 9bbeec2

Please sign in to comment.