A Vite plugin (v0.2.0) that enables true on-demand CSS injection for dynamically imported code by virtualizing CSS Modules. It prevents lazy components' styles from being bundled into the page's initial CSS, injecting them only when the corresponding JavaScript chunk is actually loaded. Supports Vite 4+, TypeScript, and frameworks like React, Vue, Svelte (via external *.module.css imports). Unlike typical CSS-in-JS solutions or code-splitting plugins, it preserves CSS Modules hashing, avoids preloading CSS via __vitePreload, and includes built-in RTL support via runtimeIsRtlCondition. Released as a proof-of-concept with active maintenance.
npm install vite-plugin-lazy-css-modules-inlinerVerified import paths — ran on the pinned version, not inferred.
Shows basic setup with vite.config.ts plugin initialization and a lazy-loaded React component using CSS Modules. The plugin intercepts dynamic imports to inline CSS only when the component is actually rendered.
Update Node.js to 18+ and Vite to 4+. Check engines field in package.json.
Use 'css' (default) unless you fully understand the implications. 'all' strips all dependencies from __vitePreload which can break chunk loading.
Use only dynamic imports for CSS modules that are lazy-loaded, or accept the duplication. Do not mix import styles for the same module.
Ensure you are using Vite (not a custom bundler). Virtual modules are Vite-specific and won't work with plain Rollup.
Run `npm install --save-dev rtlcss` and set runtimeIsRtlCondition in plugin options.
Ensure you have a ClientRouter or wait for hydration. Consider using a CSS-in-JS solution for SSR if flashes are unacceptable.
Install with `npm install --save-dev vite-plugin-lazy-css-modules-inliner` and verify package.json includes it.
Change `import viteLazyCssInliner from ...` to `import { viteLazyCssInliner } from ...`.Update Vite to version 4 or higher: `npm install vite@^4`.
Upgrade Vite to >=4, as the plugin relies on dynamicImporters property added in Vite 4.
Set stripPreloadDepsMode to 'css' (default) unless you have a specific reason to use 'all'.