中文 | English
A tsdown plugin that automatically injects CSS imports into JS chunks.
Automatically injects CSS import statements at the top of each JS chunk during library bundling, ensuring CSS is loaded alongside JS.
import { defineConfig } from "vite-plus";
import { cssInject } from "tsdown-plugin-css-inject";
export default defineConfig({
pack: {
plugins: [cssInject()],
css: { splitting: true },
},
});- Track CSS Imports - Intercept all CSS file imports in the
resolveIdhook - Record Module Associations - Store CSS files imported by each module
- Inject Imports - In the
renderChunkhook, inject CSS imports for chunks with CSS dependencies
- CSS files share the same name as their corresponding chunk (standard behavior)
- Example:
index.jscorresponds toindex.css,demo2-abc123.jscorresponds todemo2-abc123.css
- ES Module:
import './style.css'; - CommonJS:
require('./style.css');
- ✅ tsdown (vite-plus)
Inspired by vite-plugin-lib-inject-css