@unocss/vite 0.31.17 → 0.32.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 +32 -10
- package/dist/index.cjs +52 -11
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +52 -11
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -28,11 +28,33 @@ Add `uno.css` to your main entry:
|
|
|
28
28
|
import 'uno.css'
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
### Presetless usage
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm i -D @unocss/vite
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// vite.config.ts
|
|
39
|
+
import Unocss from '@unocss/vite'
|
|
40
|
+
|
|
41
|
+
export default {
|
|
42
|
+
plugins: [
|
|
43
|
+
Unocss({
|
|
44
|
+
presets: [
|
|
45
|
+
/* no presets by default */
|
|
46
|
+
],
|
|
47
|
+
/* options */
|
|
48
|
+
}),
|
|
49
|
+
],
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
31
53
|
## Modes
|
|
32
54
|
|
|
33
55
|
The Vite plugin comes with a set of modes that enable different behaviors.
|
|
34
56
|
|
|
35
|
-
|
|
57
|
+
###### `global` (default)
|
|
36
58
|
|
|
37
59
|
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
60
|
|
|
@@ -40,25 +62,25 @@ This mode enables a set of Vite plugins for `build` and for `dev` with `HMR` sup
|
|
|
40
62
|
|
|
41
63
|
The generated `css` will be a global stylesheet injected on the `index.html`.
|
|
42
64
|
|
|
43
|
-
|
|
65
|
+
###### `vue-scoped`
|
|
44
66
|
|
|
45
67
|
This mode will inject generated CSS to Vue SFC's `<style scoped>` for isolation.
|
|
46
68
|
|
|
47
|
-
|
|
69
|
+
###### `svelte-scoped`
|
|
48
70
|
|
|
49
71
|
This mode will inject generated CSS to Svelte's `<style>` for isolation.
|
|
50
72
|
|
|
51
|
-
|
|
73
|
+
###### `shadow-dom`
|
|
52
74
|
|
|
53
75
|
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.
|
|
54
76
|
|
|
55
77
|
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.
|
|
56
78
|
|
|
57
|
-
|
|
79
|
+
###### `per-module` (Experimental)
|
|
58
80
|
|
|
59
81
|
This mode will generate a CSS sheet for each module, can be scoped.
|
|
60
82
|
|
|
61
|
-
|
|
83
|
+
###### `dist-chunk` (Experimental)
|
|
62
84
|
|
|
63
85
|
This mode will generate a CSS sheet for each code chunk on build, great for MPA.
|
|
64
86
|
|
|
@@ -201,7 +223,7 @@ You must add the plugin before `@sveltejs/vite-plugin-svelte`.
|
|
|
201
223
|
|
|
202
224
|
To support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.
|
|
203
225
|
|
|
204
|
-
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
|
|
226
|
+
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.
|
|
205
227
|
|
|
206
228
|
```ts
|
|
207
229
|
// vite.config.js
|
|
@@ -226,7 +248,7 @@ You have a `Vite + Svelte` example project on [examples/vite-svelte](https://git
|
|
|
226
248
|
|
|
227
249
|
To support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.
|
|
228
250
|
|
|
229
|
-
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
|
|
251
|
+
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 below.
|
|
230
252
|
|
|
231
253
|
```ts
|
|
232
254
|
// svelte.config.js
|
|
@@ -240,7 +262,7 @@ const config = {
|
|
|
240
262
|
// for more information about preprocessors
|
|
241
263
|
preprocess: preprocess(),
|
|
242
264
|
kit: {
|
|
243
|
-
|
|
265
|
+
vite: {
|
|
244
266
|
plugins: [
|
|
245
267
|
UnoCss({
|
|
246
268
|
extractors: [extractorSvelte],
|
|
@@ -305,7 +327,7 @@ You have a `Web Components` example project on [examples/vite-lit](https://githu
|
|
|
305
327
|
|
|
306
328
|
#### `::part` built-in support
|
|
307
329
|
|
|
308
|
-
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
|
|
330
|
+
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.
|
|
309
331
|
|
|
310
332
|
The `part-[<part-name>]:<rule|shortcut>` will work only with this plugin using the `shadow-dom` mode.
|
|
311
333
|
|
package/dist/index.cjs
CHANGED
|
@@ -119,12 +119,29 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
119
119
|
};
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
function getHash(input, length = 8) {
|
|
123
|
-
return crypto.createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
124
|
-
}
|
|
125
122
|
function getPath(id) {
|
|
126
123
|
return id.replace(/\?.*$/, "");
|
|
127
124
|
}
|
|
125
|
+
function replaceAsync(string, searchValue, replacer) {
|
|
126
|
+
try {
|
|
127
|
+
if (typeof replacer === "function") {
|
|
128
|
+
const values = [];
|
|
129
|
+
String.prototype.replace.call(string, searchValue, (...args) => {
|
|
130
|
+
values.push(replacer(...args));
|
|
131
|
+
return "";
|
|
132
|
+
});
|
|
133
|
+
return Promise.all(values).then((resolvedValues) => {
|
|
134
|
+
return String.prototype.replace.call(string, searchValue, () => {
|
|
135
|
+
return resolvedValues.shift() || "";
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
} else {
|
|
139
|
+
return Promise.resolve(String.prototype.replace.call(string, searchValue, replacer));
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
return Promise.reject(error);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
128
145
|
|
|
129
146
|
function ChunkModeBuildPlugin({ uno, filter }) {
|
|
130
147
|
let cssPlugin;
|
|
@@ -168,10 +185,31 @@ function ChunkModeBuildPlugin({ uno, filter }) {
|
|
|
168
185
|
};
|
|
169
186
|
}
|
|
170
187
|
|
|
171
|
-
function
|
|
188
|
+
function getHash(input, length = 8) {
|
|
189
|
+
return crypto.createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }) {
|
|
172
193
|
const vfsLayerMap = /* @__PURE__ */ new Map();
|
|
173
194
|
let tasks = [];
|
|
195
|
+
let cssPostPlugin;
|
|
174
196
|
let cssPlugin;
|
|
197
|
+
async function transformCSS(css, id) {
|
|
198
|
+
const {
|
|
199
|
+
postcss = true
|
|
200
|
+
} = await getConfig();
|
|
201
|
+
if (!cssPlugin || !postcss)
|
|
202
|
+
return css;
|
|
203
|
+
const result = await cssPlugin.transform(css, id);
|
|
204
|
+
if (!result)
|
|
205
|
+
return css;
|
|
206
|
+
if (typeof result === "string")
|
|
207
|
+
css = result;
|
|
208
|
+
else if (result.code)
|
|
209
|
+
css = result.code;
|
|
210
|
+
css = css.replace(/[\n\r]/g, "");
|
|
211
|
+
return css;
|
|
212
|
+
}
|
|
175
213
|
return [
|
|
176
214
|
{
|
|
177
215
|
name: "unocss:global:build:scan",
|
|
@@ -205,23 +243,25 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
|
|
|
205
243
|
return getLayerPlaceholder(layer);
|
|
206
244
|
},
|
|
207
245
|
async configResolved(config) {
|
|
208
|
-
|
|
246
|
+
cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
247
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css");
|
|
209
248
|
await ready;
|
|
210
249
|
},
|
|
211
250
|
async renderChunk(_, chunk) {
|
|
212
|
-
if (!
|
|
251
|
+
if (!cssPostPlugin)
|
|
213
252
|
return null;
|
|
214
253
|
const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
|
|
215
254
|
if (!chunks.length)
|
|
216
255
|
return null;
|
|
217
256
|
const tokens2 = /* @__PURE__ */ new Set();
|
|
218
257
|
await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
|
|
219
|
-
|
|
258
|
+
let { css } = await uno.generate(tokens2, { minify: true });
|
|
220
259
|
if (!css)
|
|
221
260
|
return null;
|
|
222
|
-
const hash = getHash(css);
|
|
223
261
|
const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
|
|
224
|
-
await
|
|
262
|
+
css = await transformCSS(css, fakeCssId);
|
|
263
|
+
const hash = getHash(css);
|
|
264
|
+
await cssPostPlugin.transform(getHashPlaceholder(hash), fakeCssId);
|
|
225
265
|
chunk.modules[fakeCssId] = {
|
|
226
266
|
code: null,
|
|
227
267
|
originalLength: 0,
|
|
@@ -249,9 +289,10 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
|
|
|
249
289
|
for (const file of cssFiles) {
|
|
250
290
|
const chunk = bundle[file];
|
|
251
291
|
if (chunk.type === "asset" && typeof chunk.source === "string") {
|
|
252
|
-
|
|
292
|
+
const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
|
|
293
|
+
chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_2, __, layer) => {
|
|
253
294
|
replaced = true;
|
|
254
|
-
return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "";
|
|
295
|
+
return await transformCSS(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`);
|
|
255
296
|
});
|
|
256
297
|
}
|
|
257
298
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -29,13 +29,19 @@ interface VitePluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
|
|
|
29
29
|
* @default false
|
|
30
30
|
*/
|
|
31
31
|
transformCSS?: boolean | 'pre' | 'post';
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* make the generate css processed by postcss(https://vitejs.dev/guide/features.html#postcss)
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
postcss?: boolean;
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
declare function ChunkModeBuildPlugin({ uno, filter }: UnocssPluginContext): Plugin;
|
|
35
41
|
|
|
36
42
|
declare function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
|
|
37
43
|
|
|
38
|
-
declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter }: UnocssPluginContext): Plugin[];
|
|
44
|
+
declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }: UnocssPluginContext<VitePluginConfig>): Plugin[];
|
|
39
45
|
|
|
40
46
|
declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
|
|
41
47
|
|
package/dist/index.mjs
CHANGED
|
@@ -109,12 +109,29 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
function getHash(input, length = 8) {
|
|
113
|
-
return createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
114
|
-
}
|
|
115
112
|
function getPath(id) {
|
|
116
113
|
return id.replace(/\?.*$/, "");
|
|
117
114
|
}
|
|
115
|
+
function replaceAsync(string, searchValue, replacer) {
|
|
116
|
+
try {
|
|
117
|
+
if (typeof replacer === "function") {
|
|
118
|
+
const values = [];
|
|
119
|
+
String.prototype.replace.call(string, searchValue, (...args) => {
|
|
120
|
+
values.push(replacer(...args));
|
|
121
|
+
return "";
|
|
122
|
+
});
|
|
123
|
+
return Promise.all(values).then((resolvedValues) => {
|
|
124
|
+
return String.prototype.replace.call(string, searchValue, () => {
|
|
125
|
+
return resolvedValues.shift() || "";
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
return Promise.resolve(String.prototype.replace.call(string, searchValue, replacer));
|
|
130
|
+
}
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return Promise.reject(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
118
135
|
|
|
119
136
|
function ChunkModeBuildPlugin({ uno, filter }) {
|
|
120
137
|
let cssPlugin;
|
|
@@ -158,10 +175,31 @@ function ChunkModeBuildPlugin({ uno, filter }) {
|
|
|
158
175
|
};
|
|
159
176
|
}
|
|
160
177
|
|
|
161
|
-
function
|
|
178
|
+
function getHash(input, length = 8) {
|
|
179
|
+
return createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }) {
|
|
162
183
|
const vfsLayerMap = /* @__PURE__ */ new Map();
|
|
163
184
|
let tasks = [];
|
|
185
|
+
let cssPostPlugin;
|
|
164
186
|
let cssPlugin;
|
|
187
|
+
async function transformCSS(css, id) {
|
|
188
|
+
const {
|
|
189
|
+
postcss = true
|
|
190
|
+
} = await getConfig();
|
|
191
|
+
if (!cssPlugin || !postcss)
|
|
192
|
+
return css;
|
|
193
|
+
const result = await cssPlugin.transform(css, id);
|
|
194
|
+
if (!result)
|
|
195
|
+
return css;
|
|
196
|
+
if (typeof result === "string")
|
|
197
|
+
css = result;
|
|
198
|
+
else if (result.code)
|
|
199
|
+
css = result.code;
|
|
200
|
+
css = css.replace(/[\n\r]/g, "");
|
|
201
|
+
return css;
|
|
202
|
+
}
|
|
165
203
|
return [
|
|
166
204
|
{
|
|
167
205
|
name: "unocss:global:build:scan",
|
|
@@ -195,23 +233,25 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
|
|
|
195
233
|
return getLayerPlaceholder(layer);
|
|
196
234
|
},
|
|
197
235
|
async configResolved(config) {
|
|
198
|
-
|
|
236
|
+
cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
237
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css");
|
|
199
238
|
await ready;
|
|
200
239
|
},
|
|
201
240
|
async renderChunk(_, chunk) {
|
|
202
|
-
if (!
|
|
241
|
+
if (!cssPostPlugin)
|
|
203
242
|
return null;
|
|
204
243
|
const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
|
|
205
244
|
if (!chunks.length)
|
|
206
245
|
return null;
|
|
207
246
|
const tokens2 = /* @__PURE__ */ new Set();
|
|
208
247
|
await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
|
|
209
|
-
|
|
248
|
+
let { css } = await uno.generate(tokens2, { minify: true });
|
|
210
249
|
if (!css)
|
|
211
250
|
return null;
|
|
212
|
-
const hash = getHash(css);
|
|
213
251
|
const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
|
|
214
|
-
await
|
|
252
|
+
css = await transformCSS(css, fakeCssId);
|
|
253
|
+
const hash = getHash(css);
|
|
254
|
+
await cssPostPlugin.transform(getHashPlaceholder(hash), fakeCssId);
|
|
215
255
|
chunk.modules[fakeCssId] = {
|
|
216
256
|
code: null,
|
|
217
257
|
originalLength: 0,
|
|
@@ -239,9 +279,10 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
|
|
|
239
279
|
for (const file of cssFiles) {
|
|
240
280
|
const chunk = bundle[file];
|
|
241
281
|
if (chunk.type === "asset" && typeof chunk.source === "string") {
|
|
242
|
-
|
|
282
|
+
const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
|
|
283
|
+
chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_2, __, layer) => {
|
|
243
284
|
replaced = true;
|
|
244
|
-
return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "";
|
|
285
|
+
return await transformCSS(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`);
|
|
245
286
|
});
|
|
246
287
|
}
|
|
247
288
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.7",
|
|
4
4
|
"description": "The Vite plugin for UnoCSS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"unocss",
|
|
@@ -43,19 +43,19 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@rollup/pluginutils": "^4.2.1",
|
|
46
|
-
"@unocss/config": "0.
|
|
47
|
-
"@unocss/core": "0.
|
|
48
|
-
"@unocss/inspector": "0.
|
|
49
|
-
"@unocss/scope": "0.
|
|
50
|
-
"@unocss/transformer-directives": "0.
|
|
46
|
+
"@unocss/config": "0.32.7",
|
|
47
|
+
"@unocss/core": "0.32.7",
|
|
48
|
+
"@unocss/inspector": "0.32.7",
|
|
49
|
+
"@unocss/scope": "0.32.7",
|
|
50
|
+
"@unocss/transformer-directives": "0.32.7",
|
|
51
51
|
"magic-string": "^0.26.1"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"vite": "^2.9.
|
|
54
|
+
"vite": "^2.9.6"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "unbuild",
|
|
58
58
|
"stub": "unbuild --stub"
|
|
59
59
|
},
|
|
60
|
-
"readme": "# @unocss/vite\n\nThe Vite plugin for UnoCSS. Ships with the `unocss` package.\n\n> 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/unocss/unocss/blob/main/packages/unocss/src/vite.ts) for an example to bind the default presets.\n\n## Installation\n\n```bash\nnpm i -D unocss\n```\n\n```ts\n// vite.config.ts\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({ /* options */ }),\n ],\n}\n```\n\nAdd `uno.css` to your main entry:\n\n```ts\n// main.ts\nimport 'uno.css'\n```\n\n## Modes\n\nThe Vite plugin comes with a set of modes that enable different behaviors.\n\n### global (default)\n\nThis is the default mode for the plugin: in this mode you need to add the import of `uno.css` on your entry point.\n\nThis mode enables a set of Vite plugins for `build` and for `dev` with `HMR` support.\n\nThe generated `css` will be a global stylesheet injected on the `index.html`.\n\n### vue-scoped\n\nThis mode will inject generated CSS to Vue SFC's `<style scoped>` for isolation.\n\n### svelte-scoped\n\nThis mode will inject generated CSS to Svelte's `<style>` for isolation.\n\n### shadow-dom\n\nSince `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.\n\nTo 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.\n\n### per-module (Experimental)\n\nThis mode will generate a CSS sheet for each module, can be scoped.\n\n### dist-chunk (Experimental)\n\nThis mode will generate a CSS sheet for each code chunk on build, great for MPA.\n\n## \"Design in DevTools\"\n\nBecause 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.\n\n```ts\nimport 'uno.css'\nimport 'virtual:unocss-devtools'\n```\n\n> ⚠️ 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.\n\n`virtual:unocss-devtools` will be an empty bundle in production.\n\n## Frameworks\n\nSome 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.\n\n### React\n\n**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).\n\nIf you're using `@vitejs/plugin-react`:\n\n```ts\n// vite.config.js\nimport react from '@vitejs/plugin-react'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n react(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nor if you're using `@vitejs/plugin-react-refresh`:\n\n```ts\n// vite.config.js\nimport reactRefresh from '@vitejs/plugin-react-refresh'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n reactRefresh(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nIf you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.\n\nIf you are using `@vitejs/plugin-react` with `@unocss/preset-attributify`, you must add the plugin before `@vitejs/plugin-react`.\n\n```ts\n// vite.config.js\nimport react from '@vitejs/plugin-react'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n /* options */\n }),\n react(),\n ],\n}\n```\n\nYou 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.\n\n### Preact\n\nIf you're using `@preact/preset-vite`:\n\n```ts\n// vite.config.js\nimport preact from '@preact/preset-vite'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n preact(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nor if you're using `@prefresh/vite`:\n\n```ts\n// vite.config.js\nimport prefresh from '@prefresh/vite'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n prefresh(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nIf you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.\n\nIf you are using `@preact/preset-vite` with `@unocss/preset-attributify`, you must add the plugin before `@preact/preset-vite`.\n\n```ts\n// vite.config.js\nimport preact from '@preact/preset-vite'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n /* options */\n }),\n preact(),\n ],\n}\n```\n\nYou 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.\n\n### Svelte\n\nYou must add the plugin before `@sveltejs/vite-plugin-svelte`.\n\nTo support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.\n\nYou 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.\n\n```ts\n// vite.config.js\nimport { svelte } from '@sveltejs/vite-plugin-svelte'\nimport Unocss from 'unocss/vite'\nimport { extractorSvelte } from '@unocss/core'\n\nexport default {\n plugins: [\n Unocss({\n extractors: [extractorSvelte],\n /* more options */\n }),\n svelte(),\n ],\n}\n```\n\nYou have a `Vite + Svelte` example project on [examples/vite-svelte](https://github.com/unocss/unocss/tree/main/examples/vite-svelte) directory.\n\n### Sveltekit\n\nTo support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.\n\nYou 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.\n\n```ts\n// svelte.config.js\nimport preprocess from 'svelte-preprocess'\nimport UnoCss from 'unocss/vite'\nimport { extractorSvelte } from '@unocss/core'\n\n/** @type {import('@sveltejs/kit').Config} */\nconst config = {\n // Consult https://github.com/sveltejs/svelte-preprocess\n // for more information about preprocessors\n preprocess: preprocess(),\n kit: {\n vite: {\n plugins: [\n UnoCss({\n extractors: [extractorSvelte],\n /* more options */\n }),\n ],\n },\n },\n}\n```\n\nYou have a `SvelteKit` example project on [examples/sveltekit](https://github.com/unocss/unocss/tree/main/examples/sveltekit) directory.\n\n### Web Components\n\nTo work with web components you need to enable `shadow-dom` mode on the plugin.\n\nDon't forget to remove the import for `uno.css` since the `shadow-dom` mode will not expose it and the application will not work.\n\n```ts\n// vite.config.js\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n mode: 'shadow-dom',\n /* more options */\n }),\n ],\n}\n```\n\nOn each `web component` just add `@unocss-placeholder` to its style css block:\n```ts\nconst template = document.createElement('template')\ntemplate.innerHTML = `\n<style>\n:host {...}\n@unocss-placeholder\n</style>\n<div class=\"m-1em\">\n...\n</div>\n`\n```\n\nIf you're using [Lit](https://lit.dev/):\n\n```ts\n@customElement('my-element')\nexport class MyElement extends LitElement {\n static styles = css`\n :host {...}\n @unocss-placeholder\n `\n // ...\n}\n```\n\nYou have a `Web Components` example project on [examples/vite-lit](https://github.com/unocss/unocss/tree/main/examples/vite-lit) directory.\n\n#### `::part` built-in support\n\nYou 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.\n\nThe `part-[<part-name>]:<rule|shortcut>` will work only with this plugin using the `shadow-dom` mode.\n\nThe 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.\n\n```ts\n// vite.config.js\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n mode: 'shadow-dom',\n shortcuts: [\n { 'cool-blue': 'bg-blue-500 text-white' },\n { 'cool-green': 'bg-green-500 text-black' },\n ],\n /* more options */\n }),\n ],\n}\n```\n\nthen in your web components:\n\n```ts\n// my-container-wc.ts\nconst template = document.createElement('template')\ntemplate.innerHTML = `\n<style>\n@unocss-placeholder\n</style>\n<my-wc-with-parts class=\"part-[cool-part]:cool-blue part-[another-cool-part]:cool-green\">...</my-wc-with-parts>\n`\n```\n\n```ts\n// my-wc-with-parts.ts\nconst template = document.createElement('template')\ntemplate.innerHTML = `\n<style>\n@unocss-placeholder\n</style>\n<div>\n <div part=\"cool-part\">...</div>\n <div part=\"another-cool-part\">...</div>\n</div>\n`\n```\n\n### Solid\n\n**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).\n\n```ts\n// vite.config.js\nimport solidPlugin from 'vite-plugin-solid'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n solidPlugin(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nYou have a `Vite + Solid` example project on [examples/vite-solid](https://github.com/unocss/unocss/tree/main/examples/vite-solid) directory.\n\n### Elm\n\nYou need to add the `vite-plugin-elm` plugin before UnoCSS's plugin.\n\n```ts\n// vite.config.js\nimport { defineConfig } from 'vite'\nimport elmPlugin from 'vite-plugin-elm'\nimport Unocss from 'unocss/vite'\n\nexport default defineConfig({\n plugins: [\n elmPlugin(),\n Unocss({\n /* options */\n }),\n ],\n})\n```\n\nYou have a `Vite + Elm` example project on [examples/vite-elm](https://github.com/unocss/unocss/tree/main/examples/vite-elm) directory.\n\n## License\n\nMIT License © 2021-PRESENT [Anthony Fu](https://github.com/antfu)\n"
|
|
60
|
+
"readme": "# @unocss/vite\n\nThe Vite plugin for UnoCSS. Ships with the `unocss` package.\n\n> 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/unocss/unocss/blob/main/packages/unocss/src/vite.ts) for an example to bind the default presets.\n\n## Installation\n\n```bash\nnpm i -D unocss\n```\n\n```ts\n// vite.config.ts\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({ /* options */ }),\n ],\n}\n```\n\nAdd `uno.css` to your main entry:\n\n```ts\n// main.ts\nimport 'uno.css'\n```\n\n### Presetless usage\n\n```bash\nnpm i -D @unocss/vite\n```\n\n```ts\n// vite.config.ts\nimport Unocss from '@unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n presets: [\n /* no presets by default */\n ],\n /* options */\n }),\n ],\n}\n```\n\n## Modes\n\nThe Vite plugin comes with a set of modes that enable different behaviors.\n\n###### `global` (default)\n\nThis is the default mode for the plugin: in this mode you need to add the import of `uno.css` on your entry point.\n\nThis mode enables a set of Vite plugins for `build` and for `dev` with `HMR` support.\n\nThe generated `css` will be a global stylesheet injected on the `index.html`.\n\n###### `vue-scoped`\n\nThis mode will inject generated CSS to Vue SFC's `<style scoped>` for isolation.\n\n###### `svelte-scoped`\n\nThis mode will inject generated CSS to Svelte's `<style>` for isolation.\n\n###### `shadow-dom`\n\nSince `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.\n\nTo 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.\n\n###### `per-module` (Experimental)\n\nThis mode will generate a CSS sheet for each module, can be scoped.\n\n###### `dist-chunk` (Experimental)\n\nThis mode will generate a CSS sheet for each code chunk on build, great for MPA.\n\n## \"Design in DevTools\"\n\nBecause 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.\n\n```ts\nimport 'uno.css'\nimport 'virtual:unocss-devtools'\n```\n\n> ⚠️ 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.\n\n`virtual:unocss-devtools` will be an empty bundle in production.\n\n## Frameworks\n\nSome 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.\n\n### React\n\n**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).\n\nIf you're using `@vitejs/plugin-react`:\n\n```ts\n// vite.config.js\nimport react from '@vitejs/plugin-react'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n react(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nor if you're using `@vitejs/plugin-react-refresh`:\n\n```ts\n// vite.config.js\nimport reactRefresh from '@vitejs/plugin-react-refresh'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n reactRefresh(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nIf you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.\n\nIf you are using `@vitejs/plugin-react` with `@unocss/preset-attributify`, you must add the plugin before `@vitejs/plugin-react`.\n\n```ts\n// vite.config.js\nimport react from '@vitejs/plugin-react'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n /* options */\n }),\n react(),\n ],\n}\n```\n\nYou 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.\n\n### Preact\n\nIf you're using `@preact/preset-vite`:\n\n```ts\n// vite.config.js\nimport preact from '@preact/preset-vite'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n preact(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nor if you're using `@prefresh/vite`:\n\n```ts\n// vite.config.js\nimport prefresh from '@prefresh/vite'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n prefresh(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nIf you're using `@unocss/preset-attributify` you should remove `tsc` from the `build` script.\n\nIf you are using `@preact/preset-vite` with `@unocss/preset-attributify`, you must add the plugin before `@preact/preset-vite`.\n\n```ts\n// vite.config.js\nimport preact from '@preact/preset-vite'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n /* options */\n }),\n preact(),\n ],\n}\n```\n\nYou 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.\n\n### Svelte\n\nYou must add the plugin before `@sveltejs/vite-plugin-svelte`.\n\nTo support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.\n\nYou 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.\n\n```ts\n// vite.config.js\nimport { svelte } from '@sveltejs/vite-plugin-svelte'\nimport Unocss from 'unocss/vite'\nimport { extractorSvelte } from '@unocss/core'\n\nexport default {\n plugins: [\n Unocss({\n extractors: [extractorSvelte],\n /* more options */\n }),\n svelte(),\n ],\n}\n```\n\nYou have a `Vite + Svelte` example project on [examples/vite-svelte](https://github.com/unocss/unocss/tree/main/examples/vite-svelte) directory.\n\n### Sveltekit\n\nTo support `class:foo` and `class:foo={bar}` add the plugin and configure `extractorSvelte` on `extractors` option.\n\nYou 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 below.\n\n```ts\n// svelte.config.js\nimport preprocess from 'svelte-preprocess'\nimport UnoCss from 'unocss/vite'\nimport { extractorSvelte } from '@unocss/core'\n\n/** @type {import('@sveltejs/kit').Config} */\nconst config = {\n // Consult https://github.com/sveltejs/svelte-preprocess\n // for more information about preprocessors\n preprocess: preprocess(),\n kit: {\n vite: {\n plugins: [\n UnoCss({\n extractors: [extractorSvelte],\n /* more options */\n }),\n ],\n },\n },\n}\n```\n\nYou have a `SvelteKit` example project on [examples/sveltekit](https://github.com/unocss/unocss/tree/main/examples/sveltekit) directory.\n\n### Web Components\n\nTo work with web components you need to enable `shadow-dom` mode on the plugin.\n\nDon't forget to remove the import for `uno.css` since the `shadow-dom` mode will not expose it and the application will not work.\n\n```ts\n// vite.config.js\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n mode: 'shadow-dom',\n /* more options */\n }),\n ],\n}\n```\n\nOn each `web component` just add `@unocss-placeholder` to its style css block:\n```ts\nconst template = document.createElement('template')\ntemplate.innerHTML = `\n<style>\n:host {...}\n@unocss-placeholder\n</style>\n<div class=\"m-1em\">\n...\n</div>\n`\n```\n\nIf you're using [Lit](https://lit.dev/):\n\n```ts\n@customElement('my-element')\nexport class MyElement extends LitElement {\n static styles = css`\n :host {...}\n @unocss-placeholder\n `\n // ...\n}\n```\n\nYou have a `Web Components` example project on [examples/vite-lit](https://github.com/unocss/unocss/tree/main/examples/vite-lit) directory.\n\n#### `::part` built-in support\n\nYou 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.\n\nThe `part-[<part-name>]:<rule|shortcut>` will work only with this plugin using the `shadow-dom` mode.\n\nThe 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.\n\n```ts\n// vite.config.js\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n Unocss({\n mode: 'shadow-dom',\n shortcuts: [\n { 'cool-blue': 'bg-blue-500 text-white' },\n { 'cool-green': 'bg-green-500 text-black' },\n ],\n /* more options */\n }),\n ],\n}\n```\n\nthen in your web components:\n\n```ts\n// my-container-wc.ts\nconst template = document.createElement('template')\ntemplate.innerHTML = `\n<style>\n@unocss-placeholder\n</style>\n<my-wc-with-parts class=\"part-[cool-part]:cool-blue part-[another-cool-part]:cool-green\">...</my-wc-with-parts>\n`\n```\n\n```ts\n// my-wc-with-parts.ts\nconst template = document.createElement('template')\ntemplate.innerHTML = `\n<style>\n@unocss-placeholder\n</style>\n<div>\n <div part=\"cool-part\">...</div>\n <div part=\"another-cool-part\">...</div>\n</div>\n`\n```\n\n### Solid\n\n**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).\n\n```ts\n// vite.config.js\nimport solidPlugin from 'vite-plugin-solid'\nimport Unocss from 'unocss/vite'\n\nexport default {\n plugins: [\n solidPlugin(),\n Unocss({\n /* options */\n }),\n ],\n}\n```\n\nYou have a `Vite + Solid` example project on [examples/vite-solid](https://github.com/unocss/unocss/tree/main/examples/vite-solid) directory.\n\n### Elm\n\nYou need to add the `vite-plugin-elm` plugin before UnoCSS's plugin.\n\n```ts\n// vite.config.js\nimport { defineConfig } from 'vite'\nimport elmPlugin from 'vite-plugin-elm'\nimport Unocss from 'unocss/vite'\n\nexport default defineConfig({\n plugins: [\n elmPlugin(),\n Unocss({\n /* options */\n }),\n ],\n})\n```\n\nYou have a `Vite + Elm` example project on [examples/vite-elm](https://github.com/unocss/unocss/tree/main/examples/vite-elm) directory.\n\n## License\n\nMIT License © 2021-PRESENT [Anthony Fu](https://github.com/antfu)\n"
|
|
61
61
|
}
|