@unocss/core 0.22.6 → 0.22.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/dist/index.cjs CHANGED
@@ -137,28 +137,6 @@ function mergeSet(target, append) {
137
137
  return target;
138
138
  }
139
139
 
140
- const hexRE = /^#?([\da-f]+)$/i;
141
- function hex2rgba(hex = "") {
142
- const [, body] = hex.match(hexRE) || [];
143
- if (!body)
144
- return;
145
- switch (body.length) {
146
- case 3:
147
- case 4:
148
- const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
149
- if (body.length === 3)
150
- return digits;
151
- digits[3] = Math.round(digits[3] / 255 * 100) / 100;
152
- return digits;
153
- case 6:
154
- case 8:
155
- const value = Number.parseInt(body, 16);
156
- if (body.length === 6)
157
- return [value >> 16 & 255, value >> 8 & 255, value & 255];
158
- return [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255, Math.round((value & 255) / 255 * 100) / 100];
159
- }
160
- }
161
-
162
140
  const attributifyRE = /^\[(.+?)~?="(.*)"\]$/;
163
141
  const validateFilterRE = /(?!\d|-{2}|-\d)[a-zA-Z0-9\u00A0-\uFFFF-_:%-?]/;
164
142
  const CONTROL_SHORTCUT_NO_MERGE = "$$shortcut-no-merge";
@@ -388,7 +366,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
388
366
  };
389
367
  }
390
368
 
391
- const version = "0.22.6";
369
+ const version = "0.22.7";
392
370
 
