Skip to content

Commit

Permalink
feat(types): support custom VitePreloadErrorEvent (#17615)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Jul 30, 2024
1 parent bbf001f commit 116e37a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 17 additions & 0 deletions packages/vite/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,20 @@ declare module '*?inline' {
const src: string
export default src
}

declare interface VitePreloadErrorEvent extends Event {
payload: Error
}

declare interface Window {
addEventListener(
type: 'vite:preloadError',
listener: (this: Window, ev: VitePreloadErrorEvent) => unknown,
options?: boolean | AddEventListenerOptions,
): void
removeEventListener(
type: 'vite:preloadError',
listener: (this: Window, ev: VitePreloadErrorEvent) => unknown,
options?: boolean | EventListenerOptions,
): void
}
7 changes: 5 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type FileDep = {
runtime: boolean
}

type VitePreloadErrorEvent = Event & { payload: Error }

/**
* A flag for injected helpers. This flag will be set to `false` if the output
* target is not native es - so that injected helper logic can be conditionally
Expand Down Expand Up @@ -145,8 +147,9 @@ function preload(
return promise
.then(() => baseModule())
.catch((err) => {
const e = new Event('vite:preloadError', { cancelable: true })
// @ts-expect-error custom payload
const e = new Event('vite:preloadError', {
cancelable: true,
}) as VitePreloadErrorEvent
e.payload = err
window.dispatchEvent(e)
if (!e.defaultPrevented) {
Expand Down

0 comments on commit 116e37a

Please sign in to comment.