@unocss/core 0.11.0 → 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
@@ -9,13 +9,13 @@ declare class UnoGenerator {
9
9
  setConfig(userConfig?: UserConfig, defaults?: UserConfigDefaults): void;
10
10
  applyExtractors(code: string, id?: string, set?: Set<string>): Promise<Set<string>>;
11
11
  generate(input: string | Set<string>, { id, scope, preflights, safelist, minify, }?: GenerateOptions): Promise<GenerateResult>;
12
- matchVariants(raw: string): VariantMatchedResult;
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;
@@ -28,6 +28,10 @@ declare type RestArgs<T> = Shift<ArgumentType<T>>;
28
28
  declare type DeepPartial<T> = {
29
29
  [P in keyof T]?: DeepPartial<T[P]>;
30
30
  };
31
+ declare type FlatObjectTuple<T> = {
32
+ [K in keyof T]: T[K];
33
+ };
34
+ declare type PartialByKeys<T, K extends keyof T = keyof T> = FlatObjectTuple<Partial<Pick<T, Extract<keyof T, K>>> & Omit<T, K>>;
31
35
  declare type CSSObject = Record<string, string | number | undefined>;
32
36
  declare type CSSEntries = [string, string | number | undefined][];
33
37
  interface RuleContext<Theme extends {} = {}> {
@@ -69,18 +73,27 @@ interface Extractor {
69
73
  order?: number;
70
74
  }
71
75
  interface RuleMeta {
76
+ /**
77
+ * The layer name of this rule.
78
+ * @default 'default'
79
+ */
72
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;
73
86
  }
74
87
  declare type DynamicMatcher<Theme extends {} = {}> = ((match: string[], context: Readonly<RuleContext<Theme>>) => Awaitable<CSSObject | CSSEntries | string | undefined>);
75
88
  declare type DynamicRule<Theme extends {} = {}> = [RegExp, DynamicMatcher<Theme>] | [RegExp, DynamicMatcher<Theme>, RuleMeta];
76
89
  declare type StaticRule = [string, CSSObject | CSSEntries] | [string, CSSObject | CSSEntries, RuleMeta];
77
90
  declare type Rule<Theme extends {} = {}> = DynamicRule<Theme> | StaticRule;
78
- declare type DynamicShortcutMatcher = ((match: string[]) => (string | string[] | undefined));
79
- declare type DynamicShortcut = [RegExp, DynamicShortcutMatcher] | [RegExp, DynamicShortcutMatcher, RuleMeta];
91
+ declare type DynamicShortcutMatcher<Theme extends {} = {}> = ((match: string[], context: Readonly<RuleContext<Theme>>) => (string | string[] | undefined));
80
92
  declare type StaticShortcut = [string, string | string[]] | [string, string | string[], RuleMeta];
81
93
  declare type StaticShortcutMap = Record<string, string | string[]>;
82
- declare type UserShortcuts = StaticShortcutMap | (StaticShortcut | DynamicShortcut | StaticShortcutMap)[];
83
- 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>;
84
97
  interface Preflight {
85
98
  getCSS: () => string | undefined;
86
99
  layer?: string;
@@ -187,6 +200,10 @@ interface UserOnlyOptions<Theme extends {} = {}> {
187
200
  * The theme object, will be merged with the theme provides by presets
188
201
  */
189
202
  theme?: Theme;
203
+ /**
204
+ * Preprocess the incoming utilities, return falsy value to exclude
205
+ */
206
+ preprocess?: (matcher: string) => string | undefined;
190
207
  /**
191
208
  * Layout name of shortcuts
192
209
  *
@@ -208,7 +225,7 @@ interface UserConfig<Theme extends {} = {}> extends ConfigBase<Theme>, UserOnlyO
208
225
  }
209
226
  interface UserConfigDefaults<Theme extends {} = {}> extends ConfigBase<Theme>, UserOnlyOptions<Theme> {
210
227
  }
211
- interface ResolvedConfig extends Omit<Required<UserConfig>, 'rules' | 'shortcuts'> {
228
+ interface ResolvedConfig extends Omit<PartialByKeys<Required<UserConfig>, 'preprocess'>, 'rules' | 'shortcuts'> {
212
229
  shortcuts: Shortcut[];
213
230
  variants: VariantObject[];
214
231
  rulesSize: number;
@@ -316,4 +333,4 @@ declare function withLayer<T>(layer: string, rules: Rule<T>[]): Rule<T>[];
316
333
 
317
334
  declare const extractorSplit: Extractor;
318
335
 
319
- export { ArgumentType, Awaitable, BetterMap, BlocklistRule, CSSEntries, CSSObject, ConfigBase, DeepPartial, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, Extractor, ExtractorContext, GenerateOptions, GenerateResult, GeneratorOptions, ParsedUtil, Preflight, Preset, RawUtil, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, Variant, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, createGenerator, e, entriesToCss, escapeRegExp, escapeSelector, extractorSplit, hasScopePlaceholder, hex2rgba, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeVariant, toArray, uniq, validateFilterRE, withLayer };
336
+ export { ArgumentType, Awaitable, BetterMap, BlocklistRule, CSSEntries, CSSObject, ConfigBase, DeepPartial, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, Extractor, ExtractorContext, FlatObjectTuple, GenerateOptions, GenerateResult, GeneratorOptions, ParsedUtil, PartialByKeys, Preflight, Preset, RawUtil, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, Variant, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, createGenerator, e, entriesToCss, escapeRegExp, escapeSelector, extractorSplit, hasScopePlaceholder, hex2rgba, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeVariant, toArray, uniq, validateFilterRE, withLayer };
package/dist/index.js CHANGED
@@ -366,6 +366,10 @@ var UnoGenerator = class {
366
366
  layerSet.add(item[4].layer);
367
367
  }
368
368
  };
369
+ const block = (raw) => {
370
+ this.blocked.add(raw);
371
+ this._cache.set(raw, null);
372
+ };
369
373
  await Promise.all(Array.from(tokens).map(async (raw) => {
370
374
  if (matched.has(raw) || this.blocked.has(raw))
371
375
  return;
@@ -375,30 +379,31 @@ var UnoGenerator = class {
375
379
  hit(raw, r);
376
380
  return;
377
381
  }
378
- if (this.isBlocked(raw)) {
379
- this.blocked.add(raw);
380
- this._cache.set(raw, null);
381
- return;
382
- }
383
- const applied = this.matchVariants(raw);
384
- if (this.isBlocked(applied[1])) {
385
- this.blocked.add(raw);
386
- this._cache.set(raw, null);
387
- return;
388
- }
389
- const expanded = this.expandShortcut(applied[1]);
382
+ let current = raw;
383
+ if (this.config.preprocess)
384
+ current = this.config.preprocess(raw);
385
+ if (this.isBlocked(current))
386
+ return block(current);
387
+ const applied = this.matchVariants(raw, current);
388
+ if (!applied || this.isBlocked(applied[1]))
389
+ return block(raw);
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);
390
399
  if (expanded) {
391
- const utils = await this.stringifyShortcuts(applied, expanded[0], expanded[1]);
392
- if (utils == null ? void 0 : utils.length) {
393
- hit(raw, utils);
394
- return;
395
- }
400
+ const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
401
+ if (utils == null ? void 0 : utils.length)
402
+ return hit(raw, utils);
396
403
  } else {
397
- const util = this.stringifyUtil(await this.parseUtil(applied));
398
- if (util) {
399
- hit(raw, [util]);
400
- return;
401
- }
404
+ const util = this.stringifyUtil(await this.parseUtil(applied, context));
405
+ if (util)
406
+ return hit(raw, [util]);
402
407
  }
403
408
  this._cache.set(raw, null);
404
409
  }));
@@ -462,10 +467,10 @@ var UnoGenerator = class {
462
467
  matched
463
468
  };
464
469
  }
465
- matchVariants(raw) {
470
+ matchVariants(raw, current) {
466
471
  const usedVariants = new Set();
467
472
  const handlers = [];
468
- let processed = raw;
473
+ let processed = current || raw;
469
474
  let applied = false;
470
475
  while (true) {
471
476
  applied = false;
@@ -515,24 +520,22 @@ var UnoGenerator = class {
515
520
  return `${mediaQuery}{${cssBody}}`;
516
521
  return cssBody;
517
522
  }
518
- async parseUtil(input) {
519
- const { theme, rulesStaticMap, rulesDynamic, rulesSize } = this.config;
523
+ async parseUtil(input, context, internal = false) {
524
+ var _a, _b;
520
525
  const [raw, processed, variantHandlers] = typeof input === "string" ? this.matchVariants(input) : input;
521
- const staticMatch = rulesStaticMap[processed];
522
- if (staticMatch == null ? void 0 : staticMatch[1])
523
- return [staticMatch[0], raw, normalizeEntries(staticMatch[1]), staticMatch[2], variantHandlers];
524
- const context = {
525
- rawSelector: raw,
526
- currentSelector: processed,
527
- theme,
528
- generator: this,
529
- variantHandlers,
530
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
531
- };
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;
532
533
  for (let i = rulesSize; i >= 0; i--) {
533
534
  const rule = rulesDynamic[i];
534
535
  if (!rule)
535
536
  continue;
537
+ if (((_b = rule[2]) == null ? void 0 : _b.internal) && !internal)
538
+ continue;
536
539
  const [matcher, handler, meta] = rule;
537
540
  const match = processed.match(matcher);
538
541
  if (!match)
@@ -558,7 +561,7 @@ var UnoGenerator = class {
558
561
  return;
559
562
  return [parsed[0], selector, body, mediaQuery, parsed[3]];
560
563
  }
561
- expandShortcut(processed, depth = 3) {
564
+ expandShortcut(processed, context, depth = 3) {
562
565
  if (depth === 0)
563
566
  return;
564
567
  let meta;
@@ -573,7 +576,7 @@ var UnoGenerator = class {
573
576
  } else {
574
577
  const match = processed.match(s[0]);
575
578
  if (match)
576
- result = s[1](match);
579
+ result = s[1](match, context);
577
580
  if (result) {
578
581
  meta = meta || s[2];
579
582
  break;
@@ -587,14 +590,14 @@ var UnoGenerator = class {
587
590
  return [
588
591
  result.flatMap((r) => {
589
592
  var _a;
590
- 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];
591
594
  }),
592
595
  meta
593
596
  ];
594
597
  }
595
- async stringifyShortcuts(parent, expanded, meta = { layer: this.config.shortcutsLayer }) {
598
+ async stringifyShortcuts(parent, context, expanded, meta = { layer: this.config.shortcutsLayer }) {
596
599
  const selectorMap = new TwoKeyMap();
597
- 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]);
598
601
  const [raw, , parentVariants] = parent;
599
602
  for (const item of parsed) {
600
603
  if (isRawUtil(item))
@@ -613,7 +616,7 @@ var UnoGenerator = class {
613
616
  }).filter(Boolean);
614
617
  }
615
618
  isBlocked(raw) {
616
- return this.config.blocklist.some((e2) => typeof e2 === "string" ? e2 === raw : e2.test(raw));
619
+ return !raw || this.config.blocklist.some((e2) => typeof e2 === "string" ? e2 === raw : e2.test(raw));
617
620
  }
618
621
  };
619
622
  function createGenerator(config, defaults) {
package/dist/index.mjs CHANGED
@@ -329,6 +329,10 @@ var UnoGenerator = class {
329
329
  layerSet.add(item[4].layer);
330
330
  }
331
331
  };
332
+ const block = (raw) => {
333
+ this.blocked.add(raw);
334
+ this._cache.set(raw, null);
335
+ };
332
336
  await Promise.all(Array.from(tokens).map(async (raw) => {
333
337
  if (matched.has(raw) || this.blocked.has(raw))
334
338
  return;
@@ -338,30 +342,31 @@ var UnoGenerator = class {
338
342
  hit(raw, r);
339
343
  return;
340
344
  }
341
- if (this.isBlocked(raw)) {
342
- this.blocked.add(raw);
343
- this._cache.set(raw, null);
344
- return;
345
- }
346
- const applied = this.matchVariants(raw);
347
- if (this.isBlocked(applied[1])) {
348
- this.blocked.add(raw);
349
- this._cache.set(raw, null);
350
- return;
351
- }
352
- const expanded = this.expandShortcut(applied[1]);
345
+ let current = raw;
346
+ if (this.config.preprocess)
347
+ current = this.config.preprocess(raw);
348
+ if (this.isBlocked(current))
349
+ return block(current);
350
+ const applied = this.matchVariants(raw, current);
351
+ if (!applied || this.isBlocked(applied[1]))
352
+ return block(raw);
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);
353
362
  if (expanded) {
354
- const utils = await this.stringifyShortcuts(applied, expanded[0], expanded[1]);
355
- if (utils == null ? void 0 : utils.length) {
356
- hit(raw, utils);
357
- return;
358
- }
363
+ const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
364
+ if (utils == null ? void 0 : utils.length)
365
+ return hit(raw, utils);
359
366
  } else {
360
- const util = this.stringifyUtil(await this.parseUtil(applied));
361
- if (util) {
362
- hit(raw, [util]);
363
- return;
364
- }
367
+ const util = this.stringifyUtil(await this.parseUtil(applied, context));
368
+ if (util)
369
+ return hit(raw, [util]);
365
370
  }
366
371
  this._cache.set(raw, null);
367
372
  }));
@@ -425,10 +430,10 @@ var UnoGenerator = class {
425
430
  matched
426
431
  };
427
432
  }
428
- matchVariants(raw) {
433
+ matchVariants(raw, current) {
429
434
  const usedVariants = new Set();
430
435
  const handlers = [];
431
- let processed = raw;
436
+ let processed = current || raw;
432
437
  let applied = false;
433
438
  while (true) {
434
439
  applied = false;
@@ -478,24 +483,22 @@ var UnoGenerator = class {
478
483
  return `${mediaQuery}{${cssBody}}`;
479
484
  return cssBody;
480
485
  }
481
- async parseUtil(input) {
482
- const { theme, rulesStaticMap, rulesDynamic, rulesSize } = this.config;
486
+ async parseUtil(input, context, internal = false) {
487
+ var _a, _b;
483
488
  const [raw, processed, variantHandlers] = typeof input === "string" ? this.matchVariants(input) : input;
484
- const staticMatch = rulesStaticMap[processed];
485
- if (staticMatch == null ? void 0 : staticMatch[1])
486
- return [staticMatch[0], raw, normalizeEntries(staticMatch[1]), staticMatch[2], variantHandlers];
487
- const context = {
488
- rawSelector: raw,
489
- currentSelector: processed,
490
- theme,
491
- generator: this,
492
- variantHandlers,
493
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
494
- };
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;
495
496
  for (let i = rulesSize; i >= 0; i--) {
496
497
  const rule = rulesDynamic[i];
497
498
  if (!rule)
498
499
  continue;
500
+ if (((_b = rule[2]) == null ? void 0 : _b.internal) && !internal)
501
+ continue;
499
502
  const [matcher, handler, meta] = rule;
500
503
  const match = processed.match(matcher);
501
504
  if (!match)
@@ -521,7 +524,7 @@ var UnoGenerator = class {
521
524
  return;
522
525
  return [parsed[0], selector, body, mediaQuery, parsed[3]];
523
526
  }
524
- expandShortcut(processed, depth = 3) {
527
+ expandShortcut(processed, context, depth = 3) {
525
528
  if (depth === 0)
526
529
  return;
527
530
  let meta;
@@ -536,7 +539,7 @@ var UnoGenerator = class {
536
539
  } else {
537
540
  const match = processed.match(s[0]);
538
541
  if (match)
539
- result = s[1](match);
542
+ result = s[1](match, context);
540
543
  if (result) {
541
544
  meta = meta || s[2];
542
545
  break;
@@ -550,14 +553,14 @@ var UnoGenerator = class {
550
553
  return [
551
554
  result.flatMap((r) => {
552
555
  var _a;
553
- 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];
554
557
  }),
555
558
  meta
556
559
  ];
557
560
  }
558
- async stringifyShortcuts(parent, expanded, meta = { layer: this.config.shortcutsLayer }) {
561
+ async stringifyShortcuts(parent, context, expanded, meta = { layer: this.config.shortcutsLayer }) {
559
562
  const selectorMap = new TwoKeyMap();
560
- 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]);
561
564
  const [raw, , parentVariants] = parent;
562
565
  for (const item of parsed) {
563
566
  if (isRawUtil(item))
@@ -576,7 +579,7 @@ var UnoGenerator = class {
576
579
  }).filter(Boolean);
577
580
  }
578
581
  isBlocked(raw) {
579
- return this.config.blocklist.some((e2) => typeof e2 === "string" ? e2 === raw : e2.test(raw));
582
+ return !raw || this.config.blocklist.some((e2) => typeof e2 === "string" ? e2 === raw : e2.test(raw));
580
583
  }
581
584
  };
582
585
  function createGenerator(config, defaults) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
- "version": "0.11.0",
3
+ "version": "0.11.4",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "keywords": [
6
6
  "unocss",