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 1 commit into
base: main
Choose a base branch
from

Conversation

thy486
Copy link

@thy486 thy486 commented Jan 6, 2025

Description

When Vite collects css tags, it will cache the analyzed css chunk. But when collecting css tags for the second time, since the css chunk has been cached, Vite will no longer analyze the deep import, which will ignore the results of the previous import, causing the css to be out of order.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you create a reproduction so that I can understand when this happens?

packages/vite/src/node/plugins/html.ts Show resolved Hide resolved
@sapphi-red sapphi-red added feat: html p3-minor-bug An edge case that only affects very specific usage (priority) labels Jan 7, 2025
@thy486
Copy link
Author

thy486 commented Jan 7, 2025

Would you create a reproduction so that I can understand when this happens?

Reproduction

https://stackblitz.com/edit/vitejs-vite-f9kv8tr4

The meaning of parentImports is: It is passed in by the parent importer and is used to collect the css used by imported modules and itself

): HtmlTagDescriptor[] => {
const tags: HtmlTagDescriptor[] = []
if (circle.has(chunk)) {
return tags
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there may be a problem here. If it is the previous logic, when encountering a module introduced in a loop, the css tag of the module will be generated first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is fine. Actually I think it's more a bug fix as it will align with this code:

const addDeps = (filename: string) => {
if (filename === ownerFilename) return
if (analyzed.has(filename)) return
analyzed.add(filename)
const chunk = bundle[filename]
if (chunk) {
deps.add(chunk.fileName)
if (chunk.type === 'chunk') {
chunk.imports.forEach(addDeps)
// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.add(file)
})
}
} else {
const removedPureCssFiles =
removedPureCssFilesCache.get(config)!
const chunk = removedPureCssFiles.get(filename)
if (chunk) {
if (chunk.viteMetadata!.importedCss.size) {
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.add(file)
})
hasRemovedPureCssChunk = true
}
s.update(expStart, expEnd, 'Promise.resolve({})')
}
}
}

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reproduction.


processImportedCss(chunk.viteMetadata!.importedCss)
return tags
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think splitting the function into 3 like this will make the function more easier to understand

const toStyleSheetLinkTag = (
  file: string,
  toOutputPath: (filename: string) => string,
): 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 (analyzedChunk.has(chunk)) {
    return analyzedChunk.get(chunk)!
  }

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

  chunk.viteMetadata!.importedCss.forEach((file) => {
    if (!seenCss.has(file)) {
      seenCss.add(file)
      files.push(file)
    }
  })

  return files
}

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

I think analyzedChunk should be renamed to analyzedImportedCssFiles in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: html p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants