|
| 1 | +# Headwind Bun Plugin |
| 2 | + |
| 3 | +A Bun plugin that automatically generates and injects Headwind CSS into your HTML files during the build process. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +bun add @stacksjs/headwind |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +**1. Create your HTML file** (`src/template.html`): |
| 14 | +```html |
| 15 | +<!DOCTYPE html> |
| 16 | +<html> |
| 17 | +<head> |
| 18 | + <title>My App</title> |
| 19 | +</head> |
| 20 | +<body> |
| 21 | + <div class="flex items-center p-4 bg-blue-500 text-white rounded-lg"> |
| 22 | + <h1 class="text-2xl font-bold">Hello Headwind!</h1> |
| 23 | + </div> |
| 24 | +</body> |
| 25 | +</html> |
| 26 | +``` |
| 27 | + |
| 28 | +**2. Import it in your TypeScript** (`src/index.ts`): |
| 29 | +```typescript |
| 30 | +import template from './template.html' |
| 31 | + |
| 32 | +// The HTML now has Headwind CSS injected |
| 33 | +document.body.innerHTML = template |
| 34 | +``` |
| 35 | + |
| 36 | +**3. Build with the plugin**: |
| 37 | +```typescript |
| 38 | +import { plugin } from '@stacksjs/headwind' |
| 39 | + |
| 40 | +await Bun.build({ |
| 41 | + entrypoints: ['./src/index.ts'], |
| 42 | + outdir: './dist', |
| 43 | + plugins: [plugin()], |
| 44 | +}) |
| 45 | +``` |
| 46 | + |
| 47 | +The plugin will automatically: |
| 48 | +- Scan the HTML for utility classes |
| 49 | +- Generate CSS for those classes |
| 50 | +- Inject the CSS into the `<head>` section |
| 51 | + |
| 52 | +## Configuration |
| 53 | + |
| 54 | +### Basic Configuration |
| 55 | + |
| 56 | +```typescript |
| 57 | +import { plugin } from '@stacksjs/headwind' |
| 58 | + |
| 59 | +await Bun.build({ |
| 60 | + entrypoints: ['./src/index.ts'], |
| 61 | + outdir: './dist', |
| 62 | + plugins: [ |
| 63 | + plugin({ |
| 64 | + includePreflight: true, // Include CSS reset (default: true) |
| 65 | + }), |
| 66 | + ], |
| 67 | +}) |
| 68 | +``` |
| 69 | + |
| 70 | +### Custom Theme |
| 71 | + |
| 72 | +```typescript |
| 73 | +import { plugin } from '@stacksjs/headwind' |
| 74 | + |
| 75 | +await Bun.build({ |
| 76 | + entrypoints: ['./src/index.ts'], |
| 77 | + outdir: './dist', |
| 78 | + plugins: [ |
| 79 | + plugin({ |
| 80 | + config: { |
| 81 | + minify: true, |
| 82 | + theme: { |
| 83 | + colors: { |
| 84 | + primary: '#3b82f6', |
| 85 | + secondary: '#10b981', |
| 86 | + danger: '#ef4444', |
| 87 | + }, |
| 88 | + spacing: { |
| 89 | + 18: '4.5rem', |
| 90 | + 88: '22rem', |
| 91 | + }, |
| 92 | + }, |
| 93 | + shortcuts: { |
| 94 | + btn: 'px-4 py-2 rounded bg-primary text-white hover:bg-blue-600', |
| 95 | + card: 'p-6 bg-white rounded-lg shadow-md', |
| 96 | + }, |
| 97 | + }, |
| 98 | + }), |
| 99 | + ], |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +### Advanced Configuration |
| 104 | + |
| 105 | +```typescript |
| 106 | +import { plugin } from '@stacksjs/headwind' |
| 107 | + |
| 108 | +await Bun.build({ |
| 109 | + entrypoints: ['./src/index.ts'], |
| 110 | + outdir: './dist', |
| 111 | + plugins: [ |
| 112 | + plugin({ |
| 113 | + config: { |
| 114 | + minify: true, |
| 115 | + safelist: ['bg-red-500', 'text-green-500'], // Always include these |
| 116 | + blocklist: ['debug-*'], // Never include these |
| 117 | + theme: { |
| 118 | + colors: { |
| 119 | + brand: { |
| 120 | + 50: '#f0f9ff', |
| 121 | + 100: '#e0f2fe', |
| 122 | + 500: '#0ea5e9', |
| 123 | + 900: '#0c4a6e', |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + shortcuts: { |
| 128 | + 'btn-primary': 'px-4 py-2 rounded bg-brand-500 text-white hover:bg-brand-600', |
| 129 | + }, |
| 130 | + }, |
| 131 | + includePreflight: true, |
| 132 | + }), |
| 133 | + ], |
| 134 | +}) |
| 135 | +``` |
| 136 | + |
| 137 | +## API Reference |
| 138 | + |
| 139 | +### `plugin(options?)` |
| 140 | + |
| 141 | +Creates a Headwind Bun plugin instance. |
| 142 | + |
| 143 | +#### Options |
| 144 | + |
| 145 | +- **`config`** (`Partial<HeadwindConfig>`) - Custom Headwind configuration |
| 146 | + - `minify` - Minify the generated CSS |
| 147 | + - `theme` - Custom theme (colors, spacing, fonts, etc.) |
| 148 | + - `shortcuts` - Utility class shortcuts |
| 149 | + - `safelist` - Classes to always include |
| 150 | + - `blocklist` - Classes to never include |
| 151 | + - `variants` - Enable/disable variants |
| 152 | + - And more... |
| 153 | + |
| 154 | +- **`includePreflight`** (`boolean`) - Include preflight CSS (default: `true`) |
| 155 | + |
| 156 | +## How It Works |
| 157 | + |
| 158 | +1. The plugin registers an `onLoad` handler for `.html` files |
| 159 | +2. When Bun encounters an HTML import, the plugin intercepts it |
| 160 | +3. It extracts all utility classes from the HTML using Headwind's parser |
| 161 | +4. It generates CSS for those classes using Headwind's generator |
| 162 | +5. The CSS is injected into the `<head>` section |
| 163 | +6. The processed HTML is returned to the bundle |
| 164 | + |
| 165 | +## Use Cases |
| 166 | + |
| 167 | +- **SPAs**: Import HTML templates with automatic CSS generation |
| 168 | +- **Web Components**: Load component templates with scoped styles |
| 169 | +- **Static Sites**: Process HTML pages during build |
| 170 | +- **Email Templates**: Generate inline CSS for email HTML |
| 171 | + |
| 172 | +## Examples |
| 173 | + |
| 174 | +See the [examples/plugin](./examples/plugin) directory for a complete working example. |
| 175 | + |
| 176 | +## Comparison with CLI |
| 177 | + |
| 178 | +| Feature | Plugin | CLI | |
| 179 | +|---------|--------|-----| |
| 180 | +| Automatic CSS generation | ✅ | ✅ | |
| 181 | +| Watches files | ✅ (via Bun) | ✅ | |
| 182 | +| Injects CSS | ✅ | ❌ | |
| 183 | +| Separate CSS file | ❌ | ✅ | |
| 184 | +| Build integration | ✅ | ❌ | |
| 185 | +| Standalone usage | ❌ | ✅ | |
| 186 | + |
| 187 | +## Performance |
| 188 | + |
| 189 | +The plugin is highly performant: |
| 190 | +- Processes 1000 utilities in ~7ms |
| 191 | +- Minimal overhead in build process |
| 192 | +- Lazy loading - only processes imported HTML files |
| 193 | + |
| 194 | +## TypeScript Support |
| 195 | + |
| 196 | +The plugin is fully typed. Import the types: |
| 197 | + |
| 198 | +```typescript |
| 199 | +import type { HeadwindPluginOptions } from '@stacksjs/headwind' |
| 200 | + |
| 201 | +const options: HeadwindPluginOptions = { |
| 202 | + config: { |
| 203 | + minify: true, |
| 204 | + }, |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +## Configuration File |
| 209 | + |
| 210 | +The plugin also respects `headwind.config.ts` in your project root: |
| 211 | + |
| 212 | +```typescript |
| 213 | +// headwind.config.ts |
| 214 | +import type { HeadwindOptions } from '@stacksjs/headwind' |
| 215 | + |
| 216 | +export default { |
| 217 | + minify: true, |
| 218 | + theme: { |
| 219 | + colors: { |
| 220 | + primary: '#3b82f6', |
| 221 | + }, |
| 222 | + }, |
| 223 | +} satisfies HeadwindOptions |
| 224 | +``` |
| 225 | + |
| 226 | +The plugin will merge this config with any options passed to it. |
| 227 | + |
| 228 | +## License |
| 229 | + |
| 230 | +MIT |
0 commit comments