@unocss/core 0.1.0-beta.2
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 +18 -0
- package/dist/index.d.ts +219 -0
- package/dist/index.js +698 -0
- package/dist/index.mjs +661 -0
- package/package.json +34 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
21
|
+
var __export = (target, all) => {
|
|
22
|
+
__markAsModule(target);
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/types.ts
|
|
28
|
+
function defineConfig(config) {
|
|
29
|
+
return config;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/utils/escape.ts
|
|
33
|
+
function escapeRegExp(string) {
|
|
34
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
35
|
+
}
|
|
36
|
+
function escapeSelector(str) {
|
|
37
|
+
const length = str.length;
|
|
38
|
+
let index = -1;
|
|
39
|
+
let codeUnit;
|
|
40
|
+
let result = "";
|
|
41
|
+
const firstCodeUnit = str.charCodeAt(0);
|
|
42
|
+
while (++index < length) {
|
|
43
|
+
codeUnit = str.charCodeAt(index);
|
|
44
|
+
if (codeUnit === 0) {
|
|
45
|
+
result += "\uFFFD";
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (codeUnit === 44) {
|
|
49
|
+
result += "\\2c ";
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (codeUnit >= 1 && codeUnit <= 31 || codeUnit === 127 || index === 0 && codeUnit >= 48 && codeUnit <= 57 || index === 1 && codeUnit >= 48 && codeUnit <= 57 && firstCodeUnit === 45) {
|
|
53
|
+
result += `\\${codeUnit.toString(16)} `;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (index === 0 && length === 1 && codeUnit === 45) {
|
|
57
|
+
result += `\\${str.charAt(index)}`;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (codeUnit >= 128 || codeUnit === 45 || codeUnit === 95 || codeUnit >= 48 && codeUnit <= 57 || codeUnit >= 65 && codeUnit <= 90 || codeUnit >= 97 && codeUnit <= 122) {
|
|
61
|
+
result += str.charAt(index);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
result += `\\${str.charAt(index)}`;
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
var e = escapeSelector;
|
|
69
|
+
|
|
70
|
+
// src/utils/object.ts
|
|
71
|
+
function entriesToCss(arr) {
|
|
72
|
+
if (arr == null)
|
|
73
|
+
return "";
|
|
74
|
+
return arr.map(([key, value]) => value != null ? `${key}:${value};` : void 0).filter(Boolean).join("");
|
|
75
|
+
}
|
|
76
|
+
function isObject(item) {
|
|
77
|
+
return item && typeof item === "object" && !Array.isArray(item);
|
|
78
|
+
}
|
|
79
|
+
function mergeDeep(original, patch) {
|
|
80
|
+
const o = original;
|
|
81
|
+
const p = patch;
|
|
82
|
+
if (Array.isArray(o) && Array.isArray(p))
|
|
83
|
+
return [...o, ...p];
|
|
84
|
+
if (Array.isArray(o))
|
|
85
|
+
return [...o];
|
|
86
|
+
const output = __spreadValues({}, o);
|
|
87
|
+
if (isObject(o) && isObject(p)) {
|
|
88
|
+
Object.keys(p).forEach((key) => {
|
|
89
|
+
if (isObject(p[key])) {
|
|
90
|
+
if (!(key in o))
|
|
91
|
+
Object.assign(output, { [key]: p[key] });
|
|
92
|
+
else
|
|
93
|
+
output[key] = mergeDeep(o[key], p[key]);
|
|
94
|
+
} else {
|
|
95
|
+
Object.assign(output, { [key]: p[key] });
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return output;
|
|
100
|
+
}
|
|
101
|
+
function isStaticRule(rule) {
|
|
102
|
+
return typeof rule[0] === "string";
|
|
103
|
+
}
|
|
104
|
+
function isStaticShortcut(sc) {
|
|
105
|
+
return typeof sc[0] === "string";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/utils/basic.ts
|
|
109
|
+
function toArray(value = []) {
|
|
110
|
+
return Array.isArray(value) ? value : [value];
|
|
111
|
+
}
|
|
112
|
+
function uniq(value) {
|
|
113
|
+
return Array.from(new Set(value));
|
|
114
|
+
}
|
|
115
|
+
function mergeSet(target, append) {
|
|
116
|
+
append.forEach((i) => target.add(i));
|
|
117
|
+
return target;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/utils/variant.ts
|
|
121
|
+
var variantMatcher = (name) => {
|
|
122
|
+
const length = name.length + 1;
|
|
123
|
+
const re = new RegExp(`^${name}[:-]`);
|
|
124
|
+
return (input) => input.match(re) ? input.slice(length) : void 0;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/utils/handlers/handlers.ts
|
|
128
|
+
var handlers_exports = {};
|
|
129
|
+
__export(handlers_exports, {
|
|
130
|
+
border: () => border,
|
|
131
|
+
bracket: () => bracket,
|
|
132
|
+
fraction: () => fraction,
|
|
133
|
+
number: () => number,
|
|
134
|
+
percent: () => percent,
|
|
135
|
+
size: () => size
|
|
136
|
+
});
|
|
137
|
+
var numberWithUnitRE = /^(-?[0-9.]+)([a-z]*)$/i;
|
|
138
|
+
var numberRE = /^(-?[0-9.]+)$/i;
|
|
139
|
+
function size(str) {
|
|
140
|
+
if (str === "auto" || str === "a")
|
|
141
|
+
return "auto";
|
|
142
|
+
const match = str.match(numberWithUnitRE);
|
|
143
|
+
if (!match)
|
|
144
|
+
return;
|
|
145
|
+
const [, n, unit] = match;
|
|
146
|
+
if (unit)
|
|
147
|
+
return str;
|
|
148
|
+
const num = parseFloat(n);
|
|
149
|
+
if (!Number.isNaN(num))
|
|
150
|
+
return `${num / 4}rem`;
|
|
151
|
+
}
|
|
152
|
+
function border(str) {
|
|
153
|
+
const match = str.match(numberWithUnitRE);
|
|
154
|
+
if (!match)
|
|
155
|
+
return;
|
|
156
|
+
const [, n, unit] = match;
|
|
157
|
+
if (unit)
|
|
158
|
+
return str;
|
|
159
|
+
const num = parseFloat(n);
|
|
160
|
+
if (!Number.isNaN(num))
|
|
161
|
+
return `${num}px`;
|
|
162
|
+
}
|
|
163
|
+
function number(str) {
|
|
164
|
+
if (!numberRE.test(str))
|
|
165
|
+
return;
|
|
166
|
+
const num = parseFloat(str);
|
|
167
|
+
if (!Number.isNaN(num))
|
|
168
|
+
return num;
|
|
169
|
+
}
|
|
170
|
+
function percent(str) {
|
|
171
|
+
if (str.endsWith("%"))
|
|
172
|
+
str = str.slice(0, -1);
|
|
173
|
+
const num = parseFloat(str);
|
|
174
|
+
if (!Number.isNaN(num))
|
|
175
|
+
return `${num / 100}`;
|
|
176
|
+
}
|
|
177
|
+
function fraction(str) {
|
|
178
|
+
const [left, right] = str.split("/");
|
|
179
|
+
const num = parseFloat(left) / parseFloat(right);
|
|
180
|
+
if (!Number.isNaN(num))
|
|
181
|
+
return `${num * 100}%`;
|
|
182
|
+
}
|
|
183
|
+
function bracket(str) {
|
|
184
|
+
if (str[0] === "[" && str[str.length - 1] === "]")
|
|
185
|
+
return str.slice(1, -1);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/utils/handlers/shorthand.ts
|
|
189
|
+
var handlersNames = Object.keys(handlers_exports);
|
|
190
|
+
var handler = function(str) {
|
|
191
|
+
var _a;
|
|
192
|
+
const s = ((_a = this.__options) == null ? void 0 : _a.sequence) || [];
|
|
193
|
+
this.__options.sequence = [];
|
|
194
|
+
for (const n of s) {
|
|
195
|
+
const res = handlers_exports[n](str);
|
|
196
|
+
if (res)
|
|
197
|
+
return res;
|
|
198
|
+
}
|
|
199
|
+
return void 0;
|
|
200
|
+
};
|
|
201
|
+
function addProcessor(that, name) {
|
|
202
|
+
if (!that.__options) {
|
|
203
|
+
that.__options = {
|
|
204
|
+
sequence: []
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
that.__options.sequence.push(name);
|
|
208
|
+
return that;
|
|
209
|
+
}
|
|
210
|
+
handlersNames.forEach((i) => {
|
|
211
|
+
Object.defineProperty(handler, i, {
|
|
212
|
+
enumerable: true,
|
|
213
|
+
get() {
|
|
214
|
+
return addProcessor(this, i);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// src/utils/colors.ts
|
|
220
|
+
var hexRE = /^#?([\da-f]+)$/i;
|
|
221
|
+
function hex2rgba(hex) {
|
|
222
|
+
const [, body] = hex.match(hexRE) || [];
|
|
223
|
+
if (!body)
|
|
224
|
+
return;
|
|
225
|
+
switch (body.length) {
|
|
226
|
+
case 3:
|
|
227
|
+
case 4:
|
|
228
|
+
const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
|
|
229
|
+
if (body.length === 3)
|
|
230
|
+
return digits;
|
|
231
|
+
digits[3] = Math.round(digits[3] / 255 * 100) / 100;
|
|
232
|
+
return digits;
|
|
233
|
+
case 6:
|
|
234
|
+
case 8:
|
|
235
|
+
const value = Number.parseInt(body, 16);
|
|
236
|
+
if (body.length === 6)
|
|
237
|
+
return [value >> 16 & 255, value >> 8 & 255, value & 255];
|
|
238
|
+
return [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255, Math.round((value & 255) / 255 * 100) / 100];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/utils/playground.ts
|
|
243
|
+
function getMatchedPositions(code, matched) {
|
|
244
|
+
const result = [];
|
|
245
|
+
const attributify = [];
|
|
246
|
+
const plain = new Set();
|
|
247
|
+
Array.from(matched).forEach((v) => {
|
|
248
|
+
const match = isAttributifySelector(v);
|
|
249
|
+
if (!match)
|
|
250
|
+
plain.add(v);
|
|
251
|
+
else if (!match[2])
|
|
252
|
+
plain.add(match[1]);
|
|
253
|
+
else
|
|
254
|
+
attributify.push(match);
|
|
255
|
+
});
|
|
256
|
+
let start = 0;
|
|
257
|
+
code.split(/[\s"';<>]/g).forEach((i) => {
|
|
258
|
+
const end = start + i.length;
|
|
259
|
+
if (plain.has(i))
|
|
260
|
+
result.push([start, end]);
|
|
261
|
+
start = end + 1;
|
|
262
|
+
});
|
|
263
|
+
attributify.forEach(([, name, value]) => {
|
|
264
|
+
const regex = new RegExp(`${escapeRegExp(name)}=(['"])[^\\1]*?${escapeRegExp(value)}[^\\1]*?\\1`, "g");
|
|
265
|
+
Array.from(code.matchAll(regex)).forEach((match) => {
|
|
266
|
+
const start2 = match.index + match[0].indexOf(value);
|
|
267
|
+
const end = start2 + value.length;
|
|
268
|
+
result.push([start2, end]);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/utils/regex.ts
|
|
275
|
+
var attributifyRE = /^\[(.+?)~?="(.*)"\]$/;
|
|
276
|
+
var validateFilterRE = /[a-z]/;
|
|
277
|
+
function isAttributifySelector(selector) {
|
|
278
|
+
return selector.match(attributifyRE);
|
|
279
|
+
}
|
|
280
|
+
function isValidSelector(selector = "") {
|
|
281
|
+
return validateFilterRE.test(selector);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// src/utils/map.ts
|
|
285
|
+
var TwoKeyMap = class {
|
|
286
|
+
constructor() {
|
|
287
|
+
this._map = new Map();
|
|
288
|
+
}
|
|
289
|
+
get(key1, key2) {
|
|
290
|
+
const m2 = this._map.get(key1);
|
|
291
|
+
if (m2)
|
|
292
|
+
return m2.get(key2);
|
|
293
|
+
}
|
|
294
|
+
getFallback(key1, key2, fallback) {
|
|
295
|
+
let m2 = this._map.get(key1);
|
|
296
|
+
if (!m2) {
|
|
297
|
+
m2 = new Map();
|
|
298
|
+
this._map.set(key1, m2);
|
|
299
|
+
}
|
|
300
|
+
if (!m2.has(key2))
|
|
301
|
+
m2.set(key2, fallback);
|
|
302
|
+
return m2.get(key2);
|
|
303
|
+
}
|
|
304
|
+
set(key1, key2, value) {
|
|
305
|
+
let m2 = this._map.get(key1);
|
|
306
|
+
if (!m2) {
|
|
307
|
+
m2 = new Map();
|
|
308
|
+
this._map.set(key1, m2);
|
|
309
|
+
}
|
|
310
|
+
m2.set(key2, value);
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
has(key1, key2) {
|
|
314
|
+
var _a;
|
|
315
|
+
return (_a = this._map.get(key1)) == null ? void 0 : _a.has(key2);
|
|
316
|
+
}
|
|
317
|
+
delete(key1, key2) {
|
|
318
|
+
var _a;
|
|
319
|
+
return ((_a = this._map.get(key1)) == null ? void 0 : _a.delete(key2)) || false;
|
|
320
|
+
}
|
|
321
|
+
deleteTop(key1) {
|
|
322
|
+
return this._map.delete(key1);
|
|
323
|
+
}
|
|
324
|
+
map(fn) {
|
|
325
|
+
return Array.from(this._map.entries()).flatMap(([k1, m2]) => Array.from(m2.entries()).map(([k2, v]) => {
|
|
326
|
+
return fn(v, k1, k2);
|
|
327
|
+
}));
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
var BetterMap = class extends Map {
|
|
331
|
+
map(mapFn) {
|
|
332
|
+
const result = [];
|
|
333
|
+
this.forEach((v, k) => {
|
|
334
|
+
result.push(mapFn(v, k));
|
|
335
|
+
});
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
// src/extractors/split.ts
|
|
341
|
+
var extractorSplit = (code) => new Set(code.split(/[\s'"`;>=]/g).filter(isValidSelector));
|
|
342
|
+
|
|
343
|
+
// src/config.ts
|
|
344
|
+
function resolveShortcuts(shortcuts) {
|
|
345
|
+
return toArray(shortcuts).flatMap((s) => {
|
|
346
|
+
if (Array.isArray(s))
|
|
347
|
+
return [s];
|
|
348
|
+
return Object.entries(s);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
function resolveConfig(userConfig = {}, defaults = {}) {
|
|
352
|
+
const config = Object.assign({}, defaults, userConfig);
|
|
353
|
+
const rawPresets = config.presets || [];
|
|
354
|
+
const sortedPresets = [
|
|
355
|
+
...rawPresets.filter((p) => p.enforce === "pre"),
|
|
356
|
+
...rawPresets.filter((p) => !p.enforce),
|
|
357
|
+
...rawPresets.filter((p) => p.enforce === "post")
|
|
358
|
+
];
|
|
359
|
+
function mergePresets(key) {
|
|
360
|
+
return uniq([
|
|
361
|
+
...sortedPresets.flatMap((p) => toArray(p[key] || [])),
|
|
362
|
+
...toArray(config[key] || [])
|
|
363
|
+
]);
|
|
364
|
+
}
|
|
365
|
+
const extractors = mergePresets("extractors");
|
|
366
|
+
if (!extractors.length)
|
|
367
|
+
extractors.push(extractorSplit);
|
|
368
|
+
const rules = mergePresets("rules");
|
|
369
|
+
const rulesStaticMap = {};
|
|
370
|
+
const rulesSize = rules.length;
|
|
371
|
+
rules.forEach((rule, i) => {
|
|
372
|
+
if (isStaticRule(rule)) {
|
|
373
|
+
rulesStaticMap[rule[0]] = [i, rule[1]];
|
|
374
|
+
delete rules[i];
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
const theme = [
|
|
378
|
+
...sortedPresets.map((p) => p.theme || {}),
|
|
379
|
+
config.theme || {}
|
|
380
|
+
].reduce((a, p) => mergeDeep(a, p), {});
|
|
381
|
+
return __spreadProps(__spreadValues({
|
|
382
|
+
mergeSelectors: true,
|
|
383
|
+
warnExcluded: true,
|
|
384
|
+
excluded: []
|
|
385
|
+
}, config), {
|
|
386
|
+
theme,
|
|
387
|
+
rulesSize,
|
|
388
|
+
rulesDynamic: rules,
|
|
389
|
+
rulesStaticMap,
|
|
390
|
+
variants: mergePresets("variants"),
|
|
391
|
+
shortcuts: resolveShortcuts(mergePresets("shortcuts")),
|
|
392
|
+
extractors
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// src/generator/utils.ts
|
|
397
|
+
var reScopePlaceholder = / \$\$ /;
|
|
398
|
+
var hasScopePlaceholder = (css) => css.match(reScopePlaceholder);
|
|
399
|
+
function applyScope(css, scope) {
|
|
400
|
+
if (hasScopePlaceholder(css))
|
|
401
|
+
return css.replace(reScopePlaceholder, scope ? ` ${scope} ` : " ");
|
|
402
|
+
else
|
|
403
|
+
return scope ? `${scope} ${css}` : css;
|
|
404
|
+
}
|
|
405
|
+
function toEscapedSelector(raw) {
|
|
406
|
+
if (raw.startsWith("["))
|
|
407
|
+
return raw.replace(/^\[(.+?)(~?=)"(.*)"\]$/, (_, n, s, i) => `[${escapeSelector(n)}${s}"${escapeSelector(i)}"]`);
|
|
408
|
+
else
|
|
409
|
+
return `.${escapeSelector(raw)}`;
|
|
410
|
+
}
|
|
411
|
+
function normalizeEntries(obj) {
|
|
412
|
+
return !Array.isArray(obj) ? Object.entries(obj) : obj;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// src/generator/index.ts
|
|
416
|
+
var UnoGenerator = class {
|
|
417
|
+
constructor(userConfig = {}, defaults = {}) {
|
|
418
|
+
this.userConfig = userConfig;
|
|
419
|
+
this.defaults = defaults;
|
|
420
|
+
this._cache = new Map();
|
|
421
|
+
this.excluded = new Set();
|
|
422
|
+
this.config = resolveConfig(userConfig, defaults);
|
|
423
|
+
}
|
|
424
|
+
setConfig(userConfig, defaults) {
|
|
425
|
+
if (!userConfig)
|
|
426
|
+
return;
|
|
427
|
+
if (defaults)
|
|
428
|
+
this.defaults = defaults;
|
|
429
|
+
this.userConfig = userConfig;
|
|
430
|
+
this.config = resolveConfig(userConfig, this.defaults);
|
|
431
|
+
this.excluded = new Set();
|
|
432
|
+
this._cache = new Map();
|
|
433
|
+
}
|
|
434
|
+
async applyExtractors(code, id, set = new Set()) {
|
|
435
|
+
await Promise.all(this.config.extractors.map(async (i) => {
|
|
436
|
+
const result = await i(code, id);
|
|
437
|
+
result == null ? void 0 : result.forEach((t) => set.add(t));
|
|
438
|
+
}));
|
|
439
|
+
return set;
|
|
440
|
+
}
|
|
441
|
+
async generate(input, id, scope) {
|
|
442
|
+
const tokens = typeof input === "string" ? await this.applyExtractors(input, id) : input;
|
|
443
|
+
const matched = new Set();
|
|
444
|
+
const sheet = new Map();
|
|
445
|
+
const hit = (raw, payload) => {
|
|
446
|
+
this._cache.set(raw, payload);
|
|
447
|
+
matched.add(raw);
|
|
448
|
+
for (const item of payload) {
|
|
449
|
+
const query = item[3] || "";
|
|
450
|
+
if (!sheet.has(query))
|
|
451
|
+
sheet.set(query, []);
|
|
452
|
+
sheet.get(query).push(item);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
await Promise.all(Array.from(tokens).map(async (raw) => {
|
|
456
|
+
if (matched.has(raw) || this.excluded.has(raw))
|
|
457
|
+
return;
|
|
458
|
+
if (this._cache.has(raw)) {
|
|
459
|
+
const r = this._cache.get(raw);
|
|
460
|
+
if (r)
|
|
461
|
+
hit(raw, r);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (this.isExcluded(raw)) {
|
|
465
|
+
this.excluded.add(raw);
|
|
466
|
+
this._cache.set(raw, null);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
const applied = this.matchVariants(raw);
|
|
470
|
+
if (this.isExcluded(applied[1])) {
|
|
471
|
+
this.excluded.add(raw);
|
|
472
|
+
this._cache.set(raw, null);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
const expanded = this.expandShortcut(applied[1]);
|
|
476
|
+
if (expanded) {
|
|
477
|
+
const utils = await this.stringifyShortcuts(applied, expanded);
|
|
478
|
+
if (utils.length) {
|
|
479
|
+
hit(raw, utils);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
} else {
|
|
483
|
+
const util = this.stringifyUtil(await this.parseUtil(applied));
|
|
484
|
+
if (util) {
|
|
485
|
+
hit(raw, [util]);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
this._cache.set(raw, null);
|
|
490
|
+
}));
|
|
491
|
+
const css = Array.from(sheet).map(([query, items]) => {
|
|
492
|
+
const size2 = items.length;
|
|
493
|
+
const sorted = items.sort((a, b) => a[0] - b[0] || a[1].localeCompare(b[1])).map((a) => [applyScope(a[1], scope), a[2]]);
|
|
494
|
+
const rules = sorted.map(([selector, body], idx) => {
|
|
495
|
+
if (this.config.mergeSelectors) {
|
|
496
|
+
for (let i = size2 - 1; i > idx; i--) {
|
|
497
|
+
const current = sorted[i];
|
|
498
|
+
if (current[1] === body) {
|
|
499
|
+
current[0] = `${selector},${current[0]}`;
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return `${selector}{${body}}`;
|
|
505
|
+
}).filter(Boolean).join("\n");
|
|
506
|
+
return query ? `${query}{
|
|
507
|
+
${rules}
|
|
508
|
+
}` : rules;
|
|
509
|
+
}).join("\n");
|
|
510
|
+
return {
|
|
511
|
+
css,
|
|
512
|
+
matched
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
matchVariants(raw) {
|
|
516
|
+
const variants = [];
|
|
517
|
+
let processed = raw;
|
|
518
|
+
let applied = false;
|
|
519
|
+
while (true) {
|
|
520
|
+
applied = false;
|
|
521
|
+
for (const v of this.config.variants) {
|
|
522
|
+
const result = v.match(processed, this.config.theme);
|
|
523
|
+
if (result && result !== processed) {
|
|
524
|
+
processed = result;
|
|
525
|
+
variants.push(v);
|
|
526
|
+
applied = true;
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (!applied)
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
return [raw, processed, variants];
|
|
534
|
+
}
|
|
535
|
+
applyVariants(parsed, variants = parsed[3], raw = parsed[1]) {
|
|
536
|
+
const theme = this.config.theme;
|
|
537
|
+
const selector = variants.reduce((p, v) => {
|
|
538
|
+
var _a;
|
|
539
|
+
return ((_a = v.selector) == null ? void 0 : _a.call(v, p, theme)) || p;
|
|
540
|
+
}, toEscapedSelector(raw));
|
|
541
|
+
const mediaQuery = variants.reduce((p, v) => {
|
|
542
|
+
var _a;
|
|
543
|
+
return ((_a = v.mediaQuery) == null ? void 0 : _a.call(v, parsed[1], theme)) || p;
|
|
544
|
+
}, void 0);
|
|
545
|
+
const entries = variants.reduce((p, v) => {
|
|
546
|
+
var _a;
|
|
547
|
+
return ((_a = v.rewrite) == null ? void 0 : _a.call(v, p, theme)) || p;
|
|
548
|
+
}, parsed[2]);
|
|
549
|
+
return [
|
|
550
|
+
selector,
|
|
551
|
+
entries,
|
|
552
|
+
mediaQuery
|
|
553
|
+
];
|
|
554
|
+
}
|
|
555
|
+
async parseUtil(input) {
|
|
556
|
+
const { theme, rulesStaticMap, rulesDynamic, rulesSize } = this.config;
|
|
557
|
+
const [raw, processed, variants] = typeof input === "string" ? this.matchVariants(input) : input;
|
|
558
|
+
const staticMatch = rulesStaticMap[processed];
|
|
559
|
+
if (staticMatch == null ? void 0 : staticMatch[1])
|
|
560
|
+
return [staticMatch[0], raw, normalizeEntries(staticMatch[1]), variants];
|
|
561
|
+
for (let i = rulesSize; i >= 0; i--) {
|
|
562
|
+
const rule = rulesDynamic[i];
|
|
563
|
+
if (!rule)
|
|
564
|
+
continue;
|
|
565
|
+
const [matcher, handler2] = rule;
|
|
566
|
+
const match = processed.match(matcher);
|
|
567
|
+
if (!match)
|
|
568
|
+
continue;
|
|
569
|
+
const obj = await handler2(match, theme);
|
|
570
|
+
if (obj)
|
|
571
|
+
return [i, raw, normalizeEntries(obj), variants];
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
stringifyUtil(parsed) {
|
|
575
|
+
if (!parsed)
|
|
576
|
+
return;
|
|
577
|
+
const [selector, entries, mediaQuery] = this.applyVariants(parsed);
|
|
578
|
+
const body = entriesToCss(entries);
|
|
579
|
+
if (!body)
|
|
580
|
+
return;
|
|
581
|
+
return [parsed[0], selector, body, mediaQuery];
|
|
582
|
+
}
|
|
583
|
+
expandShortcut(processed) {
|
|
584
|
+
let result;
|
|
585
|
+
for (const s of this.config.shortcuts) {
|
|
586
|
+
if (isStaticShortcut(s)) {
|
|
587
|
+
if (s[0] === processed)
|
|
588
|
+
result = s[1];
|
|
589
|
+
} else {
|
|
590
|
+
const match = processed.match(s[0]);
|
|
591
|
+
if (match)
|
|
592
|
+
result = s[1](match);
|
|
593
|
+
if (result)
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (!result)
|
|
598
|
+
return;
|
|
599
|
+
if (typeof result === "string")
|
|
600
|
+
result = result.split(/ /g);
|
|
601
|
+
return result;
|
|
602
|
+
}
|
|
603
|
+
async stringifyShortcuts(parent, expanded) {
|
|
604
|
+
const selectorMap = new TwoKeyMap();
|
|
605
|
+
const parsed = (await Promise.all(expanded.map((i) => this.parseUtil(i)))).filter(Boolean).sort((a, b) => a[0] - b[0]);
|
|
606
|
+
const [raw, , parentVariants] = parent;
|
|
607
|
+
for (const item of parsed) {
|
|
608
|
+
const [selector, entries, mediaQuery] = this.applyVariants(item, [...item[3], ...parentVariants], raw);
|
|
609
|
+
const mapItem = selectorMap.getFallback(selector, mediaQuery, [[], item[0]]);
|
|
610
|
+
mapItem[0].push(...entries);
|
|
611
|
+
if (item[0] > mapItem[1])
|
|
612
|
+
mapItem[1] = item[0];
|
|
613
|
+
}
|
|
614
|
+
return selectorMap.map(([entries, index], selector, mediaQuery) => {
|
|
615
|
+
const body = entriesToCss(entries);
|
|
616
|
+
if (!body)
|
|
617
|
+
return void 0;
|
|
618
|
+
return [index, selector, body, mediaQuery];
|
|
619
|
+
}).filter(Boolean);
|
|
620
|
+
}
|
|
621
|
+
isExcluded(raw) {
|
|
622
|
+
return this.config.excluded.some((e2) => typeof e2 === "string" ? e2 === raw : e2.test(raw));
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
function createGenerator(config, defaults) {
|
|
626
|
+
return new UnoGenerator(config, defaults);
|
|
627
|
+
}
|
|
628
|
+
export {
|
|
629
|
+
BetterMap,
|
|
630
|
+
TwoKeyMap,
|
|
631
|
+
UnoGenerator,
|
|
632
|
+
attributifyRE,
|
|
633
|
+
border,
|
|
634
|
+
bracket,
|
|
635
|
+
createGenerator,
|
|
636
|
+
defineConfig,
|
|
637
|
+
e,
|
|
638
|
+
entriesToCss,
|
|
639
|
+
escapeRegExp,
|
|
640
|
+
escapeSelector,
|
|
641
|
+
extractorSplit,
|
|
642
|
+
fraction,
|
|
643
|
+
getMatchedPositions,
|
|
644
|
+
handler,
|
|
645
|
+
handlersNames,
|
|
646
|
+
hex2rgba,
|
|
647
|
+
isAttributifySelector,
|
|
648
|
+
isObject,
|
|
649
|
+
isStaticRule,
|
|
650
|
+
isStaticShortcut,
|
|
651
|
+
isValidSelector,
|
|
652
|
+
mergeDeep,
|
|
653
|
+
mergeSet,
|
|
654
|
+
number,
|
|
655
|
+
percent,
|
|
656
|
+
size,
|
|
657
|
+
toArray,
|
|
658
|
+
uniq,
|
|
659
|
+
validateFilterRE,
|
|
660
|
+
variantMatcher
|
|
661
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unocss/core",
|
|
3
|
+
"version": "0.1.0-beta.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"homepage": "https://github.com/antfu/unocss#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/antfu/unocss/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/antfu/unocss.git"
|
|
13
|
+
},
|
|
14
|
+
"funding": "https://github.com/sponsors/antfu",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"require": "./dist/index.js",
|
|
21
|
+
"import": "./dist/index.mjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"main": "dist/index.js",
|
|
25
|
+
"module": "dist/index.mjs",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsup",
|
|
32
|
+
"dev": "tsup --watch src"
|
|
33
|
+
}
|
|
34
|
+
}
|