@unocss/core 0.25.1 → 0.26.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/dist/index.cjs CHANGED
@@ -366,7 +366,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
366
366
  };
367
367
  }
368
368
 
369
- const version = "0.25.1";
369
+ const version = "0.26.0";
370
370
 
371
371
  class UnoGenerator {
372
372
  constructor(userConfig = {}, defaults = {}) {
@@ -403,48 +403,55 @@ class UnoGenerator {
403
403
  }
404
404
  return set;
405
405
  }
406
- async parseToken(raw) {
406
+ makeContext(raw, applied) {
407
+ const context = {
408
+ rawSelector: raw,
409
+ currentSelector: applied[1],
410
+ theme: this.config.theme,
411
+ generator: this,
412
+ variantHandlers: applied[2],
413
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args),
414
+ variantMatch: applied
415
+ };
416
+ return context;
417
+ }
418
+ async parseToken(raw, alias) {
407
419
  if (this.blocked.has(raw))
408
420
  return;
409
- if (this._cache.has(raw))
410
- return this._cache.get(raw);
421
+ const cacheKey = `${raw}${alias ? ` ${alias}` : ""}`;
422
+ const cached = this._cache.get(cacheKey);
423
+ if (cached)
424
+ return cached;
411
425
  let current = raw;
412
426
  for (const p of this.config.preprocess)
413
427
  current = p(raw);
414
428
  if (this.isBlocked(current)) {
415
429
  this.blocked.add(raw);
416
- this._cache.set(raw, null);
430
+ this._cache.set(cacheKey, null);
417
431
  return;
418
432
  }
419
433
  const applied = this.matchVariants(raw, current);
420
434
  if (!applied || this.isBlocked(applied[1])) {
421
435
  this.blocked.add(raw);
422
- this._cache.set(raw, null);
436
+ this._cache.set(cacheKey, null);
423
437
  return;
424
438
  }
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);
439
+ const context = this.makeContext(raw, [alias || applied[0], applied[1], applied[2]]);
440
+ const expanded = this.expandShortcut(context.currentSelector, context);
434
441
  if (expanded) {
435
- const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
442
+ const utils = await this.stringifyShortcuts(context.variantMatch, context, expanded[0], expanded[1]);
436
443
  if (utils?.length) {
437
- this._cache.set(raw, utils);
444
+ this._cache.set(cacheKey, utils);
438
445
  return utils;
439
446
  }
440
447
  } else {
441
- const utils = (await this.parseUtil(applied, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
448
+ const utils = (await this.parseUtil(context.variantMatch, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
442
449
  if (utils?.length) {
443
- this._cache.set(raw, utils);
450
+ this._cache.set(cacheKey, utils);
444
451
  return utils;
445
452
  }
446
453
  }
447
- this._cache.set(raw, null);
454
+ this._cache.set(cacheKey, null);
448
455
  }
449
456
  async generate(input, {
450
457
  id,
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { LoadConfigResult } from 'unconfig';
2
+
1
3
  declare class UnoGenerator {
2
4
  userConfig: UserConfig;
3
5
  defaults: UserConfigDefaults;
@@ -9,7 +11,8 @@ declare class UnoGenerator {
9
11
  constructor(userConfig?: UserConfig, defaults?: UserConfigDefaults);
10
12
  setConfig(userConfig?: UserConfig, defaults?: UserConfigDefaults): void;
11
13
  applyExtractors(code: string, id?: string, set?: Set<string>): Promise<Set<string>>;
12
- parseToken(raw: string): Promise<StringifiedUtil[] | null | undefined>;
14
+ makeContext(raw: string, applied: VariantMatchedResult): RuleContext<{}>;
15
+ parseToken(raw: string, alias?: string): Promise<StringifiedUtil[] | undefined>;
13
16
  generate(input: string | Set<string>, { id, scope, preflights, safelist, minify, }?: GenerateOptions): Promise<GenerateResult>;
14
17
  matchVariants(raw: string, current?: string): VariantMatchedResult;
15
18
  applyVariants(parsed: ParsedUtil, variantHandlers?: VariantHandler[], raw?: string): UtilObject;
@@ -23,6 +26,68 @@ declare class UnoGenerator {
23
26
  declare function createGenerator(config?: UserConfig, defaults?: UserConfigDefaults): UnoGenerator;
24
27
  declare const hasScopePlaceholder: (css: string) => RegExpMatchArray | null;
25
28
 
29
+ declare function escapeRegExp(string: string): string;
30
+ /**
31
+ * CSS Selector Escape
32
+ */
33
+ declare function escapeSelector(str: string): string;
34
+ declare const e: typeof escapeSelector;
35
+
36
+ declare function normalizeCSSEntries(obj: CSSEntries | CSSObject): CSSEntries;
37
+ declare function normalizeCSSValues(obj: CSSValues): CSSEntries[];
38
+ declare function clearIdenticalEntries(entry: CSSEntries): CSSEntries;
39
+ declare function entriesToCss(arr?: CSSEntries): string;
40
+ declare function isObject(item: any): item is Record<string, any>;
41
+ declare function mergeDeep<T>(original: T, patch: DeepPartial<T>): T;
42
+ declare function clone<T>(val: T): T;
43
+ declare function isStaticRule(rule: Rule): rule is StaticRule;
44
+ declare function isStaticShortcut(sc: Shortcut): sc is StaticShortcut;
45
+
46
+ declare function toArray<T>(value?: T | T[]): T[];
47
+ declare function uniq<T>(value: T[]): T[];
48
+ declare function mergeSet<T>(target: Set<T>, append: Set<T>): Set<T>;
49
+
50
+ declare const attributifyRE: RegExp;
51
+ declare const validateFilterRE: RegExp;
52
+ declare const CONTROL_SHORTCUT_NO_MERGE = "$$shortcut-no-merge";
53
+ declare function isAttributifySelector(selector: string): RegExpMatchArray | null;
54
+ declare function isValidSelector(selector?: string): selector is string;
55
+ declare function normalizeVariant(variant: Variant): VariantObject;
56
+ declare function isRawUtil(util: ParsedUtil | RawUtil | StringifiedUtil): util is RawUtil;
57
+ declare function notNull<T>(value: T | null | undefined): value is T;
58
+
59
+ declare class TwoKeyMap<K1, K2, V> {
60
+ _map: Map<K1, Map<K2, V>>;
61
+ get(key1: K1, key2: K2): V | undefined;
62
+ getFallback(key1: K1, key2: K2, fallback: V): V;
63
+ set(key1: K1, key2: K2, value: V): this;
64
+ has(key1: K1, key2: K2): boolean | undefined;
65
+ delete(key1: K1, key2: K2): boolean;
66
+ deleteTop(key1: K1): boolean;
67
+ map<T>(fn: (v: V, k1: K1, k2: K2) => T): T[];
68
+ }
69
+ declare class BetterMap<K, V> extends Map<K, V> {
70
+ map<R>(mapFn: (value: V, key: K) => R): R[];
71
+ }
72
+
73
+ declare function withLayer<T>(layer: string, rules: Rule<T>[]): Rule<T>[];
74
+
75
+ declare const regexClassGroup: RegExp;
76
+ declare function expandVariantGroup(str: string): string;
77
+
78
+ declare function warnOnce(msg: string): void;
79
+
80
+ declare type ValueHandlerCallback = (str: string) => string | number | undefined;
81
+ declare type ValueHandler<K extends string> = {
82
+ [S in K]: ValueHandler<K>;
83
+ } & {
84
+ (str: string): string | undefined;
85
+ __options: {
86
+ sequence: K[];
87
+ };
88
+ };
89
+ declare function createValueHandler<K extends string>(handlers: Record<K, ValueHandlerCallback>): ValueHandler<K>;
90
+
26
91
  declare type Awaitable<T> = T | Promise<T>;
27
92
  declare type Arrayable<T> = T | T[];
28
93
  declare type ArgumentType<T> = T extends ((...args: infer A) => any) ? A : never;
@@ -93,6 +158,10 @@ interface RuleContext<Theme extends {} = {}> {
93
158
  * Matched variants handlers for this rule.
94
159
  */
95
160
  variantHandlers: VariantHandler[];
161
+ /**
162
+ * The result of variant matching.
163
+ */
164
+ variantMatch: VariantMatchedResult;
96
165
  /**
97
166
  * Constrcut a custom CSS rule.
98
167
  * Variants and selector escaping will be handled automatically.
@@ -310,6 +379,48 @@ interface UserOnlyOptions<Theme extends {} = {}> {
310
379
  */
311
380
  envMode?: 'dev' | 'build';
312
381
  }
382
+ interface UnocssPluginContext<Config extends UserConfig = UserConfig> {
383
+ ready: Promise<LoadConfigResult<Config>>;
384
+ uno: UnoGenerator;
385
+ tokens: Set<string>;
386
+ modules: BetterMap<string, string>;
387
+ filter: (code: string, id: string) => boolean;
388
+ extract: (code: string, id?: string) => Promise<void>;
389
+ reloadConfig: () => Promise<LoadConfigResult<Config>>;
390
+ getConfig: () => Promise<Config>;
391
+ invalidate: () => void;
392
+ onInvalidate: (fn: () => void) => void;
393
+ }
394
+ interface SourceMap {
395
+ file?: string;
396
+ mappings?: string;
397
+ names?: string[];
398
+ sources?: string[];
399
+ sourcesContent?: string[];
400
+ version?: number;
401
+ }
402
+ interface TransformResult {
403
+ code: string;
404
+ map?: SourceMap | null;
405
+ etag?: string;
406
+ deps?: string[];
407
+ dynamicDeps?: string[];
408
+ }
409
+ interface SourceCodeTransformer {
410
+ name: string;
411
+ /**
412
+ * The order of transformer
413
+ */
414
+ enforce?: 'pre' | 'post';
415
+ /**
416
+ * Custom id filter, if not provided, the extraction filter will be applied
417
+ */
418
+ idFilter?: (id: string) => boolean;
419
+ /**
420
+ * The transform function
421
+ */
422
+ transform: (code: string, id: string, ctx: UnocssPluginContext) => Awaitable<string | TransformResult | null | undefined>;
423
+ }
313
424
  /**
314
425
  * For other modules to aggregate the options
315
426
  */
@@ -332,6 +443,12 @@ interface PluginOptions {
332
443
  * Patterns that filter the files NOT being extracted.
333
444
  */
334
445
  exclude?: FilterPattern;
446
+ /**
447
+ * Custom transformers to the source code
448
+ *
449
+ * Currently only supported in Vite
450
+ */
451
+ transformers?: SourceCodeTransformer[];
335
452
  }
336
453
  interface UserConfig<Theme extends {} = {}> extends ConfigBase<Theme>, UserOnlyOptions<Theme>, GeneratorOptions, PluginOptions {
337
454
  }
@@ -410,70 +527,8 @@ interface GenerateOptions {
410
527
  scope?: string;
411
528
  }
412
529
 
413
- declare function escapeRegExp(string: string): string;
414
- /**
415
- * CSS Selector Escape
416
- */
417
- declare function escapeSelector(str: string): string;
418
- declare const e: typeof escapeSelector;
419
-
420
- declare function normalizeCSSEntries(obj: CSSEntries | CSSObject): CSSEntries;
421
- declare function normalizeCSSValues(obj: CSSValues): CSSEntries[];
422
- declare function clearIdenticalEntries(entry: CSSEntries): CSSEntries;
423
- declare function entriesToCss(arr?: CSSEntries): string;
424
- declare function isObject(item: any): item is Record<string, any>;
425
- declare function mergeDeep<T>(original: T, patch: DeepPartial<T>): T;
426
- declare function clone<T>(val: T): T;
427
- declare function isStaticRule(rule: Rule): rule is StaticRule;
428
- declare function isStaticShortcut(sc: Shortcut): sc is StaticShortcut;
429
-
430
- declare function toArray<T>(value?: T | T[]): T[];
431
- declare function uniq<T>(value: T[]): T[];
432
- declare function mergeSet<T>(target: Set<T>, append: Set<T>): Set<T>;
433
-
434
- declare const attributifyRE: RegExp;
435
- declare const validateFilterRE: RegExp;
436
- declare const CONTROL_SHORTCUT_NO_MERGE = "$$shortcut-no-merge";
437
- declare function isAttributifySelector(selector: string): RegExpMatchArray | null;
438
- declare function isValidSelector(selector?: string): selector is string;
439
- declare function normalizeVariant(variant: Variant): VariantObject;
440
- declare function isRawUtil(util: ParsedUtil | RawUtil | StringifiedUtil): util is RawUtil;
441
- declare function notNull<T>(value: T | null | undefined): value is T;
442
-
443
- declare class TwoKeyMap<K1, K2, V> {
444
- _map: Map<K1, Map<K2, V>>;
445
- get(key1: K1, key2: K2): V | undefined;
446
- getFallback(key1: K1, key2: K2, fallback: V): V;
447
- set(key1: K1, key2: K2, value: V): this;
448
- has(key1: K1, key2: K2): boolean | undefined;
449
- delete(key1: K1, key2: K2): boolean;
450
- deleteTop(key1: K1): boolean;
451
- map<T>(fn: (v: V, k1: K1, k2: K2) => T): T[];
452
- }
453
- declare class BetterMap<K, V> extends Map<K, V> {
454
- map<R>(mapFn: (value: V, key: K) => R): R[];
455
- }
456
-
457
- declare function withLayer<T>(layer: string, rules: Rule<T>[]): Rule<T>[];
458
-
459
- declare const regexClassGroup: RegExp;
460
- declare function expandVariantGroup(str: string): string;
461
-
462
- declare function warnOnce(msg: string): void;
463
-
464
- declare type ValueHandlerCallback = (str: string) => string | number | undefined;
465
- declare type ValueHandler<K extends string> = {
466
- [S in K]: ValueHandler<K>;
467
- } & {
468
- (str: string): string | undefined;
469
- __options: {
470
- sequence: K[];
471
- };
472
- };
473
- declare function createValueHandler<K extends string>(handlers: Record<K, ValueHandlerCallback>): ValueHandler<K>;
474
-
475
530
  declare const extractorSplit: Extractor;
476
531
 
477
532
  declare const extractorSvelte: Extractor;
478
533
 
479
- 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 };
534
+ 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, SourceCodeTransformer, SourceMap, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, ThemeExtender, TransformResult, TwoKeyMap, UnoGenerator, UnocssPluginContext, 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
@@ -362,7 +362,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
362
362
  };
363
363
  }
364
364
 
365
- const version = "0.25.1";
365
+ const version = "0.26.0";
366
366
 
367
367
  class UnoGenerator {
368
368
  constructor(userConfig = {}, defaults = {}) {
@@ -399,48 +399,55 @@ class UnoGenerator {
399
399
  }
400
400
  return set;
401
401
  }
402
- async parseToken(raw) {
402
+ makeContext(raw, applied) {
403
+ const context = {
404
+ rawSelector: raw,
405
+ currentSelector: applied[1],
406
+ theme: this.config.theme,
407
+ generator: this,
408
+ variantHandlers: applied[2],
409
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args),
410
+ variantMatch: applied
411
+ };
412
+ return context;
413
+ }
414
+ async parseToken(raw, alias) {
403
415
  if (this.blocked.has(raw))
404
416
  return;
405
- if (this._cache.has(raw))
406
- return this._cache.get(raw);
417
+ const cacheKey = `${raw}${alias ? ` ${alias}` : ""}`;
418
+ const cached = this._cache.get(cacheKey);
419
+ if (cached)
420
+ return cached;
407
421
  let current = raw;
408
422
  for (const p of this.config.preprocess)
409
423
  current = p(raw);
410
424
  if (this.isBlocked(current)) {
411
425
  this.blocked.add(raw);
412
- this._cache.set(raw, null);
426
+ this._cache.set(cacheKey, null);
413
427
  return;
414
428
  }
415
429
  const applied = this.matchVariants(raw, current);
416
430
  if (!applied || this.isBlocked(applied[1])) {
417
431
  this.blocked.add(raw);
418
- this._cache.set(raw, null);
432
+ this._cache.set(cacheKey, null);
419
433
  return;
420
434
  }
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);
435
+ const context = this.makeContext(raw, [alias || applied[0], applied[1], applied[2]]);
436
+ const expanded = this.expandShortcut(context.currentSelector, context);
430
437
  if (expanded) {
431
- const utils = await this.stringifyShortcuts(applied, context, expanded[0], expanded[1]);
438
+ const utils = await this.stringifyShortcuts(context.variantMatch, context, expanded[0], expanded[1]);
432
439
  if (utils?.length) {
433
- this._cache.set(raw, utils);
440
+ this._cache.set(cacheKey, utils);
434
441
  return utils;
435
442
  }
436
443
  } else {
437
- const utils = (await this.parseUtil(applied, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
444
+ const utils = (await this.parseUtil(context.variantMatch, context))?.map((i) => this.stringifyUtil(i)).filter(notNull);
438
445
  if (utils?.length) {
439
- this._cache.set(raw, utils);
446
+ this._cache.set(cacheKey, utils);
440
447
  return utils;
441
448
  }
442
449
  }
443
- this._cache.set(raw, null);
450
+ this._cache.set(cacheKey, null);
444
451
  }
445
452
  async generate(input, {
446
453
  id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
- "version": "0.25.1",
3
+ "version": "0.26.0",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "keywords": [
6
6
  "unocss",