@unocss/vite 0.50.6 → 0.50.7

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,452 +2,9 @@
2
2
 
3
3
  The Vite plugin for UnoCSS. Ships with the `unocss` package.
4
4
 
5
- ## Installation
5
+ ## Documentation
6
6
 
7
- ```bash
8
- npm i -D unocss
9
- ```
10
-
11
- ```ts
12
- // vite.config.ts
13
- import UnoCSS from 'unocss/vite'
14
-
15
- export default {
16
- plugins: [
17
- UnoCSS({ /* options */ }),
18
- ],
19
- }
20
- ```
21
-
22
- Add `uno.css` to your main entry:
23
-
24
- ```ts
25
- // main.ts
26
- import 'uno.css'
27
- ```
28
-
29
- ### Presetless usage
30
-
31
- > This plugin does not come with any default presets.
32
- > 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.
33
-
34
- ```bash
35
- npm i -D @unocss/vite
36
- ```
37
-
38
- ```ts
39
- // vite.config.ts
40
- import UnoCSS from '@unocss/vite'
41
-
42
- export default {
43
- plugins: [
44
- UnoCSS({
45
- presets: [
46
- /* no presets by default */
47
- ],
48
- /* options */
49
- }),
50
- ],
51
- }
52
- ```
53
-
54
- ## Modes
55
-
56
- The Vite plugin comes with a set of modes that enable different behaviors.
57
-
58
- ###### `global` (default)
59
-
60
- This is the default mode for the plugin: in this mode you need to add the import of `uno.css` on your entry point.
61
-
62
- This mode enables a set of Vite plugins for `build` and for `dev` with `HMR` support.
63
-
64
- The generated `css` will be a global stylesheet injected on the `index.html`.
65
-
66
- ###### `vue-scoped`
67
-
68
- This mode will inject generated CSS to Vue SFC's `<style scoped>` for isolation.
69
-
70
- ###### `svelte-scoped`
71
-
72
- This mode will inject generated CSS to Svelte's `<style>` for isolation.
73
-
74
- ###### `shadow-dom`
75
-
76
- 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.
77
-
78
- 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.
79
-
80
- ###### `per-module` (Experimental)
81
-
82
- This mode will generate a CSS sheet for each module, can be scoped.
83
-
84
- ###### `dist-chunk` (Experimental)
85
-
86
- This mode will generate a CSS sheet for each code chunk on build, great for MPA.
87
-
88
- ## "Design in DevTools"
89
-
90
- 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.
91
-
92
- ```ts
93
- import 'uno.css'
94
- import 'virtual:unocss-devtools'
95
- ```
96
-
97
- > ⚠️ Please use it with caution, under the hood we use [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/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](https://github.com/unocss/unocss/issues/511) or setup UI regression tests for your production build if possible.
98
-
99
- `virtual:unocss-devtools` will be an empty bundle in production.
100
-
101
- ## Frameworks
102
-
103
- 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.
104
-
105
- ### React
106
-
107
- **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).
108
-
109
- If you're using `@vitejs/plugin-react`:
110
-
111
- ```ts
112
- // vite.config.js
113
- import react from '@vitejs/plugin-react'
114
- import UnoCSS from 'unocss/vite'
115
-
116
- export default {
117
- plugins: [
118
- react(),
119
- UnoCSS({
120
- /* options */
121
- }),
122
- ],
123
- }
124
- ```
125
-
126
- or if you're using `@vitejs/plugin-react-refresh`:
127
-
128
- ```ts
129
- // vite.config.js
130
- import reactRefresh from '@vitejs/plugin-react-refresh'
131
- import UnoCSS from 'unocss/vite'
132
-
133
- export default {
134
- plugins: [
135
- reactRefresh(),
136
- UnoCSS({
137
- /* options */
138
- }),
139
- ],
140
- }
141
- ```
142
-
143
- If you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.
144
-
145
- If you are using `@vitejs/plugin-react` with `@unocss/preset-attributify`, you must add the plugin before `@vitejs/plugin-react`.
146
-
147
- ```ts
148
- // vite.config.js
149
- import react from '@vitejs/plugin-react'
150
- import UnoCSS from 'unocss/vite'
151
-
152
- export default {
153
- plugins: [
154
- UnoCSS({
155
- /* options */
156
- }),
157
- react(),
158
- ],
159
- }
160
- ```
161
-
162
- You have a `React` example project on [examples/vite-react](https://github.com/unocss/unocss/tree/main/examples/vite-react) directory using both plugins, check the scripts on `package.json` and its Vite configuration file.
163
-
164
- ### Preact
165
-
166
- If you're using `@preact/preset-vite`:
167
-
168
- ```ts
169
- // vite.config.js
170
- import preact from '@preact/preset-vite'
171
- import UnoCSS from 'unocss/vite'
172
-
173
- export default {
174
- plugins: [
175
- preact(),
176
- UnoCSS({
177
- /* options */
178
- }),
179
- ],
180
- }
181
- ```
182
-
183
- or if you're using `@prefresh/vite`:
184
-
185
- ```ts
186
- // vite.config.js
187
- import prefresh from '@prefresh/vite'
188
- import UnoCSS from 'unocss/vite'
189
-
190
- export default {
191
- plugins: [
192
- prefresh(),
193
- UnoCSS({
194
- /* options */
195
- }),
196
- ],
197
- }
198
- ```
199
-
200
- If you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.
201
-
202
- If you are using `@preact/preset-vite` with `@unocss/preset-attributify`, you must add the plugin before `@preact/preset-vite`.
203
-
204
- ```ts
205
- // vite.config.js
206
- import preact from '@preact/preset-vite'
207
- import UnoCSS from 'unocss/vite'
208
-
209
- export default {
210
- plugins: [
211
- UnoCSS({
212
- /* options */
213
- }),
214
- preact(),
215
- ],
216
- }
217
- ```
218
-
219
- You have a `Preact` example project on [examples/vite-preact](https://github.com/unocss/unocss/tree/main/examples/vite-preact) directory using both plugins, check the scripts on `package.json` and its Vite configuration file.
220
-
221
- ### Svelte
222
-
223
- You must add the plugin before `@sveltejs/vite-plugin-svelte`.
224
-
225
- To support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.
226
-
227
- You can use simple rules with `class:`, for example `class:bg-red-500={foo}` or using `shortcuts` to include multiples rules, see `src/App.svelte` on linked example project below.
228
-
229
- ```ts
230
- // vite.config.js
231
- import { svelte } from '@sveltejs/vite-plugin-svelte'
232
- import UnoCSS from 'unocss/vite'
233
- import { extractorSvelte } from '@unocss/core'
234
-
235
- export default {
236
- plugins: [
237
- UnoCSS({
238
- extractors: [extractorSvelte],
239
- /* more options */
240
- }),
241
- svelte(),
242
- ],
243
- }
244
- ```
245
-
246
- You have a `Vite + Svelte` example project on [examples/vite-svelte](https://github.com/unocss/unocss/tree/main/examples/vite-svelte) directory.
247
-
248
- ### Sveltekit
249
-
250
- To support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.
251
-
252
- 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.
253
-
254
- ```ts
255
- // vite.config.js
256
- import { sveltekit } from '@sveltejs/kit/vite'
257
- import UnoCSS from 'unocss/vite'
258
- import { extractorSvelte } from '@unocss/core'
259
-
260
- /** @type {import('vite').UserConfig} */
261
- const config = {
262
- plugins: [
263
- UnoCSS({
264
- extractors: [extractorSvelte],
265
- /* more options */
266
- }),
267
- sveltekit(),
268
- ],
269
- }
270
- ```
271
-
272
- You have a `SvelteKit` example project on [examples/sveltekit](https://github.com/unocss/unocss/tree/main/examples/sveltekit) directory.
273
-
274
- ### Svelte/SvelteKit Scoped Mode
275
-
276
- 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"`.
277
-
278
- Support for `class:foo` and `class:foo={bar}` is already included. There is no need to add the `extractorSvelte` when using `svelte-scoped` mode.
279
-
280
- 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`:
281
-
282
- ```html
283
- <style uno:preflights uno:safelist global></style>
284
- ```
285
-
286
- 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.
287
-
288
- ```ts
289
- // vite.config.js
290
- import { sveltekit } from '@sveltejs/kit/vite'
291
- import UnoCSS from 'unocss/vite'
292
-
293
- /** @type {import('vite').UserConfig} */
294
- const config = {
295
- plugins: [
296
- UnoCSS({
297
- mode: 'svelte-scoped',
298
- /* options */
299
- }),
300
- sveltekit(),
301
- ],
302
- }
303
- ```
304
-
305
- 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.
306
-
307
- ### Web Components
308
-
309
- To work with web components you need to enable `shadow-dom` mode on the plugin.
310
-
311
- 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.
312
-
313
- ```ts
314
- // vite.config.js
315
- import UnoCSS from 'unocss/vite'
316
-
317
- export default {
318
- plugins: [
319
- UnoCSS({
320
- mode: 'shadow-dom',
321
- /* more options */
322
- }),
323
- ],
324
- }
325
- ```
326
-
327
- On each `web component` just add `@unocss-placeholder` to its style css block:
328
- ```ts
329
- const template = document.createElement('template')
330
- template.innerHTML = `
331
- <style>
332
- :host {...}
333
- @unocss-placeholder
334
- </style>
335
- <div class="m-1em">
336
- ...
337
- </div>
338
- `
339
- ```
340
-
341
- If you're using [Lit](https://lit.dev/):
342
-
343
- ```ts
344
- @customElement('my-element')
345
- export class MyElement extends LitElement {
346
- static styles = css`
347
- :host {...}
348
- @unocss-placeholder
349
- `
350
- // ...
351
- }
352
- ```
353
-
354
- You have a `Web Components` example project on [examples/vite-lit](https://github.com/unocss/unocss/tree/main/examples/vite-lit) directory.
355
-
356
- #### `::part` built-in support
357
-
358
- 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 below.
359
-
360
- The `part-[<part-name>]:<rule|shortcut>` will work only with this plugin using the `shadow-dom` mode.
361
-
362
- 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.
363
-
364
- ```ts
365
- // vite.config.js
366
- import UnoCSS from 'unocss/vite'
367
-
368
- export default {
369
- plugins: [
370
- UnoCSS({
371
- mode: 'shadow-dom',
372
- shortcuts: [
373
- { 'cool-blue': 'bg-blue-500 text-white' },
374
- { 'cool-green': 'bg-green-500 text-black' },
375
- ],
376
- /* more options */
377
- }),
378
- ],
379
- }
380
- ```
381
-
382
- then in your web components:
383
-
384
- ```ts
385
- // my-container-wc.ts
386
- const template = document.createElement('template')
387
- template.innerHTML = `
388
- <style>
389
- @unocss-placeholder
390
- </style>
391
- <my-wc-with-parts class="part-[cool-part]:cool-blue part-[another-cool-part]:cool-green">...</my-wc-with-parts>
392
- `
393
- ```
394
-
395
- ```ts
396
- // my-wc-with-parts.ts
397
- const template = document.createElement('template')
398
- template.innerHTML = `
399
- <style>
400
- @unocss-placeholder
401
- </style>
402
- <div>
403
- <div part="cool-part">...</div>
404
- <div part="another-cool-part">...</div>
405
- </div>
406
- `
407
- ```
408
-
409
- ### Solid
410
-
411
- **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).
412
-
413
- ```ts
414
- // vite.config.js
415
- import solidPlugin from 'vite-plugin-solid'
416
- import UnoCSS from 'unocss/vite'
417
-
418
- export default {
419
- plugins: [
420
- solidPlugin(),
421
- UnoCSS({
422
- /* options */
423
- }),
424
- ],
425
- }
426
- ```
427
-
428
- You have a `Vite + Solid` example project on [examples/vite-solid](https://github.com/unocss/unocss/tree/main/examples/vite-solid) directory.
429
-
430
- ### Elm
431
-
432
- You need to add the `vite-plugin-elm` plugin before UnoCSS's plugin.
433
-
434
- ```ts
435
- // vite.config.js
436
- import { defineConfig } from 'vite'
437
- import elmPlugin from 'vite-plugin-elm'
438
- import UnoCSS from 'unocss/vite'
439
-
440
- export default defineConfig({
441
- plugins: [
442
- elmPlugin(),
443
- UnoCSS({
444
- /* options */
445
- }),
446
- ],
447
- })
448
- ```
449
-
450
- You have a `Vite + Elm` example project on [examples/vite-elm](https://github.com/unocss/unocss/tree/main/examples/vite-elm) directory.
7
+ Refer to https://unocss.dev/integrations/vite
451
8
 
452
9
  ## License
453
10
 
package/dist/index.cjs CHANGED
@@ -12,6 +12,7 @@ const node_path = require('node:path');
12
12
  const fs = require('node:fs/promises');
13
13
  const fg = require('fast-glob');
14
14
  const remapping = require('@ampproject/remapping');
15
+ const node_buffer = require('node:buffer');
15
16
  const fs$1 = require('node:fs');
16
17
  const node_url = require('node:url');
17
18
 
@@ -614,6 +615,12 @@ function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules,
614
615
  }, WARN_TIMEOUT);
615
616
  }
616
617
  }
618
+ function clearWarnTimer() {
619
+ if (resolvedWarnTimer) {
620
+ clearTimeout(resolvedWarnTimer);
621
+ resolvedWarnTimer = void 0;
622
+ }
623
+ }
617
624
  onInvalidate(() => {
618
625
  invalidate(10, /* @__PURE__ */ new Set([...entries, ...affectedModules]));
619
626
  });
@@ -650,6 +657,7 @@ function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules,
650
657
  const entry = resolveId(id);
651
658
  if (entry) {
652
659
  resolved = true;
660
+ clearWarnTimer();
653
661
  entries.add(entry);
654
662
  return entry;
655
663
  }
@@ -663,6 +671,9 @@ function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules,
663
671
  code: `__uno_hash_${hash}{--:'';}${css}`,
664
672
  map: { mappings: "" }
665
673
  };
674
+ },
675
+ closeBundle() {
676
+ clearWarnTimer();
666
677
  }
667
678
  },
668
679
  {
@@ -773,7 +784,7 @@ function PerModuleModePlugin({ uno, filter }) {
773
784
  if (!css && !hasScope)
774
785
  return null;
775
786
  if (hasScope)
776
- code = code.replace(SCOPE_IMPORT_RE, ` from 'data:text/javascript;base64,${Buffer.from(`export default () => "${hash}"`).toString("base64")}'`);
787
+ code = code.replace(SCOPE_IMPORT_RE, ` from 'data:text/javascript;base64,${node_buffer.Buffer.from(`export default () => "${hash}"`).toString("base64")}'`);
777
788
  moduleMap.set(hash, [id, css]);
778
789
  invalidate(hash);
779
790
  return {
package/dist/index.mjs CHANGED
@@ -8,6 +8,7 @@ import { resolve, isAbsolute, dirname } from 'node:path';
8
8
  import fs from 'node:fs/promises';
9
9
  import fg from 'fast-glob';
10
10
  import remapping from '@ampproject/remapping';
11
+ import { Buffer } from 'node:buffer';
11
12
  import fs$1 from 'node:fs';
12
13
  import { fileURLToPath } from 'node:url';
13
14
 
@@ -601,6 +602,12 @@ function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules,
601
602
  }, WARN_TIMEOUT);
602
603
  }
603
604
  }
605
+ function clearWarnTimer() {
606
+ if (resolvedWarnTimer) {
607
+ clearTimeout(resolvedWarnTimer);
608
+ resolvedWarnTimer = void 0;
609
+ }
610
+ }
604
611
  onInvalidate(() => {
605
612
  invalidate(10, /* @__PURE__ */ new Set([...entries, ...affectedModules]));
606
613
  });
@@ -637,6 +644,7 @@ function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules,
637
644
  const entry = resolveId(id);
638
645
  if (entry) {
639
646
  resolved = true;
647
+ clearWarnTimer();
640
648
  entries.add(entry);
641
649
  return entry;
642
650
  }
@@ -650,6 +658,9 @@ function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules,
650
658
  code: `__uno_hash_${hash}{--:'';}${css}`,
651
659
  map: { mappings: "" }
652
660
  };
661
+ },
662
+ closeBundle() {
663
+ clearWarnTimer();
653
664
  }
654
665
  },
655
666
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.50.6",
3
+ "version": "0.50.7",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -47,15 +47,15 @@
47
47
  "chokidar": "^3.5.3",
48
48
  "fast-glob": "^3.2.12",
49
49
  "magic-string": "^0.30.0",
50
- "@unocss/config": "0.50.6",
51
- "@unocss/core": "0.50.6",
52
- "@unocss/inspector": "0.50.6",
53
- "@unocss/scope": "0.50.6",
54
- "@unocss/transformer-directives": "0.50.6"
50
+ "@unocss/config": "0.50.7",
51
+ "@unocss/core": "0.50.7",
52
+ "@unocss/inspector": "0.50.7",
53
+ "@unocss/scope": "0.50.7",
54
+ "@unocss/transformer-directives": "0.50.7"
55
55
  },
56
56
  "devDependencies": {
57
- "vite": "^4.2.0",
58
- "@unocss/shared-integration": "0.50.6"
57
+ "vite": "^4.2.1",
58
+ "@unocss/shared-integration": "0.50.7"
59
59
  },
60
60
  "scripts": {
61
61
  "build": "unbuild",