preload() — vite Function Reference
Architecture documentation for the preload() function in importAnalysisBuild.ts from the vite codebase.
Entity Profile
Dependency Diagram
graph TD df86ee87_337a_caf3_087e_7c8c6c00a088["preload()"] 04ad4685_2ce3_556a_152b_c93668a74b3b["importAnalysisBuild.ts"] df86ee87_337a_caf3_087e_7c8c6c00a088 -->|defined in| 04ad4685_2ce3_556a_152b_c93668a74b3b style df86ee87_337a_caf3_087e_7c8c6c00a088 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/vite/src/node/plugins/importAnalysisBuild.ts lines 62–163
function preload(
baseModule: () => Promise<unknown>,
deps?: string[],
importerUrl?: string,
) {
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')
const cspNonceMeta = document.querySelector<HTMLMetaElement>(
'meta[property=csp-nonce]',
)
// `.nonce` should be used to get along with nonce hiding (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce#accessing_nonces_and_nonce_hiding)
// Firefox 67-74 uses modern chunks and supports CSP nonce, but does not support `.nonce`
// in that case fallback to getAttribute
const cspNonce = cspNonceMeta?.nonce || cspNonceMeta?.getAttribute('nonce')
// Promise.allSettled is not supported by Chrome 64-75, Firefox 67-70, Safari 11.1-12.1
function allSettled<T>(
promises: Array<T | PromiseLike<T>>,
): Promise<PromiseSettledResult<T>[]> {
return Promise.all(
promises.map((p) =>
Promise.resolve(p).then(
(value: T) => ({ status: 'fulfilled' as const, value }),
(reason: unknown) => ({ status: 'rejected' as const, reason }),
),
),
)
}
promise = allSettled(
deps.map((dep) => {
// @ts-expect-error assetsURL is declared before preload.toString()
dep = assetsURL(dep, importerUrl)
if (dep in seen) return
seen[dep] = true
const isCss = dep.endsWith('.css')
const cssSelector = isCss ? '[rel="stylesheet"]' : ''
const isBaseRelative = !!importerUrl
// check if the file is already preloaded by SSR markup
if (isBaseRelative) {
// When isBaseRelative is true then we have `importerUrl` and `dep` is
// already converted to an absolute URL by the `assetsURL` function
for (let i = links.length - 1; i >= 0; i--) {
const link = links[i]
// The `links[i].href` is an absolute URL thanks to browser doing the work
// for us. See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:idl-domstring-5
if (link.href === dep && (!isCss || link.rel === 'stylesheet')) {
return
}
}
} else if (
document.querySelector(`link[href="${dep}"]${cssSelector}`)
) {
return
}
const link = document.createElement('link')
link.rel = isCss ? 'stylesheet' : scriptRel
if (!isCss) {
link.as = 'script'
}
link.crossOrigin = ''
link.href = dep
if (cspNonce) {
link.setAttribute('nonce', cspNonce)
}
document.head.appendChild(link)
if (isCss) {
return new Promise((res, rej) => {
link.addEventListener('load', res)
link.addEventListener('error', () =>
rej(new Error(`Unable to preload CSS for ${dep}`)),
)
})
}
}),
)
Domain
Subdomains
Source
Frequently Asked Questions
What does preload() do?
preload() is a function in the vite codebase, defined in packages/vite/src/node/plugins/importAnalysisBuild.ts.
Where is preload() defined?
preload() is defined in packages/vite/src/node/plugins/importAnalysisBuild.ts at line 62.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free