Skip to content

Commit

Permalink
fix(preload): throw error preloading module as well (#18098)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Sep 13, 2024
1 parent b897cb8 commit ba56cf4
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()
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 ba56cf4

Please sign in to comment.