feat: Skip already compiled code in vite plugin #1642
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.
Problem
The Vite plugin currently processes all
.css.{ts,js,tsx,jsx}files based solely on file extension, without distinguishing between source files and already-compiled output.This causes issues in monorepo setups where:
Solution
Add a simple check in the
transformhook to detect already-compiled vanilla-extract files by looking for the signature pattern:importstatements containing.vanilla.cssKey pattern:
Why Not Other Approaches?
Path-Based Filtering
Q: Since the Vite plugin has access to A's file path during build, why not filter based on path?
A: Path-based filtering would block use-cases where someone intentionally want to transpile Package A's source code during Package B's build (e.g., in monorepo development mode with direct source imports).
Content-based detection only skips files that are already compiled, preserving flexibility for different monorepo workflows.
Renaming Output Files (as suggested in #1568)
Q: Why not use rollup's
assetFileNamesoption to rename compiled files (e.g.,.styles.mjsinstead of.css.mjs) to avoid the.css.{js,ts}pattern matching?A: While this workaround can avoid the immediate issue, it doesn't address the root cause: the plugin attempting to reprocess already-compiled files.
This PR aims to fix the fundamental problem rather than working around it.
The file renaming approach remains valid as a temporary workaround, but this fix ensures the plugin correctly handles all scenarios by default.