@unocss/core 0.11.3 → 0.11.4

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.d.ts CHANGED
@@ -12,10 +12,10 @@ declare class UnoGenerator {
12
12
  matchVariants(raw: string, current?: string): VariantMatchedResult;
13
13
  applyVariants(parsed: ParsedUtil, variantHandlers?: VariantHandler[], raw?: string): readonly [string, CSSEntries, string | undefined];
14
14
  constructCustomCSS(context: Readonly<RuleContext>, body: CSSObject | CSSEntries, overrideSelector?: string): string;
15
- parseUtil(input: string | VariantMatchedResult): Promise<ParsedUtil | RawUtil | undefined>;
15
+ parseUtil(input: string | VariantMatchedResult, context: RuleContext, internal?: boolean): Promise<ParsedUtil | RawUtil | undefined>;
16
16
  stringifyUtil(parsed?: ParsedUtil | RawUtil): StringifiedUtil | undefined;
17
- expandShortcut(processed: string, depth?: number): [string[], RuleMeta | undefined] | undefined;
18
- stringifyShortcuts(parent: VariantMatchedResult, expanded: string[], meta?: RuleMeta): Promise<StringifiedUtil[] | undefined>;
17
+ expandShortcut(processed: string, context: RuleContext, depth?: number): [string[], RuleMeta | undefined] | undefined;
18
+ stringifyShortcuts(parent: VariantMatchedResult, context: RuleContext, expanded: string[], meta?: RuleMeta): Promise<StringifiedUtil[] | undefined>;
19
19
  isBlocked(raw: string): boolean;
20
20
  }
21
21
  declare function createGenerator(config?: UserConfig, defaults?: UserConfigDefaults): UnoGenerator;
@@ -73,18 +73,27 @@ interface Extractor {
73
73
  order?: number;
74
74
  }
75
75
  interface RuleMeta {
76
+ /**
77
+ * The layer name of this rule.
78
+ * @default 'default'
79
+ */
76
80
  layer?: string;
81
+ /**
82
+ * Internal rules will only be matched for shortcuts but not the user code.
83
+ * @default false
84
+ */
85
+ internal?: boolean;
77
86
  }
78
87
  declare type DynamicMatcher<Theme extends {} = {}> = ((match: string[], context: Readonly<RuleContext<Theme>>) => Awaitable<CSSObject | CSSEntries | string | undefined>);
79
88
  declare type DynamicRule<Theme extends {} = {}> = [RegExp, DynamicMatcher<Theme>] | [RegExp, DynamicMatcher<Theme>, RuleMeta];
80
89
  declare type StaticRule = [string, CSSObject | CSSEntries] | [string, CSSObject | CSSEntries, RuleMeta];
81
90
  declare type Rule<Theme extends {} = {}> = DynamicRule<Theme> | StaticRule;
82
- declare type DynamicShortcutMatcher = ((match: string[]) => (string | string[] | undefined));
83
- declare type DynamicShortcut = [RegExp, DynamicShortcutMatcher] | [RegExp, DynamicShortcutMatcher, RuleMeta];
91
+ declare type DynamicShortcutMatcher<Theme extends {} = {}> = ((match: string[], context: Readonly<RuleContext<Theme>>) => (string | string[] | undefined));
84
92
  declare type StaticShortcut = [string, string | string[]] | [string, string | string[], RuleMeta];
85
93
  declare type StaticShortcutMap = Record<string, string | string[]>;
