Skip to content

Commit d8001c5

Browse files
authoredDec 1, 2023
fix: emit vite:preloadError for chunks without deps (#15203)
1 parent 676804d commit d8001c5

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed
 

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

+50-47
Original file line numberDiff line numberDiff line change
@@ -92,57 +92,60 @@ function preload(
9292
deps?: string[],
9393
importerUrl?: string,
9494
) {
95+
let promise: Promise<unknown> = Promise.resolve()
9596
// @ts-expect-error __VITE_IS_MODERN__ will be replaced with boolean later
96-
if (!__VITE_IS_MODERN__ || !deps || deps.length === 0) {
97-
return baseModule()
98-
}
99-
100-
const links = document.getElementsByTagName('link')
101-
102-
return Promise.all(
103-
deps.map((dep) => {
104-
// @ts-expect-error assetsURL is declared before preload.toString()
105-
dep = assetsURL(dep, importerUrl)
106-
if (dep in seen) return
107-
seen[dep] = true
108-
const isCss = dep.endsWith('.css')
109-
const cssSelector = isCss ? '[rel="stylesheet"]' : ''
110-
const isBaseRelative = !!importerUrl
111-
112-
// check if the file is already preloaded by SSR markup
113-
if (isBaseRelative) {
114-
// When isBaseRelative is true then we have `importerUrl` and `dep` is
115-
// already converted to an absolute URL by the `assetsURL` function
116-
for (let i = links.length - 1; i >= 0; i--) {
117-
const link = links[i]
118-
// The `links[i].href` is an absolute URL thanks to browser doing the work
119-
// for us. See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:idl-domstring-5
120-
if (link.href === dep && (!isCss || link.rel === 'stylesheet')) {
121-
return
97+
if (__VITE_IS_MODERN__ && deps && deps.length > 0) {
98+
const links = document.getElementsByTagName('link')
99+
100+
promise = Promise.all(
101+
deps.map((dep) => {
102+
// @ts-expect-error assetsURL is declared before preload.toString()
103+
dep = assetsURL(dep, importerUrl)
104+
if (dep in seen) return
105+
seen[dep] = true
106+
const isCss = dep.endsWith('.css')
107+
const cssSelector = isCss ? '[rel="stylesheet"]' : ''
108+
const isBaseRelative = !!importerUrl
109+
110+
// check if the file is already preloaded by SSR markup
111+
if (isBaseRelative) {
112+
// When isBaseRelative is true then we have `importerUrl` and `dep` is
113+
// already converted to an absolute URL by the `assetsURL` function
114+
for (let i = links.length - 1; i >= 0; i--) {
115+
const link = links[i]
116+
// The `links[i].href` is an absolute URL thanks to browser doing the work
117+
// for us. See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:idl-domstring-5
118+
if (link.href === dep && (!isCss || link.rel === 'stylesheet')) {
119+
return
120+
}
122121
}
122+
} else if (
123+
document.querySelector(`link[href="${dep}"]${cssSelector}`)
124+
) {
125+
return
123126
}
124-
} else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
125-
return
126-
}
127127

128-
const link = document.createElement('link')
129-
link.rel = isCss ? 'stylesheet' : scriptRel
130-
if (!isCss) {
131-
link.as = 'script'
132-
link.crossOrigin = ''
133-
}
134-
link.href = dep
135-
document.head.appendChild(link)
136-
if (isCss) {
137-
return new Promise((res, rej) => {
138-
link.addEventListener('load', res)
139-
link.addEventListener('error', () =>
140-
rej(new Error(`Unable to preload CSS for ${dep}`)),
141-
)
142-
})
143-
}
144-
}),
145-
)
128+
const link = document.createElement('link')
129+
link.rel = isCss ? 'stylesheet' : scriptRel
130+
if (!isCss) {
131+
link.as = 'script'
132+
link.crossOrigin = ''
133+
}
134+
link.href = dep
135+
document.head.appendChild(link)
136+
if (isCss) {
137+
return new Promise((res, rej) => {
138+
link.addEventListener('load', res)
139+
link.addEventListener('error', () =>
140+
rej(new Error(`Unable to preload CSS for ${dep}`)),
141+
)
142+
})
143+
}
144+
}),
145+
)
146+
}
147+
148+
return promise
146149
.then(() => baseModule())
147150
.catch((err) => {
148151
const e = new Event('vite:preloadError', { cancelable: true })

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe.runIf(isBuild)('build tests', () => {
116116
const map = findAssetFile(/after-preload-dynamic.*\.js\.map/)
117117
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
118118
{
119-
"mappings": "k2BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
119+
"mappings": "i3BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
120120
"sources": [
121121
"../../after-preload-dynamic.js",
122122
],

0 commit comments

Comments
 (0)