
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@unocss/vite
Advanced tools
@unocss/vite is a utility-first CSS framework that integrates with Vite, a build tool for modern web projects. It provides a highly customizable and performant way to handle CSS in your Vite projects, leveraging the power of utility classes and on-demand generation.
Utility-First CSS
This feature allows you to use utility-first CSS classes in your project. The code sample shows how to integrate Unocss with a Vite project.
import { defineConfig } from 'vite';
import Unocss from '@unocss/vite';
export default defineConfig({
plugins: [
Unocss(),
],
});
On-Demand Generation
Unocss generates CSS on-demand, meaning only the styles you use in your HTML or JavaScript are included in the final build. The code sample demonstrates how to configure Unocss with a preset for on-demand generation.
import { defineConfig } from 'vite';
import Unocss from '@unocss/vite';
export default defineConfig({
plugins: [
Unocss({
presets: [
require('@unocss/preset-uno'),
],
}),
],
});
Customizable
Unocss is highly customizable, allowing you to define your own utility classes and rules. The code sample shows how to add custom rules for margin and padding.
import { defineConfig } from 'vite';
import Unocss from '@unocss/vite';
export default defineConfig({
plugins: [
Unocss({
rules: [
['m-1', { margin: '0.25rem' }],
['p-1', { padding: '0.25rem' }],
],
}),
],
});
Tailwind CSS is another utility-first CSS framework that provides a wide range of utility classes out of the box. Unlike Unocss, Tailwind CSS requires a configuration file and a build step to generate the final CSS.
Windi CSS is a next-generation utility-first CSS framework that is similar to Tailwind CSS but offers faster build times and on-demand generation. It is more similar to Unocss in terms of performance and on-demand capabilities.
Twind is a utility-first CSS-in-JS library that provides the benefits of Tailwind CSS without the need for a build step. It generates styles at runtime, making it more flexible but potentially less performant than Unocss.
The Vite plugin for UnoCSS. Ships with the unocss
package.
npm i -D unocss
// vite.config.ts
import Unocss from 'unocss/vite'
export default {
plugins: [
Unocss({ /* options */ }),
],
}
Add uno.css
to your main entry:
// main.ts
import 'uno.css'
This plugin does not come with any default presets. If you are building a meta framework on top of UnoCSS, see this file for an example to bind the default presets.
npm i -D @unocss/vite
// vite.config.ts
import Unocss from '@unocss/vite'
export default {
plugins: [
Unocss({
presets: [
/* no presets by default */
],
/* options */
}),
],
}
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 <style scoped>
for isolation.
svelte-scoped
This mode will inject generated CSS to Svelte's <style>
for isolation.
shadow-dom
Since Web Components
uses Shadow DOM
, there is no way to style content directly from a global stylesheet (unless you use custom css vars
, those will penetrate the Shadow DOM
), you need to inline the generated css by the plugin into the Shadow DOM
style.
To inline the generated css, you only need to configure the plugin mode to shadow-dom
and include @unocss-placeholder
magic placeholder on each web component style css block. If you are defining your Web Components in Vue SFCs and want to define custom styles alongside UnoCSS, you can wrap placeholder in a CSS comment to avoid syntax errors in your IDE.
per-module
(Experimental)This mode will generate a CSS sheet for each module, can be scoped.
dist-chunk
(Experimental)This mode will generate a CSS sheet for each code chunk on build, great for MPA.
Because of limitation of "on-demand" where the DevTools don't know those you haven't used in your source code yet. So if you want to try how things work by directly changing the classes in DevTools, just add the following lines to your main entry.
import 'uno.css'
import 'virtual:unocss-devtools'
⚠️ Please use it with caution, under the hood we use
MutationObserver
to detect the class changes. Which means not only your manual changes but also the changes made by your scripts will be detected and included in the stylesheet. This could cause some misalignment between dev and the production build when you add dynamic classes based on some logic in script tags. We recommended adding your dynamic parts to the safelist or setup UI regression tests for your production build if possible.
virtual:unocss-devtools
will be an empty bundle in production.
Some UI/App frameworks have some caveats that must be fixed to make it work, if you're using one of the following frameworks, just apply the suggestions.
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).
If you're using @vitejs/plugin-react
:
// vite.config.js
import react from '@vitejs/plugin-react'
import Unocss from 'unocss/vite'
export default {
plugins: [
react(),
Unocss({
/* options */
}),
],
}
or if you're using @vitejs/plugin-react-refresh
:
// vite.config.js
import reactRefresh from '@vitejs/plugin-react-refresh'
import Unocss from 'unocss/vite'
export default {
plugins: [
reactRefresh(),
Unocss({
/* options */
}),
],
}
If you're using @unocss/preset-attributify
you should remove tsc
from the build
script.
If you are using @vitejs/plugin-react
with @unocss/preset-attributify
, you must add the plugin before @vitejs/plugin-react
.
// vite.config.js
import react from '@vitejs/plugin-react'
import Unocss from 'unocss/vite'
export default {
plugins: [
Unocss({
/* options */
}),
react(),
],
}
You have a React
example project on examples/vite-react directory using both plugins, check the scripts on package.json
and its Vite configuration file.
If you're using @preact/preset-vite
:
// vite.config.js
import preact from '@preact/preset-vite'
import Unocss from 'unocss/vite'
export default {
plugins: [
preact(),
Unocss({
/* options */
}),
],
}
or if you're using @prefresh/vite
:
// vite.config.js
import prefresh from '@prefresh/vite'
import Unocss from 'unocss/vite'
export default {
plugins: [
prefresh(),
Unocss({
/* options */
}),
],
}
If you're using @unocss/preset-attributify
you should remove tsc
from the build
script.
If you are using @preact/preset-vite
with @unocss/preset-attributify
, you must add the plugin before @preact/preset-vite
.
// vite.config.js
import preact from '@preact/preset-vite'
import Unocss from 'unocss/vite'
export default {
plugins: [
Unocss({
/* options */
}),
preact(),
],
}
You have a Preact
example project on examples/vite-preact directory using both plugins, check the scripts on package.json
and its Vite configuration file.
You must add the plugin before @sveltejs/vite-plugin-svelte
.
To support class:foo
and class:foo={bar}
add the plugin and configure extractorSvelte
on extractors
option.
You can use simple rules with class:
, for example class:bg-red-500={foo}
or using shorcuts
to include multiples rules, see src/App.svelte
on linked example project below.
// vite.config.js
import { svelte } from '@sveltejs/vite-plugin-svelte'
import Unocss from 'unocss/vite'
import { extractorSvelte } from '@unocss/core'
export default {
plugins: [
Unocss({
extractors: [extractorSvelte],
/* more options */
}),
svelte(),
],
}
You have a Vite + Svelte
example project on examples/vite-svelte directory.
To support class:foo
and class:foo={bar}
add the plugin and configure extractorSvelte
on extractors
option.
You can use simple rules with class:
, for example class:bg-red-500={foo}
or using shortcuts
to include multiples rules, see src/routes/+layout.svelte
on linked example project below.
// vite.config.js
import { sveltekit } from '@sveltejs/kit/vite'
import UnoCSS from 'unocss/vite'
import { extractorSvelte } from '@unocss/core'
/** @type {import('vite').UserConfig} */
const config = {
plugins: [
UnoCSS({
extractors: [extractorSvelte],
/* more options */
}),
sveltekit(),
],
}
You have a SvelteKit
example project on examples/sveltekit directory.
Adding mode: 'svelte-scoped'
to your UnoCSS config options will place styles right inside of each component's style block instead of in a global uno.css
file. Due to automatic class name compilation, classes that depend on attributes in parent components (like dir="rtl"
or .dark
) will just work. Also, you can pass class to children components as long as you pass them using a prop named class
, e.g. class="text-lg bg-red-100"
.
Support for class:foo
and class:foo={bar}
is already included. There is no need to add the extractorSvelte
when using svelte-scoped
mode.
Because there is no import 'uno.css'
in your root +layout.svelte
preflights and safelist classes have no where to be placed. Add the uno:preflights
or uno:safelist
attributes to the style block of any component where you want to place them. To use both globally, add the following to your root +layout.svelte
:
<style uno:preflights uno:safelist global></style>
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.
// 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 directory with more detailed explanation of how this mode works.
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.
// 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:
const template = document.createElement('template')
template.innerHTML = `
<style>
:host {...}
@unocss-placeholder
</style>
<div class="m-1em">
...
</div>
`
If you're using Lit:
@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 directory.
::part
built-in supportYou can use ::part
since the plugin supports it via shortcuts
and using part-[<part-name>]:<rule|shortcut>
rule from preset-mini
, for example using it with simple rules like part-[<part-name>]:bg-green-500
or using some shortcut
: check src/my-element.ts
on linked example project below.
The part-[<part-name>]:<rule|shortcut>
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.
// 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:
// my-container-wc.ts
const template = document.createElement('template')
template.innerHTML = `
<style>
@unocss-placeholder
</style>
<my-wc-with-parts class="part-[cool-part]:cool-blue part-[another-cool-part]:cool-green">...</my-wc-with-parts>
`
// my-wc-with-parts.ts
const template = document.createElement('template')
template.innerHTML = `
<style>
@unocss-placeholder
</style>
<div>
<div part="cool-part">...</div>
<div part="another-cool-part">...</div>
</div>
`
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).
// 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 directory.
You need to add the vite-plugin-elm
plugin before UnoCSS's plugin.
// 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 directory.
MIT License © 2021-PRESENT Anthony Fu
FAQs
The Vite plugin for UnoCSS
The npm package @unocss/vite receives a total of 201,442 weekly downloads. As such, @unocss/vite popularity was classified as popular.
We found that @unocss/vite demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.