@unocss/core 0.19.0 → 0.20.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,13 +110,6 @@ 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
- }
120
113
 
121
114
  const hexRE = /^#?([\da-f]+)$/i;
122
115
  function hex2rgba(hex = "") {
@@ -360,6 +353,8 @@ function resolveConfig(userConfig = {}, defaults = {}) {
360
353
  rulesSize,
361
354
  rulesDynamic: rules,
362
355
  rulesStaticMap,
356
+ preprocess: mergePresets("preprocess"),
357
+ postprocess: mergePresets("postprocess"),
363
358
  preflights: mergePresets("preflights"),
364
359
  variants: mergePresets("variants").map(normalizeVariant),
365
360
  shortcuts: resolveShortcuts(mergePresets("shortcuts")),
@@ -368,7 +363,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
368
363
  };
369
364
  }
370
365
 
371
- const version = "0.19.0";
366
+ const version = "0.20.0";
372
367
 
373
368
  class UnoGenerator {
374
369
  constructor(userConfig = {}, defaults = {}) {
@@ -446,8 +441,8 @@ class UnoGenerator {
446
441
  return;
447
442
  }
448
443
  let current = raw;
449
- if (this.config.preprocess)
450
- current = this.config.preprocess(raw);
444
+ for (const p of this.config.preprocess)
445
+ current = p(raw);
451
446
  if (this.isBlocked(current))
452
447
  return block(current);
453
448
  const applied = this.matchVariants(raw, current);
@@ -485,7 +480,13 @@ class UnoGenerator {
485
480
  const getLayer = (layer) => {
486
481
  if (layerCache[layer])
487
482
  return layerCache[layer];
488
- let css = Array.from(sheet).sort((a, b) => (this.parentOrders.get(a[0]) || 0) - (this.parentOrders.get(b[0]) || 0)).map(([parent, items]) => {
483
+ let css = Array.from(sheet).sort((a, b) => {
484
+ const parentOrderA = this.parentOrders.get(a[0]);
485
+ const parentOrderB = this.parentOrders.get(b[0]);
486
+ if (parentOrderA !== void 0 && parentOrderB !== void 0)
487
+ return parentOrderA - parentOrderB;
488
+ return a[0]?.localeCompare(b[0] || "");
489
+ }).map(([parent, items]) => {
489
490
  const size = items.length;
490
491
  const sorted = items.filter((i) => (i[4]?.layer || "default") === layer).sort((a, b) => a[0] - b[0] || a[1]?.localeCompare(b[1] || "") || 0).map((a) => [a[1] ? applyScope(a[1], scope) : a[1], a[2]]);
491
492
  if (!sorted.length)
@@ -565,18 +566,21 @@ class UnoGenerator {
565
566
  }
566
567
  applyVariants(parsed, variantHandlers = parsed[4], raw = parsed[1]) {
567
568
  const entries = variantHandlers.reduce((p, v) => v.body?.(p) || p, parsed[2]);
568
- return [
569
- variantHandlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw)),
569
+ const obj = {
570
+ selector: variantHandlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw)),
570
571
  entries,
571
- variantHandlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0)
572
- ];
572
+ parent: variantHandlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0)
573
+ };
574
+ for (const p of this.config.postprocess)
575
+ p(obj);
576
+ return obj;
573
577
  }
574
578
  constructCustomCSS(context, body, overrideSelector) {
575
579
  body = normalizeCSSEntries(body);
576
- const [selector, entries, mediaQuery] = this.applyVariants([0, overrideSelector || context.rawSelector, body, void 0, context.variantHandlers]);
580
+ const { selector, entries, parent } = this.applyVariants([0, overrideSelector || context.rawSelector, body, void 0, context.variantHandlers]);
577
581
  const cssBody = `${selector}{${entriesToCss(entries)}}`;
578
- if (mediaQuery)
579
- return `${mediaQuery}{${cssBody}}`;
582
+ if (parent)
583
+ return `${parent}{${cssBody}}`;
580
584
  return cssBody;
581
585
  }
582
586
  async parseUtil(input, context, internal = false) {
@@ -613,11 +617,11 @@ class UnoGenerator {
613
617
  return;
614
618
  if (isRawUtil(parsed))
615
619
  return [parsed[0], void 0, parsed[1], void 0, parsed[2]];
616
- const [selector, entries, mediaQuery] = this.applyVariants(parsed);
620
+ const { selector, entries, parent } = this.applyVariants(parsed);
617
621
  const body = entriesToCss(entries);
618
622
  if (!body)
619
623
  return;
620
- return [parsed[0], selector, body, mediaQuery, parsed[3]];
624
+ return [parsed[0], selector, body, parent, parsed[3]];
621
625
  }
622
626
  expandShortcut(processed, context, depth = 3) {
623
627
  if (depth === 0)
@@ -662,8 +666,8 @@ class UnoGenerator {
662
666
  for (const item of parsed) {
663
667
  if (isRawUtil(item))
664
668
  continue;
665
- const [selector, entries, mediaQuery] = this.applyVariants(item, [...item[4], ...parentVariants], raw);
666
- const mapItem = selectorMap.getFallback(selector, mediaQuery, [[], item[0]]);
669
+ const { selector, entries, parent: parent2 } = this.applyVariants(item, [...item[4], ...parentVariants], raw);
670
+ const mapItem = selectorMap.getFallback(selector, parent2, [[], item[0]]);
667
671
  mapItem[0].push(...entries);
668
672
  if (item[0] > mapItem[1])
669
673
  mapItem[1] = item[0];
@@ -701,7 +705,6 @@ exports.BetterMap = BetterMap;
701
705
  exports.TwoKeyMap = TwoKeyMap;
702
706
  exports.UnoGenerator = UnoGenerator;
703
707
  exports.attributifyRE = attributifyRE;
704
- exports.cacheFunction = cacheFunction;
705
708
  exports.clearIdenticalEntries = clearIdenticalEntries;
706
709
  exports.createGenerator = createGenerator;
707
710
  exports.createValueHandler = createValueHandler;
package/dist/index.d.ts CHANGED
@@ -11,7 +11,7 @@ declare class UnoGenerator {
11
11
  applyExtractors(code: string, id?: string, set?: Set<string>): Promise<Set<string>>;
12
12
  generate(input: string | Set<string>, { id, scope, preflights, safelist, minify, }?: GenerateOptions): Promise<GenerateResult>;
13
13
  matchVariants(raw: string, current?: string): VariantMatchedResult;
14
- applyVariants(parsed: ParsedUtil, variantHandlers?: VariantHandler[], raw?: string): readonly [string, CSSEntries, string | undefined];
14
+ applyVariants(parsed: ParsedUtil, variantHandlers?: VariantHandler[], raw?: string): UtilObject;
15
15
  constructCustomCSS(context: Readonly<RuleContext>, body: CSSObject | CSSEntries, overrideSelector?: string): string;
16
16
  parseUtil(input: string | VariantMatchedResult, context: RuleContext, internal?: boolean): Promise<ParsedUtil[] | RawUtil[] | undefined>;
17
17
  stringifyUtil(parsed?: ParsedUtil | RawUtil): StringifiedUtil | undefined;
@@ -182,6 +182,8 @@ declare type VariantObject<Theme extends {} = {}> = {
182
182
  multiPass?: boolean;
183
183
  };
184
184
  declare type Variant<Theme extends {} = {}> = VariantFunction<Theme> | VariantObject<Theme>;
185
+ declare type Preprocessor = (matcher: string) => string | undefined;
186
+ declare type Postprocessor = (util: UtilObject) => void;
185
187
  interface ConfigBase<Theme extends {} = {}> {
186
188
  /**
187
189
  * Rules to generate CSS utilities
@@ -227,6 +229,14 @@ interface ConfigBase<Theme extends {} = {}> {
227
229
  * Custom function to sort layers.
228
230
  */
229
231
  sortLayers?: (layers: string[]) => string[];
232
+ /**
233
+ * Preprocess the incoming utilities, return falsy value to exclude
234
+ */
235
+ preprocess?: Preprocessor | Preprocessor[];
236
+ /**
237
+ * Process the generate utils object
238
+ */
239
+ postprocess?: Postprocessor | Postprocessor[];
230
240
  }
231
241
  interface Preset<Theme extends {} = {}> extends ConfigBase<Theme> {
232
242
  name: string;
@@ -234,7 +244,7 @@ interface Preset<Theme extends {} = {}> extends ConfigBase<Theme> {
234
244
  /**
235
245
  * Preset options for other tools like IDE to consume
236
246
  */
237
- options?: any;
247
+ options?: PresetOptions;
238
248
  }
239
249
  interface GeneratorOptions {
240
250
  /**
@@ -255,10 +265,6 @@ interface UserOnlyOptions<Theme extends {} = {}> {
255
265
  * The theme object, will be merged with the theme provides by presets
256
266
  */
257
267
  theme?: Theme;
258
- /**
259
- * Preprocess the incoming utilities, return falsy value to exclude
260
- */
261
- preprocess?: (matcher: string) => string | undefined;
262
268
  /**
263
269
  * Layout name of shortcuts
264
270
  *
@@ -306,6 +312,8 @@ interface UserConfigDefaults<Theme extends {} = {}> extends ConfigBase<Theme>, U
306
312
  interface ResolvedConfig extends Omit<RequiredByKey<UserConfig, 'mergeSelectors' | 'theme' | 'rules' | 'variants' | 'layers' | 'extractors' | 'blocklist' | 'safelist' | 'preflights' | 'sortLayers'>, 'rules' | 'shortcuts'> {
307
313
  shortcuts: Shortcut[];
308
314
  variants: VariantObject[];
315
+ preprocess: Preprocessor[];
316
+ postprocess: Postprocessor[];
309
317
  rulesSize: number;
310
318
  rulesDynamic: (DynamicRule | undefined)[];
311
319
  rulesStaticMap: Record<string, [number, CSSObject | CSSEntries, RuleMeta | undefined] | undefined>;
@@ -342,6 +350,11 @@ declare type StringifiedUtil = readonly [
342
350
  parent: string | undefined,
343
351
  meta: RuleMeta | undefined
344
352
  ];
353
+ interface UtilObject {
354
+ selector: string;
355
+ entries: CSSEntries;
356
+ parent: string | undefined;
357
+ }
345
358
  interface GenerateOptions {
346
359
  /**
347
360
  * Filepath of the file being processed.
@@ -387,9 +400,6 @@ declare function isStaticShortcut(sc: Shortcut): sc is StaticShortcut;
387
400
  declare function toArray<T>(value?: T | T[]): T[];
388
401
  declare function uniq<T>(value: T[]): T[];
389
402
  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;
393
403
 
394
404
  declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
395
405
 
@@ -437,4 +447,4 @@ declare const extractorSplit: Extractor;
437
447
 
438
448
  declare const extractorSvelte: Extractor;
439
449
 
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 };
450
+ 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, Postprocessor, Preflight, Preprocessor, Preset, PresetOptions, RGBAColorValue, RawUtil, RequiredByKey, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, UtilObject, ValueHandler, ValueHandlerCallback, Variant, VariantContext, 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 };
package/dist/index.mjs CHANGED
@@ -106,13 +106,6 @@ 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
- }
116
109
 
117
110
  const hexRE = /^#?([\da-f]+)$/i;
118
111
  function hex2rgba(hex = "") {
@@ -356,6 +349,8 @@ function resolveConfig(userConfig = {}, defaults = {}) {
356
349
  rulesSize,
357
350
  rulesDynamic: rules,
358
351
  rulesStaticMap,
352
+ preprocess: mergePresets("preprocess"),
353
+ postprocess: mergePresets("postprocess"),
359
354
  preflights: mergePresets("preflights"),
360
355
  variants: mergePresets("variants").map(normalizeVariant),
361
356
  shortcuts: resolveShortcuts(mergePresets("shortcuts")),
@@ -364,7 +359,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
364
359
  };
365
360
  }
366
361
 
367
- const version = "0.19.0";
362
+ const version = "0.20.0";
368
363
 
369
364
  class UnoGenerator {
370
365
  constructor(userConfig = {}, defaults = {}) {
@@ -442,8 +437,8 @@ class UnoGenerator {
442
437
  return;
443
438
  }
444
439
  let current = raw;
445
- if (this.config.preprocess)
446
- current = this.config.preprocess(raw);
440
+ for (const p of this.config.preprocess)
441
+ current = p(raw);
447
442
  if (this.isBlocked(current))
448
443
  return block(current);
449
444
  const applied = this.matchVariants(raw, current);
@@ -481,7 +476,13 @@ class UnoGenerator {
481
476
  const getLayer = (layer) => {
482
477
  if (layerCache[layer])
483
478
  return layerCache[layer];
484
- let css = Array.from(sheet).sort((a, b) => (this.parentOrders.get(a[0]) || 0) - (this.parentOrders.get(b[0]) || 0)).map(([parent, items]) => {
479
+ let css = Array.from(sheet).sort((a, b) => {
480
+ const parentOrderA = this.parentOrders.get(a[0]);
481
+ const parentOrderB = this.parentOrders.get(b[0]);
482
+ if (parentOrderA !== void 0 && parentOrderB !== void 0)
483
+ return parentOrderA - parentOrderB;
484
+ return a[0]?.localeCompare(b[0] || "");
485
+ }).map(([parent, items]) => {
485
486
  const size = items.length;
486
487
  const sorted = items.filter((i) => (i[4]?.layer || "default") === layer).sort((a, b) => a[0] - b[0] || a[1]?.localeCompare(b[1] || "") || 0).map((a) => [a[1] ? applyScope(a[1], scope) : a[1], a[2]]);
487
488
  if (!sorted.length)
@@ -561,18 +562,21 @@ class UnoGenerator {
561
562
  }
562
563
  applyVariants(parsed, variantHandlers = parsed[4], raw = parsed[1]) {
563
564
  const entries = variantHandlers.reduce((p, v) => v.body?.(p) || p, parsed[2]);
564
- return [
565
- variantHandlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw)),
565
+ const obj = {
566
+ selector: variantHandlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw)),
566
567
  entries,
567
- variantHandlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0)
568
- ];
568
+ parent: variantHandlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0)
569
+ };
570
+ for (const p of this.config.postprocess)
571
+ p(obj);
572
+ return obj;
569
573
  }
570
574
  constructCustomCSS(context, body, overrideSelector) {
571
575
  body = normalizeCSSEntries(body);
572
- const [selector, entries, mediaQuery] = this.applyVariants([0, overrideSelector || context.rawSelector, body, void 0, context.variantHandlers]);
576
+ const { selector, entries, parent } = this.applyVariants([0, overrideSelector || context.rawSelector, body, void 0, context.variantHandlers]);
573
577
  const cssBody = `${selector}{${entriesToCss(entries)}}`;
574
- if (mediaQuery)
575
- return `${mediaQuery}{${cssBody}}`;
578
+ if (parent)
579
+ return `${parent}{${cssBody}}`;
576
580
  return cssBody;
577
581
  }
578
582
  async parseUtil(input, context, internal = false) {
@@ -609,11 +613,11 @@ class UnoGenerator {
609
613
  return;
610
614
  if (isRawUtil(parsed))
611
615
  return [parsed[0], void 0, parsed[1], void 0, parsed[2]];
612
- const [selector, entries, mediaQuery] = this.applyVariants(parsed);
616
+ const { selector, entries, parent } = this.applyVariants(parsed);
613
617
  const body = entriesToCss(entries);
614
618
  if (!body)
615
619
  return;
616
- return [parsed[0], selector, body, mediaQuery, parsed[3]];
620
+ return [parsed[0], selector, body, parent, parsed[3]];
617
621
  }
618
622
  expandShortcut(processed, context, depth = 3) {
619
623
  if (depth === 0)
@@ -658,8 +662,8 @@ class UnoGenerator {
658
662
  for (const item of parsed) {
659
663
  if (isRawUtil(item))
660
664
  continue;
661
- const [selector, entries, mediaQuery] = this.applyVariants(item, [...item[4], ...parentVariants], raw);
662
- const mapItem = selectorMap.getFallback(selector, mediaQuery, [[], item[0]]);
665
+ const { selector, entries, parent: parent2 } = this.applyVariants(item, [...item[4], ...parentVariants], raw);
666
+ const mapItem = selectorMap.getFallback(selector, parent2, [[], item[0]]);
663
667
  mapItem[0].push(...entries);
664
668
  if (item[0] > mapItem[1])
665
669
  mapItem[1] = item[0];
@@ -693,4 +697,4 @@ function toEscapedSelector(raw) {
693
697
  return `.${e(raw)}`;
694
698
  }
695
699
 
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 };
700
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "keywords": [
6
6
  "unocss",