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

fix(html): fix css disorder when building multiple entry html #19143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
},

async generateBundle(options, bundle) {
const analyzedChunk = new Map<OutputChunk, number>()
const analyzedChunk = new Map<OutputChunk, Set<string>>()
const inlineEntryChunk = new Set<string>()
const getImportedChunks = (
chunk: OutputChunk,
Expand Down Expand Up @@ -781,32 +781,61 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
chunk: OutputChunk,
toOutputPath: (filename: string) => string,
seen: Set<string> = new Set(),
parentImports?: Set<string>,
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
circle: Set<OutputChunk> = new Set(),
): HtmlTagDescriptor[] => {
const tags: HtmlTagDescriptor[] = []
if (circle.has(chunk)) {
return tags
thy486 marked this conversation as resolved.
Show resolved Hide resolved
}
circle.add(chunk)
let analyzedChunkImportCss: Set<string>
const processImportedCss = (files: Set<string>): void => {
files.forEach((file) => {
if (parentImports) {
parentImports.add(file)
}
if (!seen.has(file)) {
seen.add(file)
tags.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
crossorigin: true,
href: toOutputPath(file),
},
})
}
})
}
if (!analyzedChunk.has(chunk)) {
analyzedChunk.set(chunk, 1)
analyzedChunkImportCss = new Set()
chunk.imports.forEach((file) => {
const importee = bundle[file]
if (importee?.type === 'chunk') {
tags.push(...getCssTagsForChunk(importee, toOutputPath, seen))
tags.push(
...getCssTagsForChunk(
importee,
toOutputPath,
seen,
analyzedChunkImportCss,
circle,
),
)
}
})
}

chunk.viteMetadata!.importedCss.forEach((file) => {
if (!seen.has(file)) {
seen.add(file)
tags.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
crossorigin: true,
href: toOutputPath(file),
},
analyzedChunk.set(chunk, analyzedChunkImportCss)
if (parentImports) {
analyzedChunkImportCss.forEach((file) => {
parentImports.add(file)
})
}
})
} else {
analyzedChunkImportCss = analyzedChunk.get(chunk)!
processImportedCss(analyzedChunkImportCss)
}

processImportedCss(chunk.viteMetadata!.importedCss)
return tags
}
thy486 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading