From 2eca54eb736b21e48273a2083391dbd734d400f5 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 15 Jun 2023 13:37:02 +0100 Subject: [PATCH] feat: emit event to handle chunk load errors (#12084) --- .../vite/src/node/plugins/importAnalysisBuild.ts | 12 +++++++++++- .../js-sourcemap/__tests__/js-sourcemap.spec.ts | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index aa9808e56886d2..368207edb04516 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -132,7 +132,17 @@ function preload( }) } }), - ).then(() => baseModule()) + ) + .then(() => baseModule()) + .catch((err) => { + const e = new Event('vite:preloadError', { cancelable: true }) + // @ts-expect-error custom payload + e.payload = err + window.dispatchEvent(e) + if (!e.defaultPrevented) { + throw err + } + }) } /** diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index f3c78604287a7c..02f18ec7ce86c4 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -54,7 +54,7 @@ describe.runIf(isBuild)('build tests', () => { const map = findAssetFile(/after-preload-dynamic.*\.js\.map/) expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(` { - "mappings": "stBAAAA,EAAA,WAAO,2BAAuB,EAAC,sEAE/B,QAAQ,IAAI,uBAAuB", + "mappings": "41BAAAA,EAAA,WAAO,2BAAuB,EAAC,sEAE/B,QAAQ,IAAI,uBAAuB", "sources": [ "../../after-preload-dynamic.js", ],