diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index f83bdd59c4ee43..19a6b19dcac64f 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -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) }) } diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index dd1d3151603af5..27a25a5445ec7f 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -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", ],