Skip to content

vite-plugin: Add extract option - #1812

Closed
aicest wants to merge 1 commit into
vanilla-extract-css:masterfrom
aicest:vite-plugin-extract
Closed

aicest wants to merge 1 commit into
vanilla-extract-css:masterfrom
aicest:vite-plugin-extract

Conversation

@aicest

@aicest aicest commented Nov 18, 2026 •

Copy link
Copy Markdown

Adds an opt-in extract option to @vanilla-extract/vite-plugin, bringing it in line with the Rollup plugin:

  • extract: false emits each .css.ts file as its own CSS asset (e.g. assets/button.css.ts.vanilla.css) and rewrites side-effect imports to point at the emitted files, deduped across chunks
  • extract: { name, sourcemap } combines all generated CSS into a single plugin-emitted asset (default name bundle.css) and strips side-effect CSS imports from the JS output
  • left unset, nothing changes — CSS continues through Vite's native CSS pipeline

Implementation notes:

  • The bundling machinery is ported from packages/rollup-plugin/src/lib.ts into a self-contained packages/vite-plugin/src/lib.ts, so the Vite plugin doesn't need to depend on the Rollup plugin.
  • The regex that strips side-effect imports is minify-safe (lookbehind-based rather than line-anchored), as Vite's default minified output is single-line.
  • The option only applies during vite build — no effect in dev, and no effect when unstable_mode: 'transform' is set.
  • Compatible with both rollup-backed Vite (5–7) and rolldown-backed Vite 8; tested against Vite 8.

Testing: added tests/vite-plugin/extract.test.ts covering per-file emission (single and multiple entries, including dedupe), bundle mode with import stripping, minified output and sourcemaps. All tests/vite-plugin tests pass, pnpm build and lint are clean. Includes a changeset.

Site docs are intentionally untouched — happy to add an extract section to the Vite integration docs if useful.

Mirror the Rollup plugin's `extract` option in the Vite plugin:

- `extract: false`: emit per-file CSS assets and rewrite side-effect
  imports to the emitted files
- `extract: { name, sourcemap }`: combine all CSS into a single
  plugin-emitted asset and strip side-effect CSS imports from JS output
- `undefined` (default): unchanged, Vite's native CSS pipeline
@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: eaf5675

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@vanilla-extract/vite-plugin Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@aicest

aicest commented Nov 18, 2026

Copy link
Copy Markdown
Author

Closing this. After further end-to-end testing, the documented approach — using @vanilla-extract/rollup-plugin directly in a Vite lib-mode config (extract: false) — covers this scenario (verified: coherent build output, consumer-side CSS flow, minify-safe import rewrites on Vite 8). A separate extract option on the Vite plugin doesn't add enough value to justify duplicating the mechanism. Thanks for the reviews!

@aicest aicest closed this Nov 18, 2026
@aicest
aicest deleted the vite-plugin-extract branch November 18, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant