fix(css): handle dynamic CSS manual chunks#22553
Open
Xavrir wants to merge 1 commit into
Open
Conversation
a93f8a9 to
af35486
Compare
Author
|
Updated the regression assertion after an external review pointed out that checking for The test now asserts that the forced CSS-only manual chunk does not emit a Restoring the production fix makes the targeted build/serve/unit checks pass again. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is this PR solving?
Fixes #22244.
A dynamic import of a plain CSS file can be forced into a manual chunk. With Rolldown, that CSS-only chunk may get synthetic facade exports. Vite was using
chunk.exports.length === 0to decide whether a chunk was pure CSS, so this case stayed in the JS bundle and could emit code that referenced an undeclared binding such aslib_exports.This PR classifies pure CSS chunks from their rendered module content instead. CSS modules and chunks with real JS still stay out of the pure CSS removal path.
Alternatives considered
I considered handling this in the import-analysis replacement path, but the bad chunk is created before that step. Keeping the fix in CSS chunk classification also preserves the existing flow where Vite removes pure CSS JS placeholders, records them in
removedPureCssFilesCache, and lets build import analysis replace the dynamic import withPromise.resolve({})while preserving CSS preload deps.Tests
Added a
css-dynamic-importplayground case that dynamically importsmanual.cssin production and forces it into a manual chunk. The test checks that the CSS loads and that the built JS does not contain the bad generated export.Manual inspection of the playground build shows the CSS file is emitted, the dynamic import is rewritten to
Promise.resolve({}), and the CSS file remains in the preload dependency list.Checks run locally:
PATH=/tmp/codex-pnpm-bin:$PATH pnpm run buildPATH=/tmp/codex-pnpm-bin:$PATH pnpm run test-build css-dynamic-importPATH=/tmp/codex-pnpm-bin:$PATH pnpm run test-serve css-dynamic-importPATH=/tmp/codex-pnpm-bin:$PATH pnpm run test-unit cssPATH=/tmp/codex-pnpm-bin:$PATH pnpm lintPATH=/tmp/codex-pnpm-bin:$PATH pnpm typecheckPATH=/tmp/codex-pnpm-bin:$PATH pnpm exec oxfmt --check packages/vite/src/node/plugins/css.ts playground/css-dynamic-import/__tests__/css-dynamic-import.spec.ts playground/css-dynamic-import/index.jsLocal note:
pnpmwas not on PATH in my shell, so I ran these through a temporary Corepack wrapper.Reviewer notes
The main behavior change is that pure CSS chunk detection no longer treats synthetic exports as proof that a chunk contains JS. The existing per-module checks still reject CSS modules and real JS chunks.