@sepagian/sv-unocss 0.0.1
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 +69 -0
- package/dist/index.mjs +269 -0
- package/dist/sv-utils.mjs +38236 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# [sv](https://svelte.dev/docs/cli/overview) community add-on: [@sepagian/sv-unocss](https://github.com/sepagian/sv-unocss)
|
|
2
|
+
|
|
3
|
+
> [!IMPORTANT]
|
|
4
|
+
> Svelte maintainers have not reviewed community add-ons for malicious code. Use at your discretion
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
```shell
|
|
9
|
+
npx sv add @sepagian/sv-unocss
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Or with specific options:
|
|
13
|
+
|
|
14
|
+
```shell
|
|
15
|
+
npx sv add @sepagian/sv-unocss="presets:wind4,attributify,icons transformers:directives,variant-group extractors:svelte"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## What you get
|
|
19
|
+
|
|
20
|
+
- **UnoCSS Vite plugin** (`unocss/vite`) added to `vite.config.ts`
|
|
21
|
+
- **Svelte preprocessor** (`@unocss/svelte-scoped/preprocess`) added to `svelte.config.js` (SvelteKit only)
|
|
22
|
+
- **`uno.config.ts`** generated based on your selected presets, transformers, and extractors
|
|
23
|
+
- **`uno.css`** import in your root layout/component
|
|
24
|
+
- **pnpm build-allow** configured for `lightningcss` (if using pnpm)
|
|
25
|
+
|
|
26
|
+
## Options
|
|
27
|
+
|
|
28
|
+
All options are optional — pick what you need.
|
|
29
|
+
|
|
30
|
+
### `presets`
|
|
31
|
+
|
|
32
|
+
| Value | Description | Package |
|
|
33
|
+
|-------|-------------|---------|
|
|
34
|
+
| `wind4` | Tailwind4 compact preset | built-in |
|
|
35
|
+
| `wind3` | Tailwind/Windi CSS compact preset | built-in |
|
|
36
|
+
| `mini` | Minimal essential rules | built-in |
|
|
37
|
+
| `attributify` | Enable attributify mode | built-in |
|
|
38
|
+
| `icons` | Pure CSS icons via Iconify | built-in |
|
|
39
|
+
| `typography` | Typography utilities | built-in |
|
|
40
|
+
| `web-fonts` | Web fonts (Google Fonts, etc.) | built-in |
|
|
41
|
+
| `tagify` | Tagify mode | built-in |
|
|
42
|
+
| `rem-to-px` | Converts rem to px | `@unocss/preset-rem-to-px` |
|
|
43
|
+
| `legacy-compat` | Legacy browser compatibility | `@unocss/preset-legacy-compat` |
|
|
44
|
+
|
|
45
|
+
### `transformers`
|
|
46
|
+
|
|
47
|
+
| Value | Description | Package |
|
|
48
|
+
|-------|-------------|---------|
|
|
49
|
+
| `directives` | `@apply`, `@screen`, `theme()` support | built-in |
|
|
50
|
+
| `variant-group` | `hover:(bg-red text-white)` syntax | built-in |
|
|
51
|
+
| `compile-class` | `:uno:` compile class groups | built-in |
|
|
52
|
+
| `attributify-jsx` | Valueless attributify in JSX/TSX | built-in |
|
|
53
|
+
|
|
54
|
+
### `extractors`
|
|
55
|
+
|
|
56
|
+
| Value | Description | Package |
|
|
57
|
+
|-------|-------------|---------|
|
|
58
|
+
| `svelte` | Extract from `class:` directive | `@unocss/extractor-svelte` |
|
|
59
|
+
| `pug` | Extract from Pug templates | `@unocss/extractor-pug` |
|
|
60
|
+
| `mdc` | Extract from Markdown Components | `@unocss/extractor-mdc` |
|
|
61
|
+
|
|
62
|
+
## Requirements
|
|
63
|
+
|
|
64
|
+
- SvelteKit or Vite-based Svelte project
|
|
65
|
+
- `sv` CLI (peer dependency)
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { pnpm as pnpm_exports, transforms } from "./sv-utils.mjs";
|
|
2
|
+
import { defineAddon, defineAddonOptions } from "sv";
|
|
3
|
+
//#region src/index.js
|
|
4
|
+
const options = defineAddonOptions().add("presets", {
|
|
5
|
+
type: "multiselect",
|
|
6
|
+
question: "Which UnoCSS presets would you like to enable? (optional)",
|
|
7
|
+
options: [
|
|
8
|
+
{
|
|
9
|
+
value: "mini",
|
|
10
|
+
label: "Mini",
|
|
11
|
+
hint: "Minimal essential rules"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
value: "wind3",
|
|
15
|
+
label: "Wind3",
|
|
16
|
+
hint: "Tailwind/Windi CSS compact preset"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
value: "wind4",
|
|
20
|
+
label: "Wind4",
|
|
21
|
+
hint: "Tailwind4 compact preset"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
value: "attributify",
|
|
25
|
+
label: "Attributify",
|
|
26
|
+
hint: "Enable attributify mode"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
value: "tagify",
|
|
30
|
+
label: "Tagify",
|
|
31
|
+
hint: "Tagify mode"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
value: "icons",
|
|
35
|
+
label: "Icons",
|
|
36
|
+
hint: "Pure CSS icons via Iconify"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
value: "web-fonts",
|
|
40
|
+
label: "Web Fonts",
|
|
41
|
+
hint: "Web fonts (Google Fonts, etc.)"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
value: "typography",
|
|
45
|
+
label: "Typography",
|
|
46
|
+
hint: "Typography utilities"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
value: "rem-to-px",
|
|
50
|
+
label: "Rem to Px",
|
|
51
|
+
hint: "Converts rem to px"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
value: "legacy-compat",
|
|
55
|
+
label: "Legacy Compat",
|
|
56
|
+
hint: "Legacy browser compatibility"
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
default: [],
|
|
60
|
+
required: false
|
|
61
|
+
}).add("transformers", {
|
|
62
|
+
type: "multiselect",
|
|
63
|
+
question: "Which UnoCSS transformers would you like to enable? (optional)",
|
|
64
|
+
options: [
|
|
65
|
+
{
|
|
66
|
+
value: "variant-group",
|
|
67
|
+
label: "Variant Group",
|
|
68
|
+
hint: "hover:(bg-red text-white) syntax"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
value: "directives",
|
|
72
|
+
label: "Directives",
|
|
73
|
+
hint: "@apply, @screen, theme() support"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
value: "compile-class",
|
|
77
|
+
label: "Compile Class",
|
|
78
|
+
hint: ":uno: compile class groups"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
value: "attributify-jsx",
|
|
82
|
+
label: "Attributify JSX",
|
|
83
|
+
hint: "Valueless attributify in JSX/TSX"
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
default: [],
|
|
87
|
+
required: false
|
|
88
|
+
}).add("extractors", {
|
|
89
|
+
type: "multiselect",
|
|
90
|
+
question: "Which UnoCSS extractors would you like to enable? (optional)",
|
|
91
|
+
options: [
|
|
92
|
+
{
|
|
93
|
+
value: "pug",
|
|
94
|
+
label: "Pug",
|
|
95
|
+
hint: "Extract from Pug templates"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
value: "mdc",
|
|
99
|
+
label: "MDC",
|
|
100
|
+
hint: "Extract from Markdown Components"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
value: "svelte",
|
|
104
|
+
label: "Svelte",
|
|
105
|
+
hint: "Extract from class: directive"
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
default: [],
|
|
109
|
+
required: false
|
|
110
|
+
}).build();
|
|
111
|
+
const BUILTIN_IMPORTS = {
|
|
112
|
+
wind4: "presetWind4",
|
|
113
|
+
wind3: "presetWind3",
|
|
114
|
+
mini: "presetMini",
|
|
115
|
+
attributify: "presetAttributify",
|
|
116
|
+
icons: "presetIcons",
|
|
117
|
+
typography: "presetTypography",
|
|
118
|
+
"web-fonts": "presetWebFonts",
|
|
119
|
+
tagify: "presetTagify",
|
|
120
|
+
directives: "transformerDirectives",
|
|
121
|
+
"variant-group": "transformerVariantGroup",
|
|
122
|
+
"compile-class": "transformerCompileClass",
|
|
123
|
+
"attributify-jsx": "transformerAttributifyJsx"
|
|
124
|
+
};
|
|
125
|
+
const BUILTIN_CALLS = {
|
|
126
|
+
wind4: "presetWind4()",
|
|
127
|
+
wind3: "presetWind3()",
|
|
128
|
+
mini: "presetMini()",
|
|
129
|
+
attributify: "presetAttributify()",
|
|
130
|
+
icons: "presetIcons()",
|
|
131
|
+
typography: "presetTypography()",
|
|
132
|
+
"web-fonts": "presetWebFonts({ fonts: { /* ... */ } })",
|
|
133
|
+
tagify: "presetTagify()",
|
|
134
|
+
directives: "transformerDirectives()",
|
|
135
|
+
"variant-group": "transformerVariantGroup()",
|
|
136
|
+
"compile-class": "transformerCompileClass()",
|
|
137
|
+
"attributify-jsx": "transformerAttributifyJsx()"
|
|
138
|
+
};
|
|
139
|
+
const EXTERNAL_PACKAGES = {
|
|
140
|
+
"rem-to-px": {
|
|
141
|
+
pkg: "@unocss/preset-rem-to-px",
|
|
142
|
+
name: "presetRemToPx",
|
|
143
|
+
call: "presetRemToPx()"
|
|
144
|
+
},
|
|
145
|
+
"legacy-compat": {
|
|
146
|
+
pkg: "@unocss/preset-legacy-compat",
|
|
147
|
+
name: "presetLegacyCompat",
|
|
148
|
+
call: "presetLegacyCompat()"
|
|
149
|
+
},
|
|
150
|
+
svelte: {
|
|
151
|
+
pkg: "@unocss/extractor-svelte",
|
|
152
|
+
name: "extractorSvelte",
|
|
153
|
+
call: "extractorSvelte()"
|
|
154
|
+
},
|
|
155
|
+
pug: {
|
|
156
|
+
pkg: "@unocss/extractor-pug",
|
|
157
|
+
name: "extractorPug",
|
|
158
|
+
call: "extractorPug()"
|
|
159
|
+
},
|
|
160
|
+
mdc: {
|
|
161
|
+
pkg: "@unocss/extractor-mdc",
|
|
162
|
+
name: "extractorMdc",
|
|
163
|
+
call: "extractorMdc()"
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
function buildUnoConfig(selections) {
|
|
167
|
+
const presetKeys = selections.presets ?? [];
|
|
168
|
+
const transformerKeys = selections.transformers ?? [];
|
|
169
|
+
const extractorKeys = selections.extractors ?? [];
|
|
170
|
+
const unocssNamedImports = ["defineConfig"];
|
|
171
|
+
const externalImports = [];
|
|
172
|
+
const presetCalls = [];
|
|
173
|
+
const transformerCalls = [];
|
|
174
|
+
const extractorCalls = [];
|
|
175
|
+
for (const key of presetKeys) if (BUILTIN_IMPORTS[key]) {
|
|
176
|
+
unocssNamedImports.push(BUILTIN_IMPORTS[key]);
|
|
177
|
+
presetCalls.push(BUILTIN_CALLS[key]);
|
|
178
|
+
} else if (EXTERNAL_PACKAGES[key]) {
|
|
179
|
+
externalImports.push(`import ${EXTERNAL_PACKAGES[key].name} from '${EXTERNAL_PACKAGES[key].pkg}'`);
|
|
180
|
+
presetCalls.push(EXTERNAL_PACKAGES[key].call);
|
|
181
|
+
}
|
|
182
|
+
for (const key of transformerKeys) if (BUILTIN_IMPORTS[key]) {
|
|
183
|
+
unocssNamedImports.push(BUILTIN_IMPORTS[key]);
|
|
184
|
+
transformerCalls.push(BUILTIN_CALLS[key]);
|
|
185
|
+
}
|
|
186
|
+
for (const key of extractorKeys) if (EXTERNAL_PACKAGES[key]) {
|
|
187
|
+
externalImports.push(`import ${EXTERNAL_PACKAGES[key].name} from '${EXTERNAL_PACKAGES[key].pkg}'`);
|
|
188
|
+
extractorCalls.push(EXTERNAL_PACKAGES[key].call);
|
|
189
|
+
}
|
|
190
|
+
const lines = [];
|
|
191
|
+
if (unocssNamedImports.length > 1) lines.push(`import {\n\t${unocssNamedImports.join(",\n ")},\n} from 'unocss';\n`);
|
|
192
|
+
else lines.push("import { defineConfig } from 'unocss';\n");
|
|
193
|
+
for (const imp of externalImports) lines.push(`${imp}\n`);
|
|
194
|
+
lines.push("");
|
|
195
|
+
lines.push("export default defineConfig({");
|
|
196
|
+
if (presetCalls.length > 0) lines.push(`\tpresets: [\n\t\t${presetCalls.join(",\n ")},\n\t],`);
|
|
197
|
+
if (transformerCalls.length > 0) lines.push(`\ttransformers: [\n\t\t${transformerCalls.join(",\n ")},\n\t],`);
|
|
198
|
+
if (extractorCalls.length > 0) lines.push(`\textractors: [\n\t\t${extractorCalls.join(",\n ")},\n\t],`);
|
|
199
|
+
lines.push("});\n");
|
|
200
|
+
return lines.join("\n");
|
|
201
|
+
}
|
|
202
|
+
var src_default = defineAddon({
|
|
203
|
+
id: "@sepagian/sv-unocss",
|
|
204
|
+
shortDescription: "UnoCSS integration",
|
|
205
|
+
options,
|
|
206
|
+
run: ({ sv, options, file, isKit, directory, language, packageManager }) => {
|
|
207
|
+
sv.devDependency("unocss", "latest");
|
|
208
|
+
if (isKit) sv.devDependency("@unocss/svelte-scoped", "latest");
|
|
209
|
+
if (options.presets.includes("rem-to-px")) sv.devDependency("@unocss/preset-rem-to-px", "latest");
|
|
210
|
+
if (options.presets.includes("legacy-compat")) sv.devDependency("@unocss/preset-legacy-compat", "latest");
|
|
211
|
+
if (options.extractors.includes("svelte")) sv.devDependency("@unocss/extractor-svelte", "latest");
|
|
212
|
+
if (options.extractors.includes("pug")) sv.devDependency("@unocss/extractor-pug", "latest");
|
|
213
|
+
if (options.extractors.includes("mdc")) sv.devDependency("@unocss/extractor-mdc", "latest");
|
|
214
|
+
if (packageManager === "pnpm") sv.file(file.findUp("pnpm-workspace.yaml"), pnpm_exports.allowBuilds("lightningcss"));
|
|
215
|
+
sv.file("uno.config.ts", (content) => {
|
|
216
|
+
if (content.trim()) return content;
|
|
217
|
+
return buildUnoConfig(options);
|
|
218
|
+
});
|
|
219
|
+
sv.file(file.viteConfig, transforms.script(({ ast, js }) => {
|
|
220
|
+
js.imports.addDefault(ast, {
|
|
221
|
+
as: "UnoCSS",
|
|
222
|
+
from: "unocss/vite"
|
|
223
|
+
});
|
|
224
|
+
js.vite.addPlugin(ast, {
|
|
225
|
+
code: "UnoCSS()",
|
|
226
|
+
mode: "prepend"
|
|
227
|
+
});
|
|
228
|
+
}));
|
|
229
|
+
if (isKit) {
|
|
230
|
+
sv.file(file.svelteConfig, transforms.script(({ ast, js }) => {
|
|
231
|
+
js.imports.addDefault(ast, {
|
|
232
|
+
as: "UnoCSS",
|
|
233
|
+
from: "@unocss/svelte-scoped/preprocess"
|
|
234
|
+
});
|
|
235
|
+
const { value: exportDefault } = js.exports.createDefault(ast, { fallback: js.object.create({}) });
|
|
236
|
+
let preprocessorArray = js.object.property(exportDefault, {
|
|
237
|
+
name: "preprocess",
|
|
238
|
+
fallback: js.array.create()
|
|
239
|
+
});
|
|
240
|
+
if (preprocessorArray.type !== "ArrayExpression") {
|
|
241
|
+
const previousElement = preprocessorArray;
|
|
242
|
+
preprocessorArray = js.array.create();
|
|
243
|
+
js.array.append(preprocessorArray, previousElement);
|
|
244
|
+
js.object.overrideProperties(exportDefault, { preprocess: preprocessorArray });
|
|
245
|
+
}
|
|
246
|
+
const unocssCall = js.functions.createCall({
|
|
247
|
+
name: "UnoCSS",
|
|
248
|
+
args: []
|
|
249
|
+
});
|
|
250
|
+
unocssCall.arguments.push(js.object.create({
|
|
251
|
+
configOrPath: "./uno.config.ts",
|
|
252
|
+
exclude: ["./node_modules"]
|
|
253
|
+
}));
|
|
254
|
+
js.array.append(preprocessorArray, unocssCall);
|
|
255
|
+
}));
|
|
256
|
+
const layoutSvelte = `${directory.kitRoutes}/+layout.svelte`;
|
|
257
|
+
sv.file(layoutSvelte, transforms.svelteScript({ language }, ({ ast, js }) => {
|
|
258
|
+
js.imports.addEmpty(ast.instance.content, { from: "uno.css" });
|
|
259
|
+
}));
|
|
260
|
+
} else {
|
|
261
|
+
const appSvelte = `${directory.src}/App.svelte`;
|
|
262
|
+
sv.file(appSvelte, transforms.svelteScript({ language }, ({ ast, js }) => {
|
|
263
|
+
js.imports.addEmpty(ast.instance.content, { from: "uno.css" });
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
//#endregion
|
|
269
|
+
export { src_default as default };
|