@unocss/core 0.18.1 → 0.19.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
@@ -110,6 +110,13 @@ function mergeSet(target, append) {
110
110
  append.forEach((i) => target.add(i));
111
111
  return target;
112
112
  }
113
+ function cacheFunction(fn) {
114
+ const cache = Object.create(null);
115
+ return (str) => {
116
+ const hit = cache[str];
117
+ return hit || (cache[str] = fn(str));
118
+ };
119
+ }
113
120
 
114
121
  const hexRE = /^#?([\da-f]+)$/i;
115
122
  function hex2rgba(hex = "") {
@@ -337,6 +344,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
337
344
  ...sortedPresets.map((p) => p.theme || {}),
338
345
  config.theme || {}
339
346
  ].reduce((a, p) => mergeDeep(a, p), {});
347
+ const options = Object.assign({}, ...config.presets?.map((p) => Array.isArray(p) ? p : [p]).flat(1).map((p) => p.options ?? {}) || []);
340
348
  return {
341
349
  mergeSelectors: true,
342
350
  warn: true,
@@ -355,11 +363,12 @@ function resolveConfig(userConfig = {}, defaults = {}) {
355
363
  preflights: mergePresets("preflights"),
356
364
  variants: mergePresets("variants").map(normalizeVariant),
357
365
  shortcuts: resolveShortcuts(mergePresets("shortcuts")),
358
- extractors
366
+ extractors,
367
+ options
359
368
  };
360
369
  }
361
370
 
362
- const version = "0.18.1";
371
+ const version = "0.19.0";
363
372
 
364
373
  class UnoGenerator {
365
374
  constructor(userConfig = {}, defaults = {}) {
@@ -388,7 +397,8 @@ class UnoGenerator {
388
397
  return code;
389
398
  },
390
399
  code,
391
- id
400
+ id,
401
+ options: this.config.options
392
402
  };
393
403
  for (const extractor of this.config.extractors) {
394
404
  const result = await extractor.extract(context);
@@ -449,7 +459,8 @@ class UnoGenerator {
449
459
  theme: this.config.theme,
450
460
  generator: this,
451
461
  variantHandlers: applied[2],
452
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
462
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args),
463
+ options: this.config.options
453
464
  };
454
465
  const expanded = this.expandShortcut(applied[1], context);
455
466
  if (expanded) {
@@ -519,12 +530,18 @@ class UnoGenerator {
519
530
  const handlers = [];
520
531
  let processed = current || raw;
521
532
  let applied = false;
533
+ const context = {
534
+ rawSelector: raw,
535
+ theme: this.config.theme,
536
+ generator: this,
537
+ options: this.config.options
538
+ };
522
539
  while (true) {
523
540
  applied = false;
524
541
  for (const v of this.config.variants) {
525
542
  if (!v.multiPass && usedVariants.has(v))
526
543
  continue;
527
- let handler = v.match(processed, raw, this.config.theme);
544
+ let handler = v.match(processed, context);
528
545
  if (!handler)
529
546
  continue;
530
547
  if (typeof handler === "string")
@@ -684,6 +701,7 @@ exports.BetterMap = BetterMap;
684
701
  exports.TwoKeyMap = TwoKeyMap;
685
702
  exports.UnoGenerator = UnoGenerator;
686
703
  exports.attributifyRE = attributifyRE;
704
+ exports.cacheFunction = cacheFunction;
687
705
  exports.clearIdenticalEntries = clearIdenticalEntries;
688
706
  exports.createGenerator = createGenerator;
689
707
  exports.createValueHandler = createValueHandler;
package/dist/index.d.ts CHANGED
@@ -59,6 +59,7 @@ declare type ParsedColorValue = {
59
59
  */
60
60
  rgba?: RGBAColorValue;
61
61
  };
62
+ declare type PresetOptions = Record<string, any>;
62
63
  interface RuleContext<Theme extends {} = {}> {
63
64
  /**
64
65
  * Unprocessed selector from user input.
@@ -86,11 +87,34 @@ interface RuleContext<Theme extends {} = {}> {
86
87
  * Variants and selector escaping will be handled automatically.
87
88
  */
88
89
  constructCSS: (body: CSSEntries | CSSObject, overrideSelector?: string) => string;
90
+ /**
91
+ * User-provided options from preset.
92
+ */
93
+ readonly options: PresetOptions;
94
+ }
95
+ interface VariantContext<Theme extends {} = {}> {
96
+ /**
97
+ * Unprocessed selector from user input.
98
+ */
99
+ rawSelector: string;
100
+ /**
101
+ * UnoCSS generator instance
102
+ */
103
+ generator: UnoGenerator;
104
+ /**
105
+ * The theme object
106
+ */
107
+ theme: Theme;
108
+ /**
109
+ * User-provided options from preset.
110
+ */
111
+ readonly options: PresetOptions;
89
112
  }
90
113
  interface ExtractorContext {
91
114
  readonly original: string;
92
115
  code: string;
93
116
  id?: string;
117
+ readonly options: PresetOptions;
94
118
  }
95
119
  interface Extractor {
96
120
  name: string;
@@ -144,7 +168,7 @@ interface VariantHandler {
144
168
  */
145
169
  parent?: string | [string, number] | undefined;
146
170
  }
147
- declare type VariantFunction<Theme extends {} = {}> = (matcher: string, raw: string, theme: Theme) => string | VariantHandler | undefined;
171
+ declare type VariantFunction<Theme extends {} = {}> = (matcher: string, context: Readonly<VariantContext<Theme>>) => string | VariantHandler | undefined;
148
172
  declare type VariantObject<Theme extends {} = {}> = {
149
173
  /**
150
174
  * The entry function to match and rewrite the selector for futher processing.
@@ -285,6 +309,7 @@ interface ResolvedConfig extends Omit<RequiredByKey<UserConfig, 'mergeSelectors'
285
309
  rulesSize: number;
286
310
  rulesDynamic: (DynamicRule | undefined)[];
287
311
  rulesStaticMap: Record<string, [number, CSSObject | CSSEntries, RuleMeta | undefined] | undefined>;
312
+ options: PresetOptions;
288
313
  }
289
314
  interface GenerateResult {
290
315
  css: string;
@@ -362,6 +387,9 @@ declare function isStaticShortcut(sc: Shortcut): sc is StaticShortcut;
362
387
  declare function toArray<T>(value?: T | T[]): T[];
363
388
  declare function uniq<T>(value: T[]): T[];
364
389
  declare function mergeSet<T>(target: Set<T>, append: Set<T>): Set<T>;
390
+ declare function cacheFunction<T extends (str: string) => R, R extends string | {
391
+ readonly [key: string]: string | number;
392
+ }>(fn: T): T;
365
393
 
366
394
  declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
367
395
 
@@ -409,4 +437,4 @@ declare const extractorSplit: Extractor;
409
437
 
410
438
  declare const extractorSvelte: Extractor;
411
439
 
412
- export { ArgumentType, Awaitable, BetterMap, BlocklistRule, CSSEntries, CSSObject, CSSValues, ConfigBase, DeepPartial, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, Extractor, ExtractorContext, FilterPattern, FlatObjectTuple, GenerateOptions, GenerateResult, GeneratorOptions, ParsedColorValue, ParsedUtil, PartialByKeys, PluginOptions, Preflight, Preset, RGBAColorValue, RawUtil, RequiredByKey, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, ValueHandler, ValueHandlerCallback, Variant, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, clearIdenticalEntries, 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 };
440
+ export { ArgumentType, Awaitable, BetterMap, BlocklistRule, CSSEntries, CSSObject, CSSValues, ConfigBase, DeepPartial, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, Extractor, ExtractorContext, FilterPattern, FlatObjectTuple, GenerateOptions, GenerateResult, GeneratorOptions, ParsedColorValue, ParsedUtil, PartialByKeys, PluginOptions, Preflight, Preset, PresetOptions, RGBAColorValue, RawUtil, RequiredByKey, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, ValueHandler, ValueHandlerCallback, Variant, VariantContext, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, cacheFunction, clearIdenticalEntries, 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 };
package/dist/index.mjs CHANGED
@@ -106,6 +106,13 @@ function mergeSet(target, append) {
106
106
  append.forEach((i) => target.add(i));
107
107
  return target;
108
108
  }
109
+ function cacheFunction(fn) {
110
+ const cache = Object.create(null);
111
+ return (str) => {
112
+ const hit = cache[str];
113
+ return hit || (cache[str] = fn(str));
114
+ };
115
+ }
109
116
 
110
117
  const hexRE = /^#?([\da-f]+)$/i;
111
118
  function hex2rgba(hex = "") {
@@ -333,6 +340,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
333
340
  ...sortedPresets.map((p) => p.theme || {}),
334
341
  config.theme || {}
335
342
  ].reduce((a, p) => mergeDeep(a, p), {});
343
+ const options = Object.assign({}, ...config.presets?.map((p) => Array.isArray(p) ? p : [p]).flat(1).map((p) => p.options ?? {}) || []);
336
344
  return {
337
345
  mergeSelectors: true,
338
346
  warn: true,
@@ -351,11 +359,12 @@ function resolveConfig(userConfig = {}, defaults = {}) {
351
359
  preflights: mergePresets("preflights"),
352
360
  variants: mergePresets("variants").map(normalizeVariant),
353
361
  shortcuts: resolveShortcuts(mergePresets("shortcuts")),
354
- extractors
362
+ extractors,
363
+ options
355
364
  };
356
365
  }
357
366
 
358
- const version = "0.18.1";
367
+ const version = "0.19.0";
359
368
 
360
369
  class UnoGenerator {
361
370
  constructor(userConfig = {}, defaults = {}) {
@@ -384,7 +393,8 @@ class UnoGenerator {
384
393
  return code;
385
394
  },
386
395
  code,
387
- id
396
+ id,
397
+ options: this.config.options
388
398
  };
389
399
  for (const extractor of this.config.extractors) {
390
400
  const result = await extractor.extract(context);
@@ -445,7 +455,8 @@ class UnoGenerator {
445
455
  theme: this.config.theme,
446
456
  generator: this,
447
457
  variantHandlers: applied[2],
448
- constructCSS: (...args) => this.constructCustomCSS(context, ...args)
458
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args),
459
+ options: this.config.options
449
460
  };
450
461
  const expanded = this.expandShortcut(applied[1], context);
451
462
  if (expanded) {
@@ -515,12 +526,18 @@ class UnoGenerator {
515
526
  const handlers = [];
516
527
  let processed = current || raw;
517
528
  let applied = false;
529
+ const context = {
530
+ rawSelector: raw,
531
+ theme: this.config.theme,
532
+ generator: this,
533
+ options: this.config.options
534
+ };
518
535
  while (true) {
519
536
  applied = false;
520
537
  for (const v of this.config.variants) {
521
538
  if (!v.multiPass && usedVariants.has(v))
522
539
  continue;
523
- let handler = v.match(processed, raw, this.config.theme);
540
+ let handler = v.match(processed, context);
524
541
  if (!handler)
525
542
  continue;
526
543
  if (typeof handler === "string")
@@ -676,4 +693,4 @@ function toEscapedSelector(raw) {
676
693
  return `.${e(raw)}`;
677
694
  }
678
695
 
679
- export { BetterMap, TwoKeyMap, UnoGenerator, attributifyRE, clearIdenticalEntries, 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 };
696
+ export { BetterMap, TwoKeyMap, UnoGenerator, attributifyRE, cacheFunction, clearIdenticalEntries, 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "keywords": [
6
6
  "unocss",