393
371
  class UnoGenerator {
394
372
  constructor(userConfig = {}, defaults = {}) {
@@ -425,6 +403,49 @@ class UnoGenerator {
425
403
  }
426
404
  return set;
427
405
  }
406
+ async parseToken(raw) {
407
+ if (this.blocked.has(raw))
408
+ return;
409
+ if (this._cache.has(raw))
410
+ return this._cache.get(raw);
411
+ let current = raw;
412
+ for (const p of this.config.preprocess)
413
+ current = p(raw);
414
+ if (this.isBlocked(current)) {
415
+ this.blocked.add(raw);
416
+ this._cache.set(raw, null);
417
+ return;
418
+ }
419
+ const applied = this.matchVariants(raw, current);
420
+ if (!applied || this.isBlocked(applied[1])) {
421
+ this.blocked.add(raw);
422
+ this._cache.set(raw, null);
423
+ return;
424
+ }
425
+ const context = {
426
+ rawSelector: raw,
427
+ currentSelector: applied[1],
428
+ theme: this.config.theme,
429
+ generator: this,
430
+ variantHandlers: applied[2],
431
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args)
432
+ };
433
+ const expanded = this.expandShortcut(applied[1], context);
434
+ if (expanded) {
435
+ const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
436
+ if (utils?.length) {
437
+ this._cache.set(raw, utils);
438
+ return utils;
439
+ }
440
+ } else {
441
+ const utils = (await this.parseUtil(applied, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
442
+ if (utils?.length) {
443
+ this._cache.set(raw, utils);
444
+ return utils;
445
+ }
446
+ }
447
+ this._cache.set(raw, null);
448
+ }
428
449
  async generate(input, {
429
450
  id,
430
451
  scope,
@@ -439,8 +460,12 @@ class UnoGenerator {
439
460
  const layerSet = /* @__PURE__ */ new Set(["default"]);
440
461
  const matched = /* @__PURE__ */ new Set();
441
462
  const sheet = /* @__PURE__ */ new Map();
442
- const hit = (raw, payload) => {
443
- this._cache.set(raw, payload);
463
+ await Promise.all(Array.from(tokens).map(async (raw) => {
464
+ if (matched.has(raw))
465
+ return;
466
+ const payload = await this.parseToken(raw);
467
+ if (payload == null)
468
+ return;
444
469
  matched.add(raw);
445
470
  for (const item of payload) {
446
471
  const parent = item[3] || "";
@@ -450,47 +475,6 @@ class UnoGenerator {
450
475
  if (item[4]?.layer)
451
476
  layerSet.add(item[4].layer);
452
477
  }
453
- };
454
- const block = (raw) => {
455
- this.blocked.add(raw);
456
- this._cache.set(raw, null);
457
- };
458
- await Promise.all(Array.from(tokens).map(async (raw) => {
459
- if (matched.has(raw) || this.blocked.has(raw))
460
- return;
461
- if (this._cache.has(raw)) {
462
- const r = this._cache.get(raw);
463
- if (r)
464
- hit(raw, r);
465
- return;
466
- }
467
- let current = raw;
468
- for (const p of this.config.preprocess)
469
- current = p(raw);
470
- if (this.isBlocked(current))
471
- return block(current);
472
- const applied = this.matchVariants(raw, current);
473
- if (!applied || this.isBlocked(applied[1]))
474
- return block(raw);
475
- const context = {
476
- rawSelector: raw,
477
- currentSelector: applied[1],
478
- theme: this.config.theme,
479
- generator: this,
480
- variantHandlers: applied[2],
481
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
482
- };
483
- const expanded = this.expandShortcut(applied[1], context);
484
- if (expanded) {
485
- const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
486
- if (utils?.length)
487
- return hit(raw, utils);
488
- } else {
489
- const utils = (await this.parseUtil(applied, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
490
- if (utils?.length)
491
- return hit(raw, utils);
492
- }
493
- this._cache.set(raw, null);
494
478
  }));
495
479
  if (preflights) {
496
480
  this.config.preflights.forEach((i) => {
@@ -591,7 +575,8 @@ class UnoGenerator {
591
575
  const obj = {
592
576
  selector: handlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw)),
593
577
  entries,
594
- parent: handlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0)
578
+ parent: handlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0),
579
+ layer: handlers.reduce((p, v) => v.layer || p, void 0)
595
580
  };
596
581
  for (const p of this.config.postprocess)
597
582
  p(obj);
@@ -639,11 +624,12 @@ class UnoGenerator {
639
624
  return;
640
625
  if (isRawUtil(parsed))
641
626
  return [parsed[0], void 0, parsed[1], void 0, parsed[2]];
642
- const { selector, entries, parent } = this.applyVariants(parsed);
627
+ const { selector, entries, parent, layer: variantLayer } = this.applyVariants(parsed);
643
628
  const body = entriesToCss(entries);
644
629
  if (!body)
645
630
  return;
646
- return [parsed[0], selector, body, parent, parsed[3]];
631
+ const { layer: metaLayer, ...meta } = parsed[3] ?? {};
632
+ return [parsed[0], selector, body, parent, { ...meta, layer: variantLayer ?? metaLayer }];
647
633
  }
648
634
  expandShortcut(processed, context, depth = 3) {
649
635
  if (depth === 0)
@@ -749,7 +735,6 @@ exports.expandVariantGroup = expandVariantGroup;
749
735
  exports.extractorSplit = extractorSplit;
750
736
  exports.extractorSvelte = extractorSvelte;
751
737
  exports.hasScopePlaceholder = hasScopePlaceholder;
752
- exports.hex2rgba = hex2rgba;
753
738
  exports.isAttributifySelector = isAttributifySelector;
754
739
  exports.isObject = isObject;
755
740
  exports.isRawUtil = isRawUtil;
package/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ declare class UnoGenerator {
9
9
  constructor(userConfig?: UserConfig, defaults?: UserConfigDefaults);
10
10
  setConfig(userConfig?: UserConfig, defaults?: UserConfigDefaults): void;
11
11
  applyExtractors(code: string, id?: string, set?: Set<string>): Promise<Set<string>>;
12
+ parseToken(raw: string): Promise<StringifiedUtil[] | null | undefined>;
12
13
  generate(input: string | Set<string>, { id, scope, preflights, safelist, minify, }?: GenerateOptions): Promise<GenerateResult>;
13
14
  matchVariants(raw: string, current?: string): VariantMatchedResult;
14
15
  applyVariants(parsed: ParsedUtil, variantHandlers?: VariantHandler[], raw?: string): UtilObject;
@@ -37,6 +38,11 @@ declare type PartialByKeys<T, K extends keyof T = keyof T> = FlatObjectTuple<Par
37
38
  declare type RequiredByKey<T, K extends keyof T = keyof T> = FlatObjectTuple<Required<Pick<T, Extract<keyof T, K>>> & Omit<T, K>>;
38
39
  declare type CSSObject = Record<string, string | number | undefined>;
39
40
  declare type CSSEntries = [string, string | number | undefined][];
41
+ interface CSSColorValue {
42
+ type: string;
43
+ components: (string | number)[];
44
+ alpha: string | number | undefined;
45
+ }
40
46
  declare type RGBAColorValue = [number, number, number, number] | [number, number, number];
41
47
  interface ParsedColorValue {
42
48
  /**
@@ -172,6 +178,10 @@ interface VariantHandler {
172
178
  * Variant ordering.
173
179
  */
174
180
  order?: number;
181
+ /**
182
+ * Override layer to the output css.
183
+ */
184
+ layer?: string | undefined;
175
185
  }
176
186
  declare type VariantFunction<Theme extends {} = {}> = (matcher: string, context: Readonly<VariantContext<Theme>>) => string | VariantHandler | undefined;
177
187
  interface VariantObject<Theme extends {} = {}> {
@@ -363,6 +373,7 @@ interface UtilObject {
363
373
  selector: string;
364
374
  entries: CSSEntries;
365
375
  parent: string | undefined;
376
+ layer: string | undefined;
366
377
  }
367
378
  interface GenerateOptions {
368
379
  /**
@@ -411,8 +422,6 @@ declare function toArray<T>(value?: T | T[]): T[];
411
422
  declare function uniq<T>(value: T[]): T[];
412
423
  declare function mergeSet<T>(target: Set<T>, append: Set<T>): Set<T>;
413
424
 
414
- declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
415
-
416
425
  declare const attributifyRE: RegExp;
417
426
  declare const validateFilterRE: RegExp;
418
427
  declare const CONTROL_SHORTCUT_NO_MERGE = "$$shortcut-no-merge";
@@ -458,4 +467,4 @@ declare const extractorSplit: Extractor;
458
467
 
459
468
  declare const extractorSvelte: Extractor;
460
469
 
461
- export { ArgumentType, Arrayable, Awaitable, BetterMap, BlocklistRule, CONTROL_SHORTCUT_NO_MERGE, CSSEntries, CSSObject, CSSValues, ConfigBase, DeepPartial, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, Extractor, ExtractorContext, FilterPattern, FlatObjectTuple, GenerateOptions, GenerateResult, GeneratorOptions, ParsedColorValue, ParsedUtil, PartialByKeys, PluginOptions, Postprocessor, Preflight, Preprocessor, Preset, PresetOptions, RGBAColorValue, RawUtil, RequiredByKey, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, ThemeExtender, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, UtilObject, ValueHandler, ValueHandlerCallback, Variant, VariantContext, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, clearIdenticalEntries, clone, createGenerator, createValueHandler, e, entriesToCss, escapeRegExp, escapeSelector, expandVariantGroup, extractorSplit, extractorSvelte, hasScopePlaceholder, hex2rgba, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeCSSEntries, normalizeCSSValues, normalizeVariant, notNull, regexClassGroup, toArray, uniq, validateFilterRE, warnOnce, withLayer };
470
+ export { ArgumentType, Arrayable, Awaitable, BetterMap, BlocklistRule, CONTROL_SHORTCUT_NO_MERGE, CSSColorValue, CSSEntries, CSSObject, CSSValues, ConfigBase, DeepPartial, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, Extractor, ExtractorContext, FilterPattern, FlatObjectTuple, GenerateOptions, GenerateResult, GeneratorOptions, ParsedColorValue, ParsedUtil, PartialByKeys, PluginOptions, Postprocessor, Preflight, Preprocessor, Preset, PresetOptions, RGBAColorValue, RawUtil, RequiredByKey, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, ThemeExtender, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, UtilObject, ValueHandler, ValueHandlerCallback, Variant, VariantContext, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, clearIdenticalEntries, clone, createGenerator, createValueHandler, e, entriesToCss, escapeRegExp, escapeSelector, expandVariantGroup, extractorSplit, extractorSvelte, hasScopePlaceholder, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeCSSEntries, normalizeCSSValues, normalizeVariant, notNull, regexClassGroup, toArray, uniq, validateFilterRE, warnOnce, withLayer };
package/dist/index.mjs CHANGED
@@ -133,28 +133,6 @@ function mergeSet(target, append) {
133
133
  return target;
134
134
  }
135
135
 
136
- const hexRE = /^#?([\da-f]+)$/i;
137
- function hex2rgba(hex = "") {
138
- const [, body] = hex.match(hexRE) || [];
139
- if (!body)
140
- return;
141
- switch (body.length) {
142
- case 3:
143
- case 4:
144
- const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
145
- if (body.length === 3)
146
- return digits;
147
- digits[3] = Math.round(digits[3] / 255 * 100) / 100;
148
- return digits;
149
- case 6:
150
- case 8:
151
- const value = Number.parseInt(body, 16);
152
- if (body.length === 6)
153
- return [value >> 16 & 255, value >> 8 & 255, value & 255];
154
- return [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255, Math.round((value & 255) / 255 * 100) / 100];
155
- }
156
- }
157
-
158
136
  const attributifyRE = /^\[(.+?)~?="(.*)"\]$/;
159
137
  const validateFilterRE = /(?!\d|-{2}|-\d)[a-zA-Z0-9\u00A0-\uFFFF-_:%-?]/;
160
138
  const CONTROL_SHORTCUT_NO_MERGE = "$$shortcut-no-merge";
@@ -384,7 +362,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
384
362
  };
385
363
  }
386
364
 
387
- const version = "0.22.6";
365
+ const version = "0.22.7";
388
366
 
389
367
  class UnoGenerator {
390
368
  constructor(userConfig = {}, defaults = {}) {
@@ -421,6 +399,49 @@ class UnoGenerator {
421
399
  }
422
400
  return set;
423
401
  }
402
+ async parseToken(raw) {
403
+ if (this.blocked.has(raw))
404
+ return;
405
+ if (this._cache.has(raw))
406
+ return this._cache.get(raw);
407
+ let current = raw;
408
+ for (const p of this.config.preprocess)
409
+ current = p(raw);
410
+ if (this.isBlocked(current)) {
411
+ this.blocked.add(raw);
412
+ this._cache.set(raw, null);
413
+ return;
414
+ }
415
+ const applied = this.matchVariants(raw, current);
416
+ if (!applied || this.isBlocked(applied[1])) {
417
+ this.blocked.add(raw);
418
+ this._cache.set(raw, null);
419
+ return;
420
+ }
421
+ const context = {
422
+ rawSelector: raw,
423
+ currentSelector: applied[1],
424
+ theme: this.config.theme,
425
+ generator: this,
426
+ variantHandlers: applied[2],
427
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args)
428
+ };
429
+ const expanded = this.expandShortcut(applied[1], context);
430
+ if (expanded) {
431
+ const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
432
+ if (utils?.length) {
433
+ this._cache.set(raw, utils);
434
+ return utils;
435
+ }
436
+ } else {
437
+ const utils = (await this.parseUtil(applied, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
438
+ if (utils?.length) {
439
+ this._cache.set(raw, utils);
440
+ return utils;
441
+ }
442
+ }
443
+ this._cache.set(raw, null);
444
+ }
424
445
  async generate(input, {
425
446
  id,
426
447
  scope,
@@ -435,8 +456,12 @@ class UnoGenerator {
435
456
  const layerSet = /* @__PURE__ */ new Set(["default"]);
436
457
  const matched = /* @__PURE__ */ new Set();
437
458
  const sheet = /* @__PURE__ */ new Map();
438
- const hit = (raw, payload) => {
439
- this._cache.set(raw, payload);
459
+ await Promise.all(Array.from(tokens).map(async (raw) => {
460
+ if (matched.has(raw))
461
+ return;
462
+ const payload = await this.parseToken(raw);
463
+ if (payload == null)
464
+ return;
440
465
  matched.add(raw);
441
466
  for (const item of payload) {
442
467
  const parent = item[3] || "";
@@ -446,47 +471,6 @@ class UnoGenerator {
446
471
  if (item[4]?.layer)
447
472
  layerSet.add(item[4].layer);
448
473
  }
449
- };
450
- const block = (raw) => {
451
- this.blocked.add(raw);
452
- this._cache.set(raw, null);
453
- };
454
- await Promise.all(Array.from(tokens).map(async (raw) => {
455
- if (matched.has(raw) || this.blocked.has(raw))
456
- return;
457
- if (this._cache.has(raw)) {
458
- const r = this._cache.get(raw);
459
- if (r)
460
- hit(raw, r);
461
- return;
462
- }
463
- let current = raw;
464
- for (const p of this.config.preprocess)
465
- current = p(raw);
466
- if (this.isBlocked(current))
467
- return block(current);
468
- const applied = this.matchVariants(raw, current);
469
- if (!applied || this.isBlocked(applied[1]))
470
- return block(raw);
471
- const context = {
472
- rawSelector: raw,
473
- currentSelector: applied[1],
474
- theme: this.config.theme,
475
- generator: this,
476
- variantHandlers: applied[2],
477
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
478
- };
479
- const expanded = this.expandShortcut(applied[1], context);
480
- if (expanded) {
481
- const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
482
- if (utils?.length)
483
- return hit(raw, utils);
484
- } else {
485
- const utils = (await this.parseUtil(applied, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
486
- if (utils?.length)
487
- return hit(raw, utils);
488
- }
489
- this._cache.set(raw, null);
490
474
  }));
491
475
  if (preflights) {
492
476
  this.config.preflights.forEach((i) => {
@@ -587,7 +571,8 @@ class UnoGenerator {
587
571
  const obj = {
588
572
  selector: handlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw)),
589
573
  entries,
590
- parent: handlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0)
574
+ parent: handlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0),
575
+ layer: handlers.reduce((p, v) => v.layer || p, void 0)
591
576
  };
592
577
  for (const p of this.config.postprocess)
593
578
  p(obj);
@@ -635,11 +620,12 @@ class UnoGenerator {
635
620
  return;
636
621
  if (isRawUtil(parsed))
637
622
  return [parsed[0], void 0, parsed[1], void 0, parsed[2]];
638
- const { selector, entries, parent } = this.applyVariants(parsed);
623
+ const { selector, entries, parent, layer: variantLayer } = this.applyVariants(parsed);
639
624
  const body = entriesToCss(entries);
640
625
  if (!body)
641
626
  return;
642
- return [parsed[0], selector, body, parent, parsed[3]];
627
+ const { layer: metaLayer, ...meta } = parsed[3] ?? {};
628
+ return [parsed[0], selector, body, parent, { ...meta, layer: variantLayer ?? metaLayer }];
643
629
  }
644
630
  expandShortcut(processed, context, depth = 3) {
645
631
  if (depth === 0)
@@ -728,4 +714,4 @@ function toEscapedSelector(raw) {
728
714
  return `.${e(raw)}`;
729
715
  }
730
716
 
731
- export { BetterMap, CONTROL_SHORTCUT_NO_MERGE, TwoKeyMap, UnoGenerator, attributifyRE, clearIdenticalEntries, clone, createGenerator, createValueHandler, e, entriesToCss, escapeRegExp, escapeSelector, expandVariantGroup, extractorSplit, extractorSvelte, hasScopePlaceholder, hex2rgba, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeCSSEntries, normalizeCSSValues, normalizeVariant, notNull, regexClassGroup, toArray, uniq, validateFilterRE, warnOnce, withLayer };
717
+ export { BetterMap, CONTROL_SHORTCUT_NO_MERGE, TwoKeyMap, UnoGenerator, attributifyRE, clearIdenticalEntries, clone, createGenerator, createValueHandler, e, entriesToCss, escapeRegExp, escapeSelector, expandVariantGroup, extractorSplit, extractorSvelte, hasScopePlaceholder, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeCSSEntries, normalizeCSSValues, normalizeVariant, notNull, regexClassGroup, toArray, uniq, validateFilterRE, warnOnce, withLayer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
- "version": "0.22.6",
3
+ "version": "0.22.7",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "keywords": [
6
6
  "unocss",