A Vite plugin that injects all CSS into the JavaScript bundle at build time, eliminating separate CSS files and enabling single-file deployments. Current stable version is 5.0.1, requiring Vite >8.0.0-0 (major breaking from v4 which supported Vite 4-7). It provides a virtual module for on-demand injection and supports custom injection targets like Shadow DOM. Key differentiator: unlike Vite's default CSS extraction, it bundles styles into JS, useful for widgets, libraries, or deployments where a single JS file is preferred. Ships TypeScript types. Active development with frequent releases.
npm install vite-plugin-css-injected-by-jsVerified import paths — ran on the pinned version, not inferred.
Basic setup: install plugin, import default export, add to Vite config, and build to get a single JS file with CSS injected.
Update Vite to version 8+ or use vite-plugin-css-injected-by-js@^4
Ensure Node.js >=16 and review any rolldown-specific configurations
If you rely on topExecutionPriority, avoid using the virtual module.
Ensure custom injection is synchronous, targets the same element, and uses DOM nodes, not CSSStyleSheet.
Remove uniqueCSS option from plugin configuration.
Use `import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'` (no destructuring).
Ensure `cssInjectedByJsPlugin()` is in the plugins list in vite.config.ts.
Check the documentation for v5 options; topExecutionPriority is still valid but verify usage.
Install optional dependencies with `npm install` or use `pnpm install --shamefully-hoist`.