Skip to content

Commit

Permalink
fix(preload): backport #18046, allow ignoring dep errors (#18076)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Roe <[email protected]>
  • Loading branch information
patak-dev and danielroe authored Sep 11, 2024
1 parent ccbfc1a commit 8760293
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ function preload(
deps?: string[],
importerUrl?: string,
) {
let promise: Promise<unknown> = Promise.resolve()
let promise: Promise<PromiseSettledResult<unknown>[] | void> =
Promise.resolve()
// @ts-expect-error __VITE_IS_MODERN__ will be replaced with boolean later
if (__VITE_IS_MODERN__ && deps && deps.length > 0) {
const links = document.getElementsByTagName('link')
Expand All @@ -93,7 +94,7 @@ function preload(
// in that case fallback to getAttribute
const cspNonce = cspNonceMeta?.nonce || cspNonceMeta?.getAttribute('nonce')

promise = Promise.all(
promise = Promise.allSettled(

This comment has been minimized.

Copy link
@funfungo

funfungo Oct 14, 2024

Promise.allSettled seems have some compatibility problem in some browser, would this be ok
https://caniuse.com/?search=allSettled

This comment has been minimized.

Copy link
@patak-dev

patak-dev Oct 14, 2024

Author Member

This is fine, see #18046 (comment). Please create new issues when you find issues in PRs, these comments are very hard to track.

deps.map((dep) => {
// @ts-expect-error assetsURL is declared before preload.toString()
dep = assetsURL(dep, importerUrl)
Expand Down Expand Up @@ -144,18 +145,21 @@ function preload(
)
}

return promise
.then(() => baseModule())
.catch((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 = err
e.payload = item.reason
window.dispatchEvent(e)
if (!e.defaultPrevented) {
throw err
throw item.reason
}
})
}
return baseModule()
})
}

/**
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": ";63BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"mappings": ";s8BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
Expand Down

0 comments on commit 8760293

Please sign in to comment.