Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce bundle size for chunk imports #9491

Closed
wants to merge 10 commits into from
38 changes: 25 additions & 13 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -390,6 +390,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const s = new MagicString(code)
const rewroteMarkerStartPos = new Set() // position of the leading double quote

const fileDeps: string[] = []
const addFileDep = (url: string): number => {
const index = fileDeps.indexOf(url)
if (index !== -1) {
return index
} else {
return fileDeps.push(url) - 1
}
}

if (imports.length) {
for (let index = 0; index < imports.length; index++) {
// To handle escape sequences in specifier strings, the .n field will be provided where possible.
Expand All @@ -407,7 +417,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
url = rawUrl.slice(1, -1)
}
const deps: Set<string> = new Set()
const deps: Set<number> = new Set()
let hasRemovedPureCssChunk = false

if (url) {
Expand All @@ -420,9 +430,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
analyzed.add(filename)
const chunk = bundle[filename] as OutputChunk | undefined
if (chunk) {
deps.add(chunk.fileName)
deps.add(addFileDep(chunk.fileName))
chunk.viteMetadata.importedCss.forEach((file) => {
deps.add(file)
deps.add(addFileDep(file))
})
chunk.imports.forEach(addDeps)
} else {
Expand All @@ -432,7 +442,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
if (chunk) {
if (chunk.viteMetadata.importedCss.size) {
chunk.viteMetadata.importedCss.forEach((file) => {
deps.add(file)
deps.add(addFileDep(file))
})
hasRemovedPureCssChunk = true
}
Expand Down Expand Up @@ -466,15 +476,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
deps.size > 1 ||
// main chunk is removed
(hasRemovedPureCssChunk && deps.size > 0)
? `[${[...deps]
.map((d) =>
JSON.stringify(
relativePreloadUrls
? path.relative(path.dirname(file), d)
: d
)
)
.join(',')}]`
? `__viteFile([${[...deps].join(',')}])`
gajus marked this conversation as resolved.
Show resolved Hide resolved
: `[]`,
{ contentOnly: true }
)
Expand All @@ -483,6 +485,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
}
}

s.append(`\
function __viteFile(indexes) {
const deps = ${JSON.stringify(
gajus marked this conversation as resolved.
Show resolved Hide resolved
fileDeps.map((fileDep) =>
relativePreloadUrls ? path.relative(path.dirname(file), fileDep) : fileDep
)
)}
return indexes.map((i) => deps[i])
}`)

// there may still be markers due to inlined dynamic imports, remove
// all the markers regardless
let markerStartPos = code.indexOf(preloadMarkerWithQuote)
Expand Down