Skip to content

Conversation

@thy486
Copy link
Contributor

@thy486 thy486 commented Dec 16, 2025

Previously, CSS file cache was built based on seenCss state at that time. When multiple HTML entries process the same chunk, the cached CSS files may exclude files that were already in seenCss during first processing, causing those files to be missing in subsequent entries.

This fix introduces dirty chunk tracking to validate and rebuild cache when CSS files are detected as missing.

e.g.

  const bundle4: MockBundle = {
    'entry.js': {
      fileName: 'entry.js',
      type: 'chunk',
      imports: ['chunk1.js', 'chunk2.js'],
      viteMetadata: { importedCss: ['entry.css'] },
    },
    'entry2.js': {
      fileName: 'entry2.js',
      type: 'chunk',
      imports: ['chunk2.js'],
      viteMetadata: { importedCss: ['entry2.css'] },
    },
    'chunk1.js': {
      fileName: 'chunk1.js',
      type: 'chunk',
      imports: [],
      viteMetadata: { importedCss: ['chunk1.css', 'chunk-shared.css'] },
    },
    'chunk2.js': {
      fileName: 'chunk2.js',
      type: 'chunk',
      imports: [],
      viteMetadata: { importedCss: ['chunk2.css', 'chunk-shared.css'] },
    },
  }

  const oldGetCss4 = createGetCssFilesForChunkOld(bundle4)
  oldGetCss4(bundle4['entry.js']) // Process the first entry and set up the cache.
  // ✅ Output: chunk-shared.css, chunk1.css, chunk2.css, entry.css
  const oldResult4 = oldGetCss4(bundle4['entry2.js'])
  // ❌ Output: chunk2.css, entry2.css
  const newGetCss4 = createGetCssFilesForChunkNew(bundle4)
  newGetCss4(bundle4['entry.js'])
  // ✅ Output: chunk-shared.css, chunk1.css, chunk2.css, entry.css
  const newResult4 = newGetCss4(bundle4['entry2.js'])
  // ✅ Output: chunk-shared.css, chunk2.css, entry2.css

Tips: This code is not ready for immediate use. I'm just presenting my modification proposal first.

@thy486 thy486 changed the title fix(html): fix CSS missing when cache was built with different seenCs… fix(html): fix CSS missing when building with different seenCss context Dec 16, 2025
@sapphi-red
Copy link
Member

Would you extract this part of code and write unit tests? It would be great if you can do the extraction and add unit tests for #19143 in a separate PR first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants