|
| 1 | +# @bosh-code/tsdown-plugin-inject-css |
| 2 | + |
| 3 | +[](https://npmjs.com/package/@bosh-code/tsdown-plugin-inject-css) |
| 4 | + |
| 5 | + |
| 6 | +Inject CSS imports at the top of each chunk file in tsdown builds. |
| 7 | + |
| 8 | +During the build process, tsdown strips CSS imports from your source files. This plugin tracks those imports and |
| 9 | +re-injects them into output chunks. |
| 10 | + |
| 11 | +```js |
| 12 | +// Input: foo.ts |
| 13 | +import './foo.css'; |
| 14 | + |
| 15 | +export const Foo = () => <div>Foo</div>; |
| 16 | + |
| 17 | +// Output: foo.js (with plugin) |
| 18 | +import './foo.css'; |
| 19 | + |
| 20 | +export const Foo = () => <div>Foo</div>; |
| 21 | +``` |
| 22 | + |
| 23 | +## Features |
| 24 | + |
| 25 | +- 💡 Automatically tracks CSS imports before they're stripped |
| 26 | +- 🎯 Injects imports into the correct output chunks |
| 27 | +- ⚡️ Sourcemap support |
| 28 | +- 🛠 Out-of-box, minimal configuration |
| 29 | +- 🔄 Works with tsdown's code splitting and chunking |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +```bash |
| 34 | +npm install @bosh-code/tsdown-plugin-inject-css -D |
| 35 | +# or |
| 36 | +pnpm add @bosh-code/tsdown-plugin-inject-css -D |
| 37 | +# or |
| 38 | +yarn add @bosh-code/tsdown-plugin-inject-css -D |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +```ts |
| 44 | +// tsdown.config.ts |
| 45 | +import { defineConfig } from 'tsdown'; |
| 46 | +import { libInjectCss } from '@bosh-code/tsdown-plugin-inject-css'; |
| 47 | + |
| 48 | +export default defineConfig({ |
| 49 | + entry: ['./src/index.ts'], |
| 50 | + format: ['esm'], |
| 51 | + plugins: [ |
| 52 | + libInjectCss({ |
| 53 | + sourcemap: true, // default: true |
| 54 | + }), |
| 55 | + ], |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +## How It Works |
| 60 | + |
| 61 | +The plugin operates in three phases: |
| 62 | + |
| 63 | +1. **Transform Phase**: Scans source files for CSS imports (e.g., `import './style.css'`) before they're stripped |
| 64 | +2. **Render Phase**: Tracks which source modules end up in which output chunks |
| 65 | +3. **Generate Phase**: Re-injects the CSS imports at the top of the appropriate chunks |
| 66 | + |
| 67 | +### Example |
| 68 | + |
| 69 | +Given this structure: |
| 70 | + |
| 71 | +```ts |
| 72 | +// src/foo.ts |
| 73 | +import './foo.css'; |
| 74 | + |
| 75 | +export const Foo = () => <div>Foo < /div>; |
| 76 | + |
| 77 | +// src/bar.ts |
| 78 | +import './bar.css'; |
| 79 | + |
| 80 | +export const Bar = () => <div>Bar < /div>; |
| 81 | + |
| 82 | +// src/index.ts |
| 83 | +export { Foo } from './foo'; |
| 84 | +export { Bar } from './bar'; |
| 85 | +``` |
| 86 | + |
| 87 | +The plugin ensures: |
| 88 | + |
| 89 | +- `foo.js` includes `import './foo.css';` |
| 90 | +- `bar.js` includes `import './bar.css';` |
| 91 | +- `index.js` imports from `foo.js` and `bar.js` (CSS already handled) |
| 92 | + |
| 93 | +## Options |
| 94 | + |
| 95 | +### `sourcemap` |
| 96 | + |
| 97 | +- Type: `boolean` |
| 98 | +- Default: `true` |
| 99 | + |
| 100 | +Whether to generate sourcemaps for the modified chunks. |
| 101 | + |
| 102 | +```ts |
| 103 | +libInjectCss({ |
| 104 | + sourcemap: false, // Disable sourcemap generation |
| 105 | +}) |
| 106 | +``` |
| 107 | + |
| 108 | +## Why This Plugin? |
| 109 | + |
| 110 | +When building component libraries with tsdown, CSS imports are typically stripped during the transpilation process. This |
| 111 | +plugin solves the problem by: |
| 112 | + |
| 113 | +1. **Preserving CSS imports**: Ensures styles are loaded when components are used |
| 114 | +2. **Proper chunking**: Each chunk only imports its required CSS files |
| 115 | +3. **Tree-shaking friendly**: Works seamlessly with tsdown's code splitting |
| 116 | +4. **Zero configuration**: Works out of the box with sensible defaults |
| 117 | + |
| 118 | +## Compatibility |
| 119 | + |
| 120 | +This plugin is designed for: |
| 121 | + |
| 122 | +- ✅ tsdown (primary target) |
| 123 | +- ✅ Rolldown (direct compatibility) |
| 124 | +- ⚠️ Rollup (may work with limitations) |
| 125 | + |
| 126 | +## Configuration Tips |
| 127 | + |
| 128 | +### For Component Libraries |
| 129 | + |
| 130 | +When building a component library, you typically want: |
| 131 | + |
| 132 | +```ts |
| 133 | +export default defineConfig({ |
| 134 | + entry: ['./src/index.ts'], |
| 135 | + format: ['esm'], |
| 136 | + dts: true, |
| 137 | + sourcemap: true, |
| 138 | + plugins: [ |
| 139 | + libInjectCss(), |
| 140 | + ], |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +### With Multiple Entry Points |
| 145 | + |
| 146 | +The plugin works seamlessly with multiple entries: |
| 147 | + |
| 148 | +```ts |
| 149 | +export default defineConfig({ |
| 150 | + entry: { |
| 151 | + index: './src/index.ts', |
| 152 | + button: './src/button/index.ts', |
| 153 | + card: './src/card/index.ts', |
| 154 | + }, |
| 155 | + format: ['esm'], |
| 156 | + plugins: [ |
| 157 | + libInjectCss(), |
| 158 | + ], |
| 159 | +}); |
| 160 | +``` |
| 161 | + |
| 162 | +Each entry point will have its CSS imports properly injected. |
| 163 | + |
| 164 | +## Inspired By |
| 165 | + |
| 166 | +This plugin is inspired by [vite-plugin-lib-inject-css](https://github.com/emosheeep/vite-plugin-lib-inject-css) but |
| 167 | +adapted specifically for tsdown's architecture and build process. |
| 168 | + |
| 169 | +## License |
| 170 | + |
| 171 | +MIT © bosh-code |
0 commit comments