@unocss/postcss 0.50.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/LICENSE +21 -0
- package/README.md +242 -0
- package/dist/index.cjs +266 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.mjs +258 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-PRESENT Anthony Fu <https://github.com/antfu>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# @unocss/postcss
|
|
2
|
+
|
|
3
|
+
## Experimental
|
|
4
|
+
This package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions.
|
|
5
|
+
|
|
6
|
+
<!-- @unocss-ignore -->
|
|
7
|
+
|
|
8
|
+
PostCSS plugin for UnoCSS. Supports `@apply`、`@screen` and `theme()` directives.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm i -D @unocss/postcss
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
// postcss.config.cjs
|
|
18
|
+
module.exports = {
|
|
19
|
+
plugins: {
|
|
20
|
+
'@unocss/postcss': {
|
|
21
|
+
// Optional
|
|
22
|
+
content: ['**/*.{html,js,ts,jsx,tsx,vue,svelte,astro}'],
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// uno.config.js
|
|
30
|
+
import { defineConfig, presetUno } from 'unocss'
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
presets: [
|
|
34
|
+
presetUno(),
|
|
35
|
+
],
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```css
|
|
40
|
+
/* style.css */
|
|
41
|
+
@unocss;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### `@unocss`
|
|
47
|
+
|
|
48
|
+
`@unocss` at-rule is a placeholder. It will be replaced by the generated CSS.
|
|
49
|
+
|
|
50
|
+
You can also inject each layer individually:
|
|
51
|
+
|
|
52
|
+
```css
|
|
53
|
+
/* style.css */
|
|
54
|
+
@unocss preflights;
|
|
55
|
+
@unocss default;
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
Fallback layer. It's always recommended to include.
|
|
59
|
+
Only unused layers will be injected here.
|
|
60
|
+
*/
|
|
61
|
+
@unocss;
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If you want to include all layers no matter whether they are previously included or not, you can use `@unocss all`. This is useful if you want to include generated CSS in multiple files.
|
|
65
|
+
|
|
66
|
+
```css
|
|
67
|
+
@unocss all;
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `@apply`
|
|
71
|
+
|
|
72
|
+
```css
|
|
73
|
+
.custom-div {
|
|
74
|
+
@apply text-center my-0 font-medium;
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Will be transformed to:
|
|
79
|
+
|
|
80
|
+
```css
|
|
81
|
+
.custom-div {
|
|
82
|
+
margin-top: 0rem;
|
|
83
|
+
margin-bottom: 0rem;
|
|
84
|
+
text-align: center;
|
|
85
|
+
font-weight: 500;
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### `@screen`
|
|
90
|
+
|
|
91
|
+
The `@screen` directive allows you to create media queries that reference your breakpoints by name comes from [`theme.breakpoints`](https://github.com/unocss/unocss/blob/main/README.md#extend-theme).
|
|
92
|
+
|
|
93
|
+
```css
|
|
94
|
+
.grid {
|
|
95
|
+
--uno: grid grid-cols-2;
|
|
96
|
+
}
|
|
97
|
+
@screen xs {
|
|
98
|
+
.grid {
|
|
99
|
+
--uno: grid-cols-1;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
@screen sm {
|
|
103
|
+
.grid {
|
|
104
|
+
--uno: grid-cols-3;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/* ... */
|
|
108
|
+
...
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Will be transformed to:
|
|
112
|
+
|
|
113
|
+
```css
|
|
114
|
+
.grid {
|
|
115
|
+
display: grid;
|
|
116
|
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
117
|
+
}
|
|
118
|
+
@media (min-width: 320px) {
|
|
119
|
+
.grid {
|
|
120
|
+
grid-template-columns: repeat(1, minmax(0, 1fr));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
@media (min-width: 640px) {
|
|
124
|
+
.grid {
|
|
125
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/* ... */
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### Breakpoint Variant Support
|
|
132
|
+
`@screen` also supports `lt`、`at` variants
|
|
133
|
+
|
|
134
|
+
##### `@screen lt`
|
|
135
|
+
|
|
136
|
+
```css
|
|
137
|
+
.grid {
|
|
138
|
+
--uno: grid grid-cols-2;
|
|
139
|
+
}
|
|
140
|
+
@screen lt-xs {
|
|
141
|
+
.grid {
|
|
142
|
+
--uno: grid-cols-1;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
@screen lt-sm {
|
|
146
|
+
.grid {
|
|
147
|
+
--uno: grid-cols-3;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/* ... */
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Will be transformed to:
|
|
154
|
+
|
|
155
|
+
```css
|
|
156
|
+
.grid {
|
|
157
|
+
display: grid;
|
|
158
|
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
159
|
+
}
|
|
160
|
+
@media (max-width: 319.9px) {
|
|
161
|
+
.grid {
|
|
162
|
+
grid-template-columns: repeat(1, minmax(0, 1fr));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
@media (max-width: 639.9px) {
|
|
166
|
+
.grid {
|
|
167
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/* ... */
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
##### `@screen at`
|
|
174
|
+
|
|
175
|
+
```css
|
|
176
|
+
.grid {
|
|
177
|
+
--uno: grid grid-cols-2;
|
|
178
|
+
}
|
|
179
|
+
@screen at-xs {
|
|
180
|
+
.grid {
|
|
181
|
+
--uno: grid-cols-1;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
@screen at-xl {
|
|
185
|
+
.grid {
|
|
186
|
+
--uno: grid-cols-3;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
@screen at-xxl {
|
|
190
|
+
.grid {
|
|
191
|
+
--uno: grid-cols-4;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/* ... */
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Will be transformed to:
|
|
198
|
+
|
|
199
|
+
```css
|
|
200
|
+
.grid {
|
|
201
|
+
display: grid;
|
|
202
|
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
203
|
+
}
|
|
204
|
+
@media (min-width: 320px) and (max-width: 639.9px) {
|
|
205
|
+
.grid {
|
|
206
|
+
grid-template-columns: repeat(1, minmax(0, 1fr));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
@media (min-width: 1280px) and (max-width: 1535.9px) {
|
|
210
|
+
.grid {
|
|
211
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
@media (min-width: 1536px) {
|
|
215
|
+
.grid {
|
|
216
|
+
grid-template-columns: repeat(4, minmax(0, 1fr));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/* ... */
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### `theme()`
|
|
223
|
+
|
|
224
|
+
Use the `theme()` function to access your theme config values using dot notation.
|
|
225
|
+
|
|
226
|
+
```css
|
|
227
|
+
.btn-blue {
|
|
228
|
+
background-color: theme('colors.blue.500');
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Will be compiled to:
|
|
233
|
+
|
|
234
|
+
```css
|
|
235
|
+
.btn-blue {
|
|
236
|
+
background-color: #3b82f6;
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## License
|
|
241
|
+
|
|
242
|
+
MIT License © 2022-PRESENT [hannoeru](https://github.com/hannoeru) [sibbng](https://github.com/sibbng)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const promises = require('fs/promises');
|
|
4
|
+
const fg = require('fast-glob');
|
|
5
|
+
const postcss = require('postcss');
|
|
6
|
+
const core = require('@unocss/core');
|
|
7
|
+
const config = require('@unocss/config');
|
|
8
|
+
const cssTree = require('css-tree');
|
|
9
|
+
const MagicString = require('magic-string');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
12
|
+
|
|
13
|
+
const fg__default = /*#__PURE__*/_interopDefaultLegacy(fg);
|
|
14
|
+
const postcss__default = /*#__PURE__*/_interopDefaultLegacy(postcss);
|
|
15
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
16
|
+
|
|
17
|
+
const defaultIncludeGlobs = ["**/*.{html,js,ts,jsx,tsx,vue,svelte,astro,elm,php,phtml,mdx,md}"];
|
|
18
|
+
|
|
19
|
+
function parseApply(root, uno, directiveName) {
|
|
20
|
+
root.walkAtRules(directiveName, async (rule) => {
|
|
21
|
+
if (!rule.parent)
|
|
22
|
+
return;
|
|
23
|
+
const source = rule.source;
|
|
24
|
+
const classNames = core.expandVariantGroup(rule.params).split(/\s+/g).map((className) => className.trim().replace(/\\/, ""));
|
|
25
|
+
const utils = (await Promise.all(
|
|
26
|
+
classNames.map((i) => uno.parseToken(i, "-"))
|
|
27
|
+
)).filter(core.notNull).flat().sort((a, b) => a[0] - b[0]).sort((a, b) => (a[3] ? uno.parentOrders.get(a[3]) ?? 0 : 0) - (b[3] ? uno.parentOrders.get(b[3]) ?? 0 : 0)).reduce((acc, item) => {
|
|
28
|
+
const target = acc.find((i) => i[1] === item[1] && i[3] === item[3]);
|
|
29
|
+
if (target)
|
|
30
|
+
target[2] += item[2];
|
|
31
|
+
else
|
|
32
|
+
acc.push([...item]);
|
|
33
|
+
return acc;
|
|
34
|
+
}, []);
|
|
35
|
+
if (!utils.length)
|
|
36
|
+
return;
|
|
37
|
+
for (const i of utils) {
|
|
38
|
+
const [, _selector, body, parent] = i;
|
|
39
|
+
const selector = _selector?.replace(core.regexScopePlaceholder, " ") || _selector;
|
|
40
|
+
if (parent || selector && selector !== ".\\-") {
|
|
41
|
+
const node = cssTree.parse(rule.parent.toString(), {
|
|
42
|
+
context: "rule"
|
|
43
|
+
});
|
|
44
|
+
let newSelector = cssTree.generate(node.prelude);
|
|
45
|
+
if (selector && selector !== ".\\-") {
|
|
46
|
+
const selectorAST = cssTree.parse(selector, {
|
|
47
|
+
context: "selector"
|
|
48
|
+
});
|
|
49
|
+
const prelude = cssTree.clone(node.prelude);
|
|
50
|
+
prelude.children.forEach((child) => {
|
|
51
|
+
const parentSelectorAst = cssTree.clone(selectorAST);
|
|
52
|
+
parentSelectorAst.children.forEach((i2) => {
|
|
53
|
+
if (i2.type === "ClassSelector" && i2.name === "\\-")
|
|
54
|
+
Object.assign(i2, cssTree.clone(child));
|
|
55
|
+
});
|
|
56
|
+
Object.assign(child, parentSelectorAst);
|
|
57
|
+
});
|
|
58
|
+
newSelector = cssTree.generate(prelude);
|
|
59
|
+
}
|
|
60
|
+
let css = `${newSelector}{${body}}`;
|
|
61
|
+
if (parent)
|
|
62
|
+
css = `${parent}{${css}}`;
|
|
63
|
+
const css_parsed = postcss__default.parse(css);
|
|
64
|
+
css_parsed.walkDecls((declaration) => {
|
|
65
|
+
declaration.source = source;
|
|
66
|
+
});
|
|
67
|
+
rule.parent.after(css_parsed);
|
|
68
|
+
} else {
|
|
69
|
+
const css = postcss__default.parse(body);
|
|
70
|
+
css.walkDecls((declaration) => {
|
|
71
|
+
declaration.source = source;
|
|
72
|
+
});
|
|
73
|
+
rule.parent.append(css);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
rule.remove();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseTheme(root, uno, directiveName) {
|
|
81
|
+
const themeFnRE = new RegExp(`${directiveName}\\((.*?)\\)`, "g");
|
|
82
|
+
root.walkDecls((decl) => {
|
|
83
|
+
const matches = Array.from(decl.value.matchAll(themeFnRE));
|
|
84
|
+
if (!matches.length)
|
|
85
|
+
return;
|
|
86
|
+
for (const match of matches) {
|
|
87
|
+
const rawArg = match[1].trim();
|
|
88
|
+
if (!rawArg)
|
|
89
|
+
throw new Error(`${directiveName}() expect exact one argument, but got 0`);
|
|
90
|
+
let value = uno.config.theme;
|
|
91
|
+
const keys = rawArg.slice(1, -1).split(".");
|
|
92
|
+
keys.every((key) => {
|
|
93
|
+
if (value[key] != null)
|
|
94
|
+
value = value[key];
|
|
95
|
+
else if (value[+key] != null)
|
|
96
|
+
value = value[+key];
|
|
97
|
+
else
|
|
98
|
+
return false;
|
|
99
|
+
return true;
|
|
100
|
+
});
|
|
101
|
+
if (typeof value === "string") {
|
|
102
|
+
const code = new MagicString__default(decl.value);
|
|
103
|
+
code.overwrite(
|
|
104
|
+
match.index,
|
|
105
|
+
match.index + match[0].length,
|
|
106
|
+
value
|
|
107
|
+
);
|
|
108
|
+
decl.value = code.toString();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function parseScreen(root, uno, directiveName) {
|
|
115
|
+
root.walkAtRules(directiveName, async (rule) => {
|
|
116
|
+
let breakpointName = "";
|
|
117
|
+
let prefix = "";
|
|
118
|
+
if (rule.params)
|
|
119
|
+
breakpointName = rule.params.trim();
|
|
120
|
+
if (!breakpointName)
|
|
121
|
+
return;
|
|
122
|
+
const match = breakpointName.match(/^(?:(lt|at)-)?(\w+)$/);
|
|
123
|
+
if (match) {
|
|
124
|
+
prefix = match[1];
|
|
125
|
+
breakpointName = match[2];
|
|
126
|
+
}
|
|
127
|
+
const resolveBreakpoints = () => {
|
|
128
|
+
let breakpoints;
|
|
129
|
+
if (uno.userConfig && uno.userConfig.theme)
|
|
130
|
+
breakpoints = uno.userConfig.theme.breakpoints;
|
|
131
|
+
if (!breakpoints)
|
|
132
|
+
breakpoints = uno.config.theme.breakpoints;
|
|
133
|
+
return breakpoints;
|
|
134
|
+
};
|
|
135
|
+
const variantEntries = Object.entries(resolveBreakpoints() ?? {}).map(([point, size], idx) => [point, size, idx]);
|
|
136
|
+
const generateMediaQuery = (breakpointName2, prefix2) => {
|
|
137
|
+
const [, size, idx] = variantEntries.find((i) => i[0] === breakpointName2);
|
|
138
|
+
if (prefix2) {
|
|
139
|
+
if (prefix2 === "lt")
|
|
140
|
+
return `(max-width: ${calcMaxWidthBySize(size)})`;
|
|
141
|
+
else if (prefix2 === "at")
|
|
142
|
+
return `(min-width: ${size})${variantEntries[idx + 1] ? ` and (max-width: ${calcMaxWidthBySize(variantEntries[idx + 1][1])})` : ""}`;
|
|
143
|
+
else
|
|
144
|
+
throw new Error(`breakpoint variant not supported: ${prefix2}`);
|
|
145
|
+
}
|
|
146
|
+
return `(min-width: ${size})`;
|
|
147
|
+
};
|
|
148
|
+
if (!variantEntries.find((i) => i[0] === breakpointName))
|
|
149
|
+
throw new Error(`breakpoint ${breakpointName} not found`);
|
|
150
|
+
rule.name = "media";
|
|
151
|
+
rule.params = `${generateMediaQuery(breakpointName, prefix)}`;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function calcMaxWidthBySize(size) {
|
|
155
|
+
const value = size.match(/^-?[0-9]+\.?[0-9]*/)?.[0] || "";
|
|
156
|
+
const unit = size.slice(value.length);
|
|
157
|
+
const maxWidth = parseFloat(value) - 0.1;
|
|
158
|
+
return Number.isNaN(maxWidth) ? size : `${maxWidth}${unit}`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function unocss({ content, directiveMap, cwd, configOrPath } = {
|
|
162
|
+
cwd: process.cwd()
|
|
163
|
+
}) {
|
|
164
|
+
const fileMap = /* @__PURE__ */ new Map();
|
|
165
|
+
const fileClassMap = /* @__PURE__ */ new Map();
|
|
166
|
+
const classes = /* @__PURE__ */ new Set();
|
|
167
|
+
const config$1 = config.loadConfig(cwd, configOrPath);
|
|
168
|
+
let uno;
|
|
169
|
+
core.warnOnce(
|
|
170
|
+
"`@unocss/postcss` package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions."
|
|
171
|
+
);
|
|
172
|
+
return {
|
|
173
|
+
postcssPlugin: "unocss",
|
|
174
|
+
plugins: [
|
|
175
|
+
async function(root, result) {
|
|
176
|
+
const cfg = await config$1;
|
|
177
|
+
if (!Object.keys(!cfg.config))
|
|
178
|
+
throw new Error("UnoCSS config file not found.");
|
|
179
|
+
if (!uno)
|
|
180
|
+
uno = core.createGenerator(cfg.config);
|
|
181
|
+
parseApply(root, uno, directiveMap?.apply || "apply");
|
|
182
|
+
parseTheme(root, uno, directiveMap?.theme || "theme");
|
|
183
|
+
parseScreen(root, uno, directiveMap?.screen || "screen");
|
|
184
|
+
const globs = content?.filter((v) => typeof v === "string") ?? defaultIncludeGlobs;
|
|
185
|
+
const rawContent = content?.filter((v) => typeof v === "object") ?? [];
|
|
186
|
+
const entries = await fg__default(globs, {
|
|
187
|
+
cwd,
|
|
188
|
+
dot: true,
|
|
189
|
+
absolute: true,
|
|
190
|
+
ignore: ["**/{.git,node_modules}/**"]
|
|
191
|
+
});
|
|
192
|
+
if (result.opts.from?.split("?")[0].endsWith(".css")) {
|
|
193
|
+
result.messages.push({
|
|
194
|
+
type: "dependency",
|
|
195
|
+
plugin: "unocss",
|
|
196
|
+
file: result.opts.from,
|
|
197
|
+
parent: result.opts.from
|
|
198
|
+
});
|
|
199
|
+
await Promise.all(
|
|
200
|
+
[
|
|
201
|
+
...rawContent.map(async (v) => {
|
|
202
|
+
const { matched } = await uno.generate(v.raw, {
|
|
203
|
+
id: `unocss${v.extension}`
|
|
204
|
+
});
|
|
205
|
+
for (const candidate of matched)
|
|
206
|
+
classes.add(candidate);
|
|
207
|
+
}),
|
|
208
|
+
...entries.map(async (file) => {
|
|
209
|
+
result.messages.push({
|
|
210
|
+
type: "dependency",
|
|
211
|
+
plugin: "unocss",
|
|
212
|
+
file,
|
|
213
|
+
parent: result.opts.from
|
|
214
|
+
});
|
|
215
|
+
const { mtimeMs } = await promises.stat(file);
|
|
216
|
+
if (fileMap.has(file) && mtimeMs <= fileMap.get(file))
|
|
217
|
+
return;
|
|
218
|
+
else
|
|
219
|
+
fileMap.set(file, mtimeMs);
|
|
220
|
+
const content2 = await promises.readFile(file, "utf8");
|
|
221
|
+
const { matched } = await uno.generate(content2, {
|
|
222
|
+
id: file
|
|
223
|
+
});
|
|
224
|
+
fileClassMap.set(file, matched);
|
|
225
|
+
})
|
|
226
|
+
]
|
|
227
|
+
);
|
|
228
|
+
for (const set of fileClassMap.values()) {
|
|
229
|
+
for (const candidate of set)
|
|
230
|
+
classes.add(candidate);
|
|
231
|
+
}
|
|
232
|
+
const c = await uno.generate(classes);
|
|
233
|
+
classes.clear();
|
|
234
|
+
const excludes = [];
|
|
235
|
+
root.walkAtRules("unocss", (rule) => {
|
|
236
|
+
if (rule.params) {
|
|
237
|
+
const source = rule.source;
|
|
238
|
+
const layers = rule.params.split(",").map((v) => v.trim());
|
|
239
|
+
const css = postcss__default.parse(
|
|
240
|
+
layers.map((i) => (i === "all" ? c.getLayers() : c.getLayer(i)) || "").filter(Boolean).join("\n")
|
|
241
|
+
);
|
|
242
|
+
css.walkDecls((declaration) => {
|
|
243
|
+
declaration.source = source;
|
|
244
|
+
});
|
|
245
|
+
rule.replaceWith(css);
|
|
246
|
+
excludes.push(rule.params);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
root.walkAtRules("unocss", (rule) => {
|
|
250
|
+
if (!rule.params) {
|
|
251
|
+
const source = rule.source;
|
|
252
|
+
const css = postcss__default.parse(c.getLayers(void 0, excludes) || "");
|
|
253
|
+
css.walkDecls((declaration) => {
|
|
254
|
+
declaration.source = source;
|
|
255
|
+
});
|
|
256
|
+
rule.replaceWith(css);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
]
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
unocss.postcss = true;
|
|
265
|
+
|
|
266
|
+
module.exports = unocss;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Root, Result } from 'postcss';
|
|
2
|
+
import { UserConfig } from '@unocss/core';
|
|
3
|
+
|
|
4
|
+
interface UnoPostcssPluginOptions {
|
|
5
|
+
content?: (string | {
|
|
6
|
+
raw: string;
|
|
7
|
+
extension: string;
|
|
8
|
+
})[];
|
|
9
|
+
directiveMap?: {
|
|
10
|
+
apply: string;
|
|
11
|
+
screen: string;
|
|
12
|
+
theme: string;
|
|
13
|
+
};
|
|
14
|
+
cwd?: string;
|
|
15
|
+
configOrPath?: string | UserConfig;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare function unocss({ content, directiveMap, cwd, configOrPath }?: UnoPostcssPluginOptions): {
|
|
19
|
+
postcssPlugin: string;
|
|
20
|
+
plugins: ((root: Root, result: Result) => Promise<void>)[];
|
|
21
|
+
};
|
|
22
|
+
declare namespace unocss {
|
|
23
|
+
var postcss: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { UnoPostcssPluginOptions, unocss as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { stat, readFile } from 'fs/promises';
|
|
2
|
+
import fg from 'fast-glob';
|
|
3
|
+
import postcss from 'postcss';
|
|
4
|
+
import { expandVariantGroup, notNull, regexScopePlaceholder, warnOnce, createGenerator } from '@unocss/core';
|
|
5
|
+
import { loadConfig } from '@unocss/config';
|
|
6
|
+
import { parse, generate, clone } from 'css-tree';
|
|
7
|
+
import MagicString from 'magic-string';
|
|
8
|
+
|
|
9
|
+
const defaultIncludeGlobs = ["**/*.{html,js,ts,jsx,tsx,vue,svelte,astro,elm,php,phtml,mdx,md}"];
|
|
10
|
+
|
|
11
|
+
function parseApply(root, uno, directiveName) {
|
|
12
|
+
root.walkAtRules(directiveName, async (rule) => {
|
|
13
|
+
if (!rule.parent)
|
|
14
|
+
return;
|
|
15
|
+
const source = rule.source;
|
|
16
|
+
const classNames = expandVariantGroup(rule.params).split(/\s+/g).map((className) => className.trim().replace(/\\/, ""));
|
|
17
|
+
const utils = (await Promise.all(
|
|
18
|
+
classNames.map((i) => uno.parseToken(i, "-"))
|
|
19
|
+
)).filter(notNull).flat().sort((a, b) => a[0] - b[0]).sort((a, b) => (a[3] ? uno.parentOrders.get(a[3]) ?? 0 : 0) - (b[3] ? uno.parentOrders.get(b[3]) ?? 0 : 0)).reduce((acc, item) => {
|
|
20
|
+
const target = acc.find((i) => i[1] === item[1] && i[3] === item[3]);
|
|
21
|
+
if (target)
|
|
22
|
+
target[2] += item[2];
|
|
23
|
+
else
|
|
24
|
+
acc.push([...item]);
|
|
25
|
+
return acc;
|
|
26
|
+
}, []);
|
|
27
|
+
if (!utils.length)
|
|
28
|
+
return;
|
|
29
|
+
for (const i of utils) {
|
|
30
|
+
const [, _selector, body, parent] = i;
|
|
31
|
+
const selector = _selector?.replace(regexScopePlaceholder, " ") || _selector;
|
|
32
|
+
if (parent || selector && selector !== ".\\-") {
|
|
33
|
+
const node = parse(rule.parent.toString(), {
|
|
34
|
+
context: "rule"
|
|
35
|
+
});
|
|
36
|
+
let newSelector = generate(node.prelude);
|
|
37
|
+
if (selector && selector !== ".\\-") {
|
|
38
|
+
const selectorAST = parse(selector, {
|
|
39
|
+
context: "selector"
|
|
40
|
+
});
|
|
41
|
+
const prelude = clone(node.prelude);
|
|
42
|
+
prelude.children.forEach((child) => {
|
|
43
|
+
const parentSelectorAst = clone(selectorAST);
|
|
44
|
+
parentSelectorAst.children.forEach((i2) => {
|
|
45
|
+
if (i2.type === "ClassSelector" && i2.name === "\\-")
|
|
46
|
+
Object.assign(i2, clone(child));
|
|
47
|
+
});
|
|
48
|
+
Object.assign(child, parentSelectorAst);
|
|
49
|
+
});
|
|
50
|
+
newSelector = generate(prelude);
|
|
51
|
+
}
|
|
52
|
+
let css = `${newSelector}{${body}}`;
|
|
53
|
+
if (parent)
|
|
54
|
+
css = `${parent}{${css}}`;
|
|
55
|
+
const css_parsed = postcss.parse(css);
|
|
56
|
+
css_parsed.walkDecls((declaration) => {
|
|
57
|
+
declaration.source = source;
|
|
58
|
+
});
|
|
59
|
+
rule.parent.after(css_parsed);
|
|
60
|
+
} else {
|
|
61
|
+
const css = postcss.parse(body);
|
|
62
|
+
css.walkDecls((declaration) => {
|
|
63
|
+
declaration.source = source;
|
|
64
|
+
});
|
|
65
|
+
rule.parent.append(css);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
rule.remove();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function parseTheme(root, uno, directiveName) {
|
|
73
|
+
const themeFnRE = new RegExp(`${directiveName}\\((.*?)\\)`, "g");
|
|
74
|
+
root.walkDecls((decl) => {
|
|
75
|
+
const matches = Array.from(decl.value.matchAll(themeFnRE));
|
|
76
|
+
if (!matches.length)
|
|
77
|
+
return;
|
|
78
|
+
for (const match of matches) {
|
|
79
|
+
const rawArg = match[1].trim();
|
|
80
|
+
if (!rawArg)
|
|
81
|
+
throw new Error(`${directiveName}() expect exact one argument, but got 0`);
|
|
82
|
+
let value = uno.config.theme;
|
|
83
|
+
const keys = rawArg.slice(1, -1).split(".");
|
|
84
|
+
keys.every((key) => {
|
|
85
|
+
if (value[key] != null)
|
|
86
|
+
value = value[key];
|
|
87
|
+
else if (value[+key] != null)
|
|
88
|
+
value = value[+key];
|
|
89
|
+
else
|
|
90
|
+
return false;
|
|
91
|
+
return true;
|
|
92
|
+
});
|
|
93
|
+
if (typeof value === "string") {
|
|
94
|
+
const code = new MagicString(decl.value);
|
|
95
|
+
code.overwrite(
|
|
96
|
+
match.index,
|
|
97
|
+
match.index + match[0].length,
|
|
98
|
+
value
|
|
99
|
+
);
|
|
100
|
+
decl.value = code.toString();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function parseScreen(root, uno, directiveName) {
|
|
107
|
+
root.walkAtRules(directiveName, async (rule) => {
|
|
108
|
+
let breakpointName = "";
|
|
109
|
+
let prefix = "";
|
|
110
|
+
if (rule.params)
|
|
111
|
+
breakpointName = rule.params.trim();
|
|
112
|
+
if (!breakpointName)
|
|
113
|
+
return;
|
|
114
|
+
const match = breakpointName.match(/^(?:(lt|at)-)?(\w+)$/);
|
|
115
|
+
if (match) {
|
|
116
|
+
prefix = match[1];
|
|
117
|
+
breakpointName = match[2];
|
|
118
|
+
}
|
|
119
|
+
const resolveBreakpoints = () => {
|
|
120
|
+
let breakpoints;
|
|
121
|
+
if (uno.userConfig && uno.userConfig.theme)
|
|
122
|
+
breakpoints = uno.userConfig.theme.breakpoints;
|
|
123
|
+
if (!breakpoints)
|
|
124
|
+
breakpoints = uno.config.theme.breakpoints;
|
|
125
|
+
return breakpoints;
|
|
126
|
+
};
|
|
127
|
+
const variantEntries = Object.entries(resolveBreakpoints() ?? {}).map(([point, size], idx) => [point, size, idx]);
|
|
128
|
+
const generateMediaQuery = (breakpointName2, prefix2) => {
|
|
129
|
+
const [, size, idx] = variantEntries.find((i) => i[0] === breakpointName2);
|
|
130
|
+
if (prefix2) {
|
|
131
|
+
if (prefix2 === "lt")
|
|
132
|
+
return `(max-width: ${calcMaxWidthBySize(size)})`;
|
|
133
|
+
else if (prefix2 === "at")
|
|
134
|
+
return `(min-width: ${size})${variantEntries[idx + 1] ? ` and (max-width: ${calcMaxWidthBySize(variantEntries[idx + 1][1])})` : ""}`;
|
|
135
|
+
else
|
|
136
|
+
throw new Error(`breakpoint variant not supported: ${prefix2}`);
|
|
137
|
+
}
|
|
138
|
+
return `(min-width: ${size})`;
|
|
139
|
+
};
|
|
140
|
+
if (!variantEntries.find((i) => i[0] === breakpointName))
|
|
141
|
+
throw new Error(`breakpoint ${breakpointName} not found`);
|
|
142
|
+
rule.name = "media";
|
|
143
|
+
rule.params = `${generateMediaQuery(breakpointName, prefix)}`;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function calcMaxWidthBySize(size) {
|
|
147
|
+
const value = size.match(/^-?[0-9]+\.?[0-9]*/)?.[0] || "";
|
|
148
|
+
const unit = size.slice(value.length);
|
|
149
|
+
const maxWidth = parseFloat(value) - 0.1;
|
|
150
|
+
return Number.isNaN(maxWidth) ? size : `${maxWidth}${unit}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function unocss({ content, directiveMap, cwd, configOrPath } = {
|
|
154
|
+
cwd: process.cwd()
|
|
155
|
+
}) {
|
|
156
|
+
const fileMap = /* @__PURE__ */ new Map();
|
|
157
|
+
const fileClassMap = /* @__PURE__ */ new Map();
|
|
158
|
+
const classes = /* @__PURE__ */ new Set();
|
|
159
|
+
const config = loadConfig(cwd, configOrPath);
|
|
160
|
+
let uno;
|
|
161
|
+
warnOnce(
|
|
162
|
+
"`@unocss/postcss` package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions."
|
|
163
|
+
);
|
|
164
|
+
return {
|
|
165
|
+
postcssPlugin: "unocss",
|
|
166
|
+
plugins: [
|
|
167
|
+
async function(root, result) {
|
|
168
|
+
const cfg = await config;
|
|
169
|
+
if (!Object.keys(!cfg.config))
|
|
170
|
+
throw new Error("UnoCSS config file not found.");
|
|
171
|
+
if (!uno)
|
|
172
|
+
uno = createGenerator(cfg.config);
|
|
173
|
+
parseApply(root, uno, directiveMap?.apply || "apply");
|
|
174
|
+
parseTheme(root, uno, directiveMap?.theme || "theme");
|
|
175
|
+
parseScreen(root, uno, directiveMap?.screen || "screen");
|
|
176
|
+
const globs = content?.filter((v) => typeof v === "string") ?? defaultIncludeGlobs;
|
|
177
|
+
const rawContent = content?.filter((v) => typeof v === "object") ?? [];
|
|
178
|
+
const entries = await fg(globs, {
|
|
179
|
+
cwd,
|
|
180
|
+
dot: true,
|
|
181
|
+
absolute: true,
|
|
182
|
+
ignore: ["**/{.git,node_modules}/**"]
|
|
183
|
+
});
|
|
184
|
+
if (result.opts.from?.split("?")[0].endsWith(".css")) {
|
|
185
|
+
result.messages.push({
|
|
186
|
+
type: "dependency",
|
|
187
|
+
plugin: "unocss",
|
|
188
|
+
file: result.opts.from,
|
|
189
|
+
parent: result.opts.from
|
|
190
|
+
});
|
|
191
|
+
await Promise.all(
|
|
192
|
+
[
|
|
193
|
+
...rawContent.map(async (v) => {
|
|
194
|
+
const { matched } = await uno.generate(v.raw, {
|
|
195
|
+
id: `unocss${v.extension}`
|
|
196
|
+
});
|
|
197
|
+
for (const candidate of matched)
|
|
198
|
+
classes.add(candidate);
|
|
199
|
+
}),
|
|
200
|
+
...entries.map(async (file) => {
|
|
201
|
+
result.messages.push({
|
|
202
|
+
type: "dependency",
|
|
203
|
+
plugin: "unocss",
|
|
204
|
+
file,
|
|
205
|
+
parent: result.opts.from
|
|
206
|
+
});
|
|
207
|
+
const { mtimeMs } = await stat(file);
|
|
208
|
+
if (fileMap.has(file) && mtimeMs <= fileMap.get(file))
|
|
209
|
+
return;
|
|
210
|
+
else
|
|
211
|
+
fileMap.set(file, mtimeMs);
|
|
212
|
+
const content2 = await readFile(file, "utf8");
|
|
213
|
+
const { matched } = await uno.generate(content2, {
|
|
214
|
+
id: file
|
|
215
|
+
});
|
|
216
|
+
fileClassMap.set(file, matched);
|
|
217
|
+
})
|
|
218
|
+
]
|
|
219
|
+
);
|
|
220
|
+
for (const set of fileClassMap.values()) {
|
|
221
|
+
for (const candidate of set)
|
|
222
|
+
classes.add(candidate);
|
|
223
|
+
}
|
|
224
|
+
const c = await uno.generate(classes);
|
|
225
|
+
classes.clear();
|
|
226
|
+
const excludes = [];
|
|
227
|
+
root.walkAtRules("unocss", (rule) => {
|
|
228
|
+
if (rule.params) {
|
|
229
|
+
const source = rule.source;
|
|
230
|
+
const layers = rule.params.split(",").map((v) => v.trim());
|
|
231
|
+
const css = postcss.parse(
|
|
232
|
+
layers.map((i) => (i === "all" ? c.getLayers() : c.getLayer(i)) || "").filter(Boolean).join("\n")
|
|
233
|
+
);
|
|
234
|
+
css.walkDecls((declaration) => {
|
|
235
|
+
declaration.source = source;
|
|
236
|
+
});
|
|
237
|
+
rule.replaceWith(css);
|
|
238
|
+
excludes.push(rule.params);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
root.walkAtRules("unocss", (rule) => {
|
|
242
|
+
if (!rule.params) {
|
|
243
|
+
const source = rule.source;
|
|
244
|
+
const css = postcss.parse(c.getLayers(void 0, excludes) || "");
|
|
245
|
+
css.walkDecls((declaration) => {
|
|
246
|
+
declaration.source = source;
|
|
247
|
+
});
|
|
248
|
+
rule.replaceWith(css);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
]
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
unocss.postcss = true;
|
|
257
|
+
|
|
258
|
+
export { unocss as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unocss/postcss",
|
|
3
|
+
"version": "0.50.0",
|
|
4
|
+
"description": "PostCSS plugin for UnoCSS",
|
|
5
|
+
"author": "sibbng <sibbngheid@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"funding": "https://github.com/sponsors/antfu",
|
|
8
|
+
"homepage": "https://github.com/unocss/unocss/tree/main/packages/postcss#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/unocss/unocss.git",
|
|
12
|
+
"directory": "packages/postcss"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/unocss/unocss/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"require": "./dist/index.cjs",
|
|
23
|
+
"import": "./dist/index.mjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"main": "dist/index.cjs",
|
|
27
|
+
"module": "dist/index.mjs",
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=14"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"postcss": "^8.4.21"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@unocss/config": "0.50.0",
|
|
40
|
+
"@unocss/core": "0.50.0",
|
|
41
|
+
"css-tree": "^2.3.1",
|
|
42
|
+
"fast-glob": "^3.2.12",
|
|
43
|
+
"magic-string": "^0.27.0",
|
|
44
|
+
"postcss": "^8.4.21"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "unbuild",
|
|
48
|
+
"stub": "unbuild --stub"
|
|
49
|
+
}
|
|
50
|
+
}
|