@unocss/vite 0.17.3 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,5 +2,364 @@
2
2
 
3
3
  The Vite plugin for UnoCSS. Ships with the `unocss` package.
4
4
 
5
- > This plugin does not come with any default presets. You are are building a meta framework on top of UnoCSS, see [this file](https://github.com/antfu/unocss/blob/main/packages/unocss/src/vite.ts) for an example to bind the default presets.
5
+ > This plugin does not come with any default presets. You are building a meta framework on top of UnoCSS, see [this file](https://github.com/antfu/unocss/blob/main/packages/unocss/src/vite.ts) for an example to bind the default presets.
6
6
 
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm i -D unocss
11
+ ```
12
+
13
+ ```ts
14
+ // vite.config.ts
15
+ import Unocss from 'unocss/vite'
16
+
17
+ export default {
18
+ plugins: [
19
+ Unocss({ /* options */ })
20
+ ]
21
+ }
22
+ ```
23
+
24
+ Add `uno.css` to your main entry:
25
+
26
+ ```ts
27
+ // main.ts
28
+ import 'uno.css'
29
+ ```
30
+
31
+ ## Modes
32
+
33
+ The Vite plugin comes with a set of modes that enable different behaviors.
34
+
35
+ ### global (default)
36
+
37
+ This is the default mode for the plugin: in this mode you need to add the import of `uno.css` on your entry point.
38
+
39
+ This mode enables a set of Vite plugins for `build` and for `dev` with `HMR` support.
40
+
41
+ The generated `css` will be a global stylesheet injected on the `index.html`.
42
+
43
+ ### vue-scoped (WIP)
44
+
45
+ This mode will inject generated CSS to Vue SFC's `<style scoped>` for isolation.
46
+
47
+ ### per-module (WIP)
48
+
49
+ This mode will generate a CSS sheet for each module, can be scoped.
50
+
51
+ ### dist-chunk (WIP)
52
+
53
+ This mode will generate a CSS sheet for each code chunk on build, great for MPA.
54
+
55
+ ### shadow-dom
56
+
57
+ 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.
58
+
59
+ 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.
60
+
61
+ ## Frameworks
62
+
63
+ 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.
64
+
65
+ ### React
66
+
67
+ If you're using `@vitejs/plugin-react`:
68
+
69
+ ```ts
70
+ // vite.config.js
71
+ import react from '@vitejs/plugin-react'
72
+ import Unocss from 'unocss/vite'
73
+
74
+ export default {
75
+ plugins: [
76
+ react(),
77
+ Unocss({
78
+ /* options */
79
+ }),
80
+ ]
81
+ }
82
+ ```
83
+
84
+ or if you're using `@vitejs/plugin-react-refresh`:
85
+
86
+ ```ts
87
+ // vite.config.js
88
+ import reactRefresh from '@vitejs/plugin-react-refresh'
89
+ import Unocss from 'unocss/vite'
90
+
91
+ export default {
92
+ plugins: [
93
+ reactRefresh(),
94
+ Unocss({
95
+ /* options */
96
+ }),
97
+ ]
98
+ }
99
+ ```
100
+
101
+ If you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.
102
+
103
+ If you are using `@vitejs/plugin-react` with `@unocss/preset-attributify`, you must add the plugin before `@vitejs/plugin-react`.
104
+
105
+ ```ts
106
+ // vite.config.js
107
+ import react from '@vitejs/plugin-react'
108
+ import Unocss from 'unocss/vite'
109
+
110
+ export default {
111
+ plugins: [
112
+ Unocss({
113
+ /* options */
114
+ }),
115
+ react()
116
+ ]
117
+ }
118
+ ```
119
+
120
+ You have a `React` example project on [test/fixtures/vite-react](https://github.com/antfu/unocss/tree/main/test/fixtures/vite-react) directory using both plugins, check the scripts on `package.json` and its Vite configuration file.
121
+
122
+ ### Preact
123
+
124
+ If you're using `@preact/preset-vite`:
125
+
126
+ ```ts
127
+ // vite.config.js
128
+ import preact from '@preact/preset-vite'
129
+ import Unocss from 'unocss/vite'
130
+
131
+ export default {
132
+ plugins: [
133
+ preact(),
134
+ Unocss({
135
+ /* options */
136
+ }),
137
+ ]
138
+ }
139
+ ```
140
+
141
+ or if you're using `@prefresh/vite`:
142
+
143
+ ```ts
144
+ // vite.config.js
145
+ import prefresh from '@prefresh/vite'
146
+ import Unocss from 'unocss/vite'
147
+
148
+ export default {
149
+ plugins: [
150
+ prefresh(),
151
+ Unocss({
152
+ /* options */
153
+ }),
154
+ ]
155
+ }
156
+ ```
157
+
158
+ If you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.
159
+
160
+ If you are using `@preact/preset-vite` with `@unocss/preset-attributify`, you must add the plugin before `@preact/preset-vite`.
161
+
162
+ ```ts
163
+ // vite.config.js
164
+ import preact from '@preact/preset-vite'
165
+ import Unocss from 'unocss/vite'
166
+
167
+ export default {
168
+ plugins: [
169
+ Unocss({
170
+ /* options */
171
+ }),
172
+ preact()
173
+ ]
174
+ }
175
+ ```
176
+
177
+ You have a `Preact` example project on [test/fixtures/vite-preact](https://github.com/antfu/unocss/tree/main/test/fixtures/vite-preact) directory using both plugins, check the scripts on `package.json` and its Vite configuration file.
178
+
179
+ ### Svelte
180
+
181
+ You must add the plugin before `@sveltejs/vite-plugin-svelte`.
182
+
183
+ To support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.
184
+
185
+ 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 bellow.
186
+
187
+ ```ts
188
+ // vite.config.js
189
+ import { svelte } from '@sveltejs/vite-plugin-svelte'
190
+ import Unocss from 'unocss/vite'
191
+ import { extractorSvelte } from '@unocss/core'
192
+
193
+ export default {
194
+ plugins: [
195
+ Unocss({
196
+ extractors: [extractorSvelte],
197
+ /* more options */
198
+ }),
199
+ svelte()
200
+ ]
201
+ }
202
+ ```
203
+
204
+ You have a `Vite + Svelte` example project on [test/fixtures/vite-svelte](https://github.com/antfu/unocss/tree/main/test/fixtures/vite-svelte) directory.
205
+
206
+ ### Sveltekit
207
+
208
+ To support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.
209
+
210
+ You can use simple rules with `class:`, for example `class:bg-red-500={foo}` or using `shorcuts` to include multiples rules, see `src/routes/__layout.svelte` on linked example project bellow.
211
+
212
+ ```ts
213
+ // svelte.config.js
214
+ import preprocess from 'svelte-preprocess'
215
+ import UnoCss from 'unocss/vite'
216
+ import { extractorSvelte } from '@unocss/core'
217
+
218
+ /** @type {import('@sveltejs/kit').Config} */
219
+ const config = {
220
+ // Consult https://github.com/sveltejs/svelte-preprocess
221
+ // for more information about preprocessors
222
+ preprocess: preprocess(),
223
+
224
+ kit: {
225
+
226
+ // hydrate the <div id="svelte"> element in src/app.html
227
+ target: '#svelte',
228
+ vite: {
229
+ plugins: [
230
+ UnoCss({
231
+ extractors: [extractorSvelte],
232
+ /* more options */
233
+ })
234
+ ]
235
+ }
236
+ }
237
+ }
238
+ ```
239
+
240
+ You have a `SvelteKit` example project on [test/fixtures/sveltekit](https://github.com/antfu/unocss/tree/main/test/fixtures/sveltekit) directory.
241
+
242
+ ### Web Components
243
+
244
+ To work with web components you need to enable `shadow-dom` mode on the plugin.
245
+
246
+ 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.
247
+
248
+ ```ts
249
+ // vite.config.js
250
+ import Unocss from 'unocss/vite'
251
+
252
+ export default {
253
+ plugins: [
254
+ Unocss({
255
+ mode: 'shadow-dom',
256
+ /* more options */
257
+ }),
258
+ ]
259
+ }
260
+ ```
261
+
262
+ On each `web component` just add `@unocss-placeholder` to its style css block:
263
+ ```ts
264
+ const template = document.createElement('template')
265
+ template.innerHTML = `
266
+ <style>
267
+ :host {...}
268
+ @unocss-placeholder
269
+ </style>
270
+ <div class="m-1em">
271
+ ...
272
+ </div>
273
+ `
274
+ ```
275
+
276
+ If you're using [Lit](https://lit.dev/):
277
+
278
+ ```ts
279
+ @customElement('my-element')
280
+ export class MyElement extends LitElement {
281
+ static styles = css`
282
+ :host {...}
283
+ @unocss-placeholder
284
+ `
285
+ ...
286
+ }
287
+ ```
288
+
289
+ You have a `Web Components` example project on [test/fixtures/vite-lit](https://github.com/antfu/unocss/tree/main/test/fixtures/vite-lit) directory.
290
+
291
+ #### `::part` built-in support
292
+
293
+ You 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 bellow.
294
+
295
+ The `part-[<part-name>]:<rule|shortcut>` will work only with this plugin using the `shadow-dom` mode.
296
+
297
+ 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.
298
+
299
+ ```ts
300
+ // vite.config.js
301
+ import Unocss from 'unocss/vite'
302
+
303
+ export default {
304
+ plugins: [
305
+ Unocss({
306
+ mode: 'shadow-dom',
307
+ shortcuts: [
308
+ { 'cool-blue': 'bg-blue-500 text-white' },
309
+ { 'cool-green': 'bg-green-500 text-black' },
310
+ ],
311
+ /* more options */
312
+ }),
313
+ ]
314
+ }
315
+ ```
316
+
317
+ then in your web components:
318
+
319
+ ```ts
320
+ // my-container-wc.ts
321
+ const template = document.createElement('template')
322
+ template.innerHTML = `
323
+ <style>
324
+ @unocss-placeholder
325
+ </style>
326
+ <my-wc-with-parts class="part-[cool-part]:cool-blue part-[another-cool-part]:cool-green">...</my-wc-with-parts>
327
+ `
328
+ ```
329
+
330
+ ```ts
331
+ // my-wc-with-parts.ts
332
+ const template = document.createElement('template')
333
+ template.innerHTML = `
334
+ <style>
335
+ @unocss-placeholder
336
+ </style>
337
+ <div>
338
+ <div part="cool-part">...</div>
339
+ <div part="another-cool-part">...</div>
340
+ </div>
341
+ `
342
+ ```
343
+
344
+ ### Solid
345
+
346
+ ```ts
347
+ // vite.config.js
348
+ import solidPlugin from 'vite-plugin-solid'
349
+ import Unocss from 'unocss/vite'
350
+
351
+ export default {
352
+ plugins: [
353
+ solidPlugin(),
354
+ Unocss({
355
+ /* options */
356
+ }),
357
+ ]
358
+ }
359
+ ```
360
+
361
+ You have a `Vite + Solid` example project on [test/fixtures/vite-solid](https://github.com/antfu/unocss/tree/main/test/fixtures/vite-solid) directory.
362
+
363
+ ## License
364
+
365
+ MIT License © 2021-PRESENT [Anthony Fu](https://github.com/antfu)
package/dist/index.d.ts CHANGED
@@ -17,6 +17,7 @@ interface VitePluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
17
17
  * - `dist-chunk` - generate a CSS sheet for each code chunk on build, great for MPA
18
18
  * - `per-module` - generate a CSS sheet for each module, can be scoped
19
19
  * - `vue-scoped` - inject generated CSS to Vue SFC's `<style scoped>` for isolation
20
+ * - `shadow-dom` - inject generated CSS to `Shadow DOM` css style block for each web component
20
21
  *
21
22
  * @default 'global'
22
23
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.17.3",
3
+ "version": "0.20.0",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -35,13 +35,13 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@rollup/pluginutils": "^4.1.2",
38
- "@unocss/config": "0.17.3",
39
- "@unocss/core": "0.17.3",
40
- "@unocss/inspector": "0.17.3",
41
- "@unocss/scope": "0.17.3"
38
+ "@unocss/config": "0.20.0",
39
+ "@unocss/core": "0.20.0",
40
+ "@unocss/inspector": "0.20.0",
41
+ "@unocss/scope": "0.20.0"
42
42
  },
43
43
  "devDependencies": {
44
- "vite": "^2.7.4"
44
+ "vite": "^2.7.7"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "unbuild",