Skip to content

Commit

Permalink
fix(preload): backport #18098, throw error preloading module as well (#…
Browse files Browse the repository at this point in the history
…18099)

Co-authored-by: Daniel Roe <daniel@roe.dev>
  • Loading branch information
patak-dev and danielroe authored Sep 13, 2024
1 parent 54c55db commit faa2405
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,23 @@ function preload(
)
}

function handlePreloadError(err: Error) {
const e = new Event('vite:preloadError', {
cancelable: true,
}) as VitePreloadErrorEvent
e.payload = err
window.dispatchEvent(e)
if (!e.defaultPrevented) {
throw err
}
}

return promise.then((res) => {
for (const item of res || []) {
if (item.status !== 'rejected') continue

const e = new Event('vite:preloadError', {
cancelable: true,
}) as VitePreloadErrorEvent
e.payload = item.reason
window.dispatchEvent(e)
if (!e.defaultPrevented) {
throw item.reason
}
handlePreloadError(item.reason)
}
return baseModule()

This comment has been minimized.

return baseModule().catch(handlePreloadError)
})
}

Expand Down
2 changes: 1 addition & 1 deletion playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe.runIf(isBuild)('build tests', () => {
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"ignoreList": [],
"mappings": ";s8BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"mappings": ";+8BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
Expand Down

0 comments on commit faa2405

Please sign in to comment.