# @unocss/vite The Vite plugin for UnoCSS. Ships with the `unocss` package. ## Installation ```bash npm i -D unocss ``` ```ts // vite.config.ts import Unocss from 'unocss/vite' export default { plugins: [ Unocss({ /* options */ }), ], } ``` Add `uno.css` to your main entry: ```ts // main.ts import 'uno.css' ``` ### Presetless usage > This plugin does not come with any default presets. > If you are building a meta framework on top of UnoCSS, see [this file](https://github.com/unocss/unocss/blob/main/packages/unocss/src/vite.ts) for an example to bind the default presets. ```bash npm i -D @unocss/vite ``` ```ts // vite.config.ts import Unocss from '@unocss/vite' export default { plugins: [ Unocss({ presets: [ /* no presets by default */ ], /* options */ }), ], } ``` ## Modes The Vite plugin comes with a set of modes that enable different behaviors. ###### `global` (default) This is the default mode for the plugin: in this mode you need to add the import of `uno.css` on your entry point. This mode enables a set of Vite plugins for `build` and for `dev` with `HMR` support. The generated `css` will be a global stylesheet injected on the `index.html`. ###### `vue-scoped` This mode will inject generated CSS to Vue SFC's ` ``` Alternatively, if you only want them to apply to a specific component just add them to that component's `style` tag and don't add the `global` attribute. ```ts // vite.config.js import { sveltekit } from '@sveltejs/kit/vite' import UnoCSS from 'unocss/vite' /** @type {import('vite').UserConfig} */ const config = { plugins: [ UnoCSS({ mode: 'svelte-scoped', /* options */ }), sveltekit(), ], } ``` There is a `SvelteKit scoped` example project in the [examples/sveltekit-scoped](https://github.com/unocss/unocss/tree/main/examples/sveltekit-scoped#readme) directory with more detailed explanation of how this mode works. ### Web Components To work with web components you need to enable `shadow-dom` mode on the plugin. Don't forget to remove the import for `uno.css` since the `shadow-dom` mode will not expose it and the application will not work. ```ts // vite.config.js import Unocss from 'unocss/vite' export default { plugins: [ Unocss({ mode: 'shadow-dom', /* more options */ }), ], } ``` On each `web component` just add `@unocss-placeholder` to its style css block: ```ts const template = document.createElement('template') template.innerHTML = `
...
` ``` If you're using [Lit](https://lit.dev/): ```ts @customElement('my-element') export class MyElement extends LitElement { static styles = css` :host {...} @unocss-placeholder ` // ... } ``` You have a `Web Components` example project on [examples/vite-lit](https://github.com/unocss/unocss/tree/main/examples/vite-lit) directory. #### `::part` built-in support You can use `::part` since the plugin supports it via `shortcuts` and using `part-[]:` rule from `preset-mini`, for example using it with simple rules like `part-[]:bg-green-500` or using some `shortcut`: check `src/my-element.ts` on linked example project below. The `part-[]:` will work only with this plugin using the `shadow-dom` mode. The plugin uses `nth-of-type` to avoid collisions with multiple parts in the same web component and for the same parts on distinct web components, you don't need to worry about it, the plugin will take care for you. ```ts // vite.config.js import Unocss from 'unocss/vite' export default { plugins: [ Unocss({ mode: 'shadow-dom', shortcuts: [ { 'cool-blue': 'bg-blue-500 text-white' }, { 'cool-green': 'bg-green-500 text-black' }, ], /* more options */ }), ], } ``` then in your web components: ```ts // my-container-wc.ts const template = document.createElement('template') template.innerHTML = ` ... ` ``` ```ts // my-wc-with-parts.ts const template = document.createElement('template') template.innerHTML = `
...
...
` ``` ### Solid **WARNING**: You should import the `uno.css` virtual module using `import 'virtual:uno.css'` instead `import 'uno.css'`. When you start the dev server first time, you'll need to update some style module to get it working (we're trying to fix it). ```ts // vite.config.js import solidPlugin from 'vite-plugin-solid' import Unocss from 'unocss/vite' export default { plugins: [ solidPlugin(), Unocss({ /* options */ }), ], } ``` You have a `Vite + Solid` example project on [examples/vite-solid](https://github.com/unocss/unocss/tree/main/examples/vite-solid) directory. ### Elm You need to add the `vite-plugin-elm` plugin before UnoCSS's plugin. ```ts // vite.config.js import { defineConfig } from 'vite' import elmPlugin from 'vite-plugin-elm' import Unocss from 'unocss/vite' export default defineConfig({ plugins: [ elmPlugin(), Unocss({ /* options */ }), ], }) ``` You have a `Vite + Elm` example project on [examples/vite-elm](https://github.com/unocss/unocss/tree/main/examples/vite-elm) directory. ## License MIT License © 2021-PRESENT [Anthony Fu](https://github.com/antfu)