|
| 1 | +# vite-plugin-beasties |
| 2 | + |
| 3 | +A Vite plugin that uses [beasties](https://github.com/danielroe/beasties) to embed critical CSS in your HTML pages. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🚀 Automatically identifies and inlines critical CSS |
| 8 | +- 🧹 Supports pruning the CSS files to remove inlined styles from external stylesheets |
| 9 | +- 🔄 Works with Vite's build process using the `transformIndexHtml` hook |
| 10 | +- ⚙️ Full access to beasties configuration options |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +# npm |
| 16 | +npm install -D vite-plugin-beasties |
| 17 | + |
| 18 | +# yarn |
| 19 | +yarn add -D vite-plugin-beasties |
| 20 | + |
| 21 | +# pnpm |
| 22 | +pnpm add -D vite-plugin-beasties |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +Add the plugin to your `vite.config.js/ts`: |
| 28 | + |
| 29 | +```js |
| 30 | +// vite.config.js |
| 31 | +import { defineConfig } from 'vite' |
| 32 | +import { beasties } from 'vite-plugin-beasties' |
| 33 | + |
| 34 | +export default defineConfig({ |
| 35 | + plugins: [ |
| 36 | + beasties({ |
| 37 | + // Plugin options |
| 38 | + options: { |
| 39 | + // Beasties library options |
| 40 | + preload: 'swap', |
| 41 | + pruneSource: true, // Enable pruning CSS files |
| 42 | + inlineThreshold: 4000, // Inline stylesheets smaller than 4kb |
| 43 | + }, |
| 44 | + // Filter to apply beasties only to specific HTML files |
| 45 | + filter: path => path.endsWith('.html'), |
| 46 | + }), |
| 47 | + ], |
| 48 | +}) |
| 49 | +``` |
| 50 | + |
| 51 | +## Options |
| 52 | + |
| 53 | +### Plugin Options |
| 54 | + |
| 55 | +| Option | Type | Default | Description | |
| 56 | +|--------|------|---------|-------------| |
| 57 | +| `options` | `Object` | `{}` | Options passed to the beasties constructor | |
| 58 | +| `filter` | `Function` | `(path) => path.endsWith('.html')` | Filter function to determine which HTML files to process | |
| 59 | + |
| 60 | +### Beasties Options |
| 61 | + |
| 62 | +See the [beasties documentation](https://github.com/danielroe/beasties) for all available options. |
| 63 | + |
| 64 | +Common options include: |
| 65 | + |
| 66 | +- `preload`: Strategy for loading non-critical CSS (`'js'`, `'js-lazy'`, `'media'`, `'swap'`, `'swap-high'`, `'swap-low'`, `false`) |
| 67 | +- `pruneSource`: Whether to update external CSS files to remove inlined styles |
| 68 | +- `inlineThreshold`: Size limit in bytes to inline an entire stylesheet |
| 69 | +- `minimumExternalSize`: If the non-critical part of a CSS file is smaller than this, the entire file will be inlined |
| 70 | +- `additionalStylesheets`: Additional stylesheets to consider for critical CSS |
| 71 | + |
| 72 | +## 💻 Development |
| 73 | + |
| 74 | +- Clone this repository |
| 75 | +- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` |
| 76 | +- Install dependencies using `pnpm install` |
| 77 | +- Run interactive tests using `pnpm dev` |
| 78 | + |
| 79 | +## License |
| 80 | + |
| 81 | +MIT |
| 82 | + |
| 83 | +Published under [MIT License](./LICENCE). |
| 84 | + |
| 85 | +<!-- Badges --> |
| 86 | + |
| 87 | +[npm-version-src]: https://img.shields.io/npm/v/vite-plugin-beasties?style=flat-square |
| 88 | +[npm-version-href]: https://npmjs.com/package/vite-plugin-beasties |
| 89 | +[npm-downloads-src]: https://img.shields.io/npm/dm/vite-plugin-beasties?style=flat-square |
| 90 | +[npm-downloads-href]: https://npm.chart.dev/vite-plugin-beasties |
| 91 | +[github-actions-src]: https://img.shields.io/github/actions/workflow/status/danielroe/vite-plugin-beasties/ci.yml?branch=main&style=flat-square |
| 92 | +[github-actions-href]: https://github.com/danielroe/vite-plugin-beasties/actions?query=workflow%3Aci |
| 93 | +[codecov-src]: https://img.shields.io/codecov/c/gh/danielroe/vite-plugin-beasties/main?style=flat-square |
| 94 | +[codecov-href]: https://codecov.io/gh/danielroe/vite-plugin-beasties |
0 commit comments