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 all commits
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
73 changes: 48 additions & 25 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 analyzedImportedCssFiles = new Map<OutputChunk, string[]>()
const inlineEntryChunk = new Set<string>()
const getImportedChunks = (
chunk: OutputChunk,
Expand Down Expand Up @@ -777,39 +777,62 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
},
})

const getCssTagsForChunk = (
chunk: OutputChunk,
const toStyleSheetLinkTag = (
file: string,
toOutputPath: (filename: string) => string,
seen: Set<string> = new Set(),
): HtmlTagDescriptor[] => {
const tags: HtmlTagDescriptor[] = []
if (!analyzedChunk.has(chunk)) {
analyzedChunk.set(chunk, 1)
chunk.imports.forEach((file) => {
const importee = bundle[file]
if (importee?.type === 'chunk') {
tags.push(...getCssTagsForChunk(importee, toOutputPath, seen))
}
})
): HtmlTagDescriptor => ({
tag: 'link',
attrs: {
rel: 'stylesheet',
crossorigin: true,
href: toOutputPath(file),
},
})

const getCssFilesForChunk = (
chunk: OutputChunk,
seenChunks: Set<string> = new Set(),
seenCss: Set<string> = new Set(),
): string[] => {
if (seenChunks.has(chunk.fileName)) {
return []
}
seenChunks.add(chunk.fileName)

if (analyzedImportedCssFiles.has(chunk)) {
const files = analyzedImportedCssFiles.get(chunk)!
const additionals = files.filter((file) => !seenCss.has(file))
additionals.forEach((file) => seenCss.add(file))
return additionals
}

const files: string[] = []
chunk.imports.forEach((file) => {
const importee = bundle[file]
if (importee?.type === 'chunk') {
files.push(...getCssFilesForChunk(importee, seenChunks, seenCss))
}
})
analyzedImportedCssFiles.set(chunk, files)

chunk.viteMetadata!.importedCss.forEach((file) => {
if (!seen.has(file)) {
seen.add(file)
tags.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
crossorigin: true,
href: toOutputPath(file),
},
})
if (!seenCss.has(file)) {
seenCss.add(file)
files.push(file)
}
})

return tags
return files
}
thy486 marked this conversation as resolved.
Show resolved Hide resolved

const getCssTagsForChunk = (
chunk: OutputChunk,
toOutputPath: (filename: string) => string,
) =>
getCssFilesForChunk(chunk).map((file) =>
toStyleSheetLinkTag(file, toOutputPath),
)

for (const [normalizedId, html] of processedHtml(this)) {
const relativeUrlPath = normalizePath(
path.relative(config.root, normalizedId),
Expand Down
Loading