86
- declare type UserShortcuts = StaticShortcutMap | (StaticShortcut | DynamicShortcut | StaticShortcutMap)[];
87
- declare type Shortcut = StaticShortcut | DynamicShortcut;
94
+ declare type DynamicShortcut<Theme extends {} = {}> = [RegExp, DynamicShortcutMatcher<Theme>] | [RegExp, DynamicShortcutMatcher<Theme>, RuleMeta];
95
+ declare type UserShortcuts<Theme extends {} = {}> = StaticShortcutMap | (StaticShortcut | DynamicShortcut<Theme> | StaticShortcutMap)[];
96
+ declare type Shortcut<Theme extends {} = {}> = StaticShortcut | DynamicShortcut<Theme>;
88
97
  interface Preflight {
89
98
  getCSS: () => string | undefined;
90
99
  layer?: string;
package/dist/index.js CHANGED
@@ -387,13 +387,21 @@ var UnoGenerator = class {
387
387
  const applied = this.matchVariants(raw, current);
388
388
  if (!applied || this.isBlocked(applied[1]))
389
389
  return block(raw);
390
- const expanded = this.expandShortcut(applied[1]);
390
+ const context = {
391
+ rawSelector: raw,
392
+ currentSelector: applied[1],
393
+ theme: this.config.theme,
394
+ generator: this,
395
+ variantHandlers: applied[2],
396
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args)
397
+ };
398
+ const expanded = this.expandShortcut(applied[1], context);
391
399
  if (expanded) {
392
- const utils = await this.stringifyShortcuts(applied, expanded[0], expanded[1]);
400
+ const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
393
401
  if (utils == null ? void 0 : utils.length)
394
402
  return hit(raw, utils);
395
403
  } else {
396
- const util = this.stringifyUtil(await this.parseUtil(applied));
404
+ const util = this.stringifyUtil(await this.parseUtil(applied, context));
397
405
  if (util)
398
406
  return hit(raw, [util]);
399
407
  }
@@ -512,24 +520,22 @@ var UnoGenerator = class {
512
520
  return `${mediaQuery}{${cssBody}}`;
513
521
  return cssBody;
514
522
  }
515
- async parseUtil(input) {
516
- const { theme, rulesStaticMap, rulesDynamic, rulesSize } = this.config;
523
+ async parseUtil(input, context, internal = false) {
524
+ var _a, _b;
517
525
  const [raw, processed, variantHandlers] = typeof input === "string" ? this.matchVariants(input) : input;
518
- const staticMatch = rulesStaticMap[processed];
519
- if (staticMatch == null ? void 0 : staticMatch[1])
520
- return [staticMatch[0], raw, normalizeEntries(staticMatch[1]), staticMatch[2], variantHandlers];
521
- const context = {
522
- rawSelector: raw,
523
- currentSelector: processed,
524
- theme,
525
- generator: this,
526
- variantHandlers,
527
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
528
- };
526
+ const staticMatch = this.config.rulesStaticMap[processed];
527
+ if (staticMatch) {
528
+ if (staticMatch[1] && (internal || !((_a = staticMatch[2]) == null ? void 0 : _a.internal)))
529
+ return [staticMatch[0], raw, normalizeEntries(staticMatch[1]), staticMatch[2], variantHandlers];
530
+ }
531
+ context.variantHandlers = variantHandlers;
532
+ const { rulesDynamic, rulesSize } = this.config;
529
533
  for (let i = rulesSize; i >= 0; i--) {
530
534
  const rule = rulesDynamic[i];
531
535
  if (!rule)
532
536
  continue;
537
+ if (((_b = rule[2]) == null ? void 0 : _b.internal) && !internal)
538
+ continue;
533
539
  const [matcher, handler, meta] = rule;
534
540
  const match = processed.match(matcher);
535
541
  if (!match)
@@ -555,7 +561,7 @@ var UnoGenerator = class {
555
561
  return;
556
562
  return [parsed[0], selector, body, mediaQuery, parsed[3]];
557
563
  }
558
- expandShortcut(processed, depth = 3) {
564
+ expandShortcut(processed, context, depth = 3) {
559
565
  if (depth === 0)
560
566
  return;
561
567
  let meta;
@@ -570,7 +576,7 @@ var UnoGenerator = class {
570
576
  } else {
571
577
  const match = processed.match(s[0]);
572
578
  if (match)
573
- result = s[1](match);
579
+ result = s[1](match, context);
574
580
  if (result) {
575
581
  meta = meta || s[2];
576
582
  break;
@@ -584,14 +590,14 @@ var UnoGenerator = class {
584
590
  return [
585
591
  result.flatMap((r) => {
586
592
  var _a;
587
- return ((_a = this.expandShortcut(r, depth - 1)) == null ? void 0 : _a[0]) || [r];
593
+ return ((_a = this.expandShortcut(r, context, depth - 1)) == null ? void 0 : _a[0]) || [r];
588
594
  }),
589
595
  meta
590
596
  ];
591
597
  }
592
- async stringifyShortcuts(parent, expanded, meta = { layer: this.config.shortcutsLayer }) {
598
+ async stringifyShortcuts(parent, context, expanded, meta = { layer: this.config.shortcutsLayer }) {
593
599
  const selectorMap = new TwoKeyMap();
594
- const parsed = (await Promise.all(uniq(expanded).map((i) => this.parseUtil(i)))).filter(Boolean).sort((a, b) => a[0] - b[0]);
600
+ const parsed = (await Promise.all(uniq(expanded).map((i) => this.parseUtil(i, context, true)))).filter(Boolean).sort((a, b) => a[0] - b[0]);
595
601
  const [raw, , parentVariants] = parent;
596
602
  for (const item of parsed) {
597
603
  if (isRawUtil(item))
package/dist/index.mjs CHANGED
@@ -350,13 +350,21 @@ var UnoGenerator = class {
350
350
  const applied = this.matchVariants(raw, current);
351
351
  if (!applied || this.isBlocked(applied[1]))
352
352
  return block(raw);
353
- const expanded = this.expandShortcut(applied[1]);
353
+ const context = {
354
+ rawSelector: raw,
355
+ currentSelector: applied[1],
356
+ theme: this.config.theme,
357
+ generator: this,
358
+ variantHandlers: applied[2],
359
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args)
360
+ };
361
+ const expanded = this.expandShortcut(applied[1], context);
354
362
  if (expanded) {
355
- const utils = await this.stringifyShortcuts(applied, expanded[0], expanded[1]);
363
+ const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
356
364
  if (utils == null ? void 0 : utils.length)
357
365
  return hit(raw, utils);
358
366
  } else {
359
- const util = this.stringifyUtil(await this.parseUtil(applied));
367
+ const util = this.stringifyUtil(await this.parseUtil(applied, context));
360
368
  if (util)
361
369
  return hit(raw, [util]);
362
370
  }
@@ -475,24 +483,22 @@ var UnoGenerator = class {
475
483
  return `${mediaQuery}{${cssBody}}`;
476
484
  return cssBody;
477
485
  }
478
- async parseUtil(input) {
479
- const { theme, rulesStaticMap, rulesDynamic, rulesSize } = this.config;
486
+ async parseUtil(input, context, internal = false) {
487
+ var _a, _b;
480
488
  const [raw, processed, variantHandlers] = typeof input === "string" ? this.matchVariants(input) : input;
481
- const staticMatch = rulesStaticMap[processed];
482
- if (staticMatch == null ? void 0 : staticMatch[1])
483
- return [staticMatch[0], raw, normalizeEntries(staticMatch[1]), staticMatch[2], variantHandlers];
484
- const context = {
485
- rawSelector: raw,
486
- currentSelector: processed,
487
- theme,
488
- generator: this,
489
- variantHandlers,
490
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
491
- };
489
+ const staticMatch = this.config.rulesStaticMap[processed];
490
+ if (staticMatch) {
491
+ if (staticMatch[1] && (internal || !((_a = staticMatch[2]) == null ? void 0 : _a.internal)))
492
+ return [staticMatch[0], raw, normalizeEntries(staticMatch[1]), staticMatch[2], variantHandlers];
493
+ }
494
+ context.variantHandlers = variantHandlers;
495
+ const { rulesDynamic, rulesSize } = this.config;
492
496
  for (let i = rulesSize; i >= 0; i--) {
493
497
  const rule = rulesDynamic[i];
494
498
  if (!rule)
495
499
  continue;
500
+ if (((_b = rule[2]) == null ? void 0 : _b.internal) && !internal)
501
+ continue;
496
502
  const [matcher, handler, meta] = rule;
497
503
  const match = processed.match(matcher);
498
504
  if (!match)
@@ -518,7 +524,7 @@ var UnoGenerator = class {
518
524
  return;
519
525
  return [parsed[0], selector, body, mediaQuery, parsed[3]];
520
526
  }
521
- expandShortcut(processed, depth = 3) {
527
+ expandShortcut(processed, context, depth = 3) {
522
528
  if (depth === 0)
523
529
  return;
524
530
  let meta;
@@ -533,7 +539,7 @@ var UnoGenerator = class {
533
539
  } else {
534
540
  const match = processed.match(s[0]);
535
541
  if (match)
536
- result = s[1](match);
542
+ result = s[1](match, context);
537
543
  if (result) {
538
544
  meta = meta || s[2];
539
545
  break;
@@ -547,14 +553,14 @@ var UnoGenerator = class {
547
553
  return [
548
554
  result.flatMap((r) => {
549
555
  var _a;
550
- return ((_a = this.expandShortcut(r, depth - 1)) == null ? void 0 : _a[0]) || [r];
556
+ return ((_a = this.expandShortcut(r, context, depth - 1)) == null ? void 0 : _a[0]) || [r];
551
557
  }),
552
558
  meta
553
559
  ];
554
560
  }
555
- async stringifyShortcuts(parent, expanded, meta = { layer: this.config.shortcutsLayer }) {
561
+ async stringifyShortcuts(parent, context, expanded, meta = { layer: this.config.shortcutsLayer }) {
556
562
  const selectorMap = new TwoKeyMap();
557
- const parsed = (await Promise.all(uniq(expanded).map((i) => this.parseUtil(i)))).filter(Boolean).sort((a, b) => a[0] - b[0]);
563
+ const parsed = (await Promise.all(uniq(expanded).map((i) => this.parseUtil(i, context, true)))).filter(Boolean).sort((a, b) => a[0] - b[0]);
558
564
  const [raw, , parentVariants] = parent;
559
565
  for (const item of parsed) {
560
566
  if (isRawUtil(item))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
- "version": "0.11.3",
3
+ "version": "0.11.4",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "keywords": [
6
6
  "unocss",