Skip to content

Commit 8760293

Browse files
patak-devdanielroe
andauthored
fix(preload): backport #18046, allow ignoring dep errors (#18076)
Co-authored-by: Daniel Roe <[email protected]>
1 parent ccbfc1a commit 8760293

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

packages/vite/src/node/plugins/importAnalysisBuild.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ function preload(
8181
deps?: string[],
8282
importerUrl?: string,
8383
) {
84-
let promise: Promise<unknown> = Promise.resolve()
84+
let promise: Promise<PromiseSettledResult<unknown>[] | void> =
85+
Promise.resolve()
8586
// @ts-expect-error __VITE_IS_MODERN__ will be replaced with boolean later
8687
if (__VITE_IS_MODERN__ && deps && deps.length > 0) {
8788
const links = document.getElementsByTagName('link')
@@ -93,7 +94,7 @@ function preload(
9394
// in that case fallback to getAttribute
9495
const cspNonce = cspNonceMeta?.nonce || cspNonceMeta?.getAttribute('nonce')
9596

96-
promise = Promise.all(
97+
promise = Promise.allSettled(
9798
deps.map((dep) => {
9899
// @ts-expect-error assetsURL is declared before preload.toString()
99100
dep = assetsURL(dep, importerUrl)
@@ -144,18 +145,21 @@ function preload(
144145
)
145146
}
146147

147-
return promise
148-
.then(() => baseModule())
149-
.catch((err) => {
148+
return promise.then((res) => {
149+
for (const item of res || []) {
150+
if (item.status !== 'rejected') continue
151+
150152
const e = new Event('vite:preloadError', {
151153
cancelable: true,
152154
}) as VitePreloadErrorEvent
153-
e.payload = err
155+
e.payload = item.reason
154156
window.dispatchEvent(e)
155157
if (!e.defaultPrevented) {
156-
throw err
158+
throw item.reason
157159
}
158-
})
160+
}
161+
return baseModule()
162+
})
159163
}
160164

161165
/**

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe.runIf(isBuild)('build tests', () => {
140140
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
141141
{
142142
"ignoreList": [],
143-
"mappings": ";63BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
143+
"mappings": ";s8BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
144144
"sources": [
145145
"../../after-preload-dynamic.js",
146146
],

0 commit comments

Comments
 (0)