Skip to content
Merged
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>,
Comment thread
sapphi-red marked this conversation as resolved.
Outdated
circle: Set<OutputChunk> = new Set(),
): HtmlTagDescriptor[] => {
const tags: HtmlTagDescriptor[] = []
if (circle.has(chunk)) {
return tags
Comment thread
thy486 marked this conversation as resolved.
Outdated
}
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
}
Comment thread
thy486 marked this conversation as resolved.

Expand Down