@unocss/core 0.38.2 → 0.39.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
@@ -40,6 +40,8 @@ function escapeSelector(str) {
40
40
  const e = escapeSelector;
41
41
 
42
42
  function normalizeCSSEntries(obj) {
43
+ if (typeof obj === "string")
44
+ return obj;
43
45
  return (!Array.isArray(obj) ? Object.entries(obj) : obj).filter((i) => i[1] != null);
44
46
  }
45
47
  function normalizeCSSValues(obj) {
@@ -458,7 +460,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
458
460
  };
459
461
  }
460
462
 
461
- const version = "0.38.2";
463
+ const version = "0.39.0";
462
464
 
463
465
  class UnoGenerator {
464
466
  constructor(userConfig = {}, defaults = {}) {
@@ -612,20 +614,29 @@ class UnoGenerator {
612
614
  return layerCache[layer];
613
615
  let css = Array.from(sheet).sort((a, b) => (this.parentOrders.get(a[0]) ?? 0) - (this.parentOrders.get(b[0]) ?? 0) || a[0]?.localeCompare(b[0] || "") || 0).map(([parent, items]) => {
614
616
  const size = items.length;
615
- const sorted = items.filter((i) => (i[4]?.layer || "default") === layer).sort((a, b) => a[0] - b[0] || (a[4]?.sort || 0) - (b[4]?.sort || 0) || a[1]?.localeCompare(b[1] || "") || 0).map((a) => [a[1] ? applyScope(a[1], scope) : a[1], a[2], !!a[4]?.noMerge]).map((a) => [a[0] == null ? a[0] : [a[0]], a[1], a[2]]);
617
+ const sorted = items.filter((i) => (i[4]?.layer || "default") === layer).sort((a, b) => a[0] - b[0] || (a[4]?.sort || 0) - (b[4]?.sort || 0) || a[1]?.localeCompare(b[1] || "") || a[2]?.localeCompare(b[2] || "") || 0).map(([, selector, body, , meta]) => {
618
+ const scopedSelector = selector ? applyScope(selector, scope) : selector;
619
+ return [
620
+ [[scopedSelector ?? "", meta?.sort ?? 0]],
621
+ body,
622
+ !!meta?.noMerge
623
+ ];
624
+ });
616
625
  if (!sorted.length)
617
626
  return void 0;
618
- const rules = sorted.reverse().map(([selector, body, noMerge], idx) => {
619
- if (!noMerge && selector && this.config.mergeSelectors) {
627
+ const rules = sorted.reverse().map(([selectorSortPair, body, noMerge], idx) => {
628
+ if (!noMerge && this.config.mergeSelectors) {
620
629
  for (let i = idx + 1; i < size; i++) {
621
630
  const current = sorted[i];
622
- if (current && !current[2] && current[0] && current[1] === body) {
623
- current[0].push(...selector);
631
+ if (current && !current[2] && (selectorSortPair && current[0] || selectorSortPair == null && current[0] == null) && current[1] === body) {
632
+ if (selectorSortPair && current[0])
633
+ current[0].push(...selectorSortPair);
624
634
  return null;
625
635
  }
626
636
  }
627
637
  }
628
- return selector ? `${[...new Set(selector)].join(`,${nl}`)}{${body}}` : body;
638
+ const selectors = selectorSortPair ? [...new Set(selectorSortPair.sort((a, b) => a[1] - b[1] || a[0]?.localeCompare(b[0] || "") || 0).map((pair) => pair[0]).filter(Boolean))] : [];
639
+ return selectors.length ? `${selectors.join(`,${nl}`)}{${body}}` : body;
629
640
  }).filter(Boolean).reverse().join(nl);
630
641
  return parent ? `${parent}{${nl}${rules}${nl}}` : rules;
631
642
  }).filter(Boolean).join(nl);
@@ -684,22 +695,34 @@ class UnoGenerator {
684
695
  }
685
696
  applyVariants(parsed, variantHandlers = parsed[4], raw = parsed[1]) {
686
697
  const handlers = [...variantHandlers].sort((a, b) => (a.order || 0) - (b.order || 0));
687
- const entries = handlers.reduce((p, v) => v.body?.(p) || p, parsed[2]);
688
- const selector = handlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw));
698
+ let entries = parsed[2];
699
+ let selector = toEscapedSelector(raw);
700
+ let parent;
701
+ let layer;
702
+ let sort;
703
+ handlers.forEach((v) => {
704
+ entries = v.body?.(entries) || entries;
705
+ selector = v.selector?.(selector, entries) || selector;
706
+ parent = Array.isArray(v.parent) ? v.parent[0] : v.parent || parent;
707
+ layer = v.layer || layer;
708
+ sort = v.sort || sort;
709
+ });
689
710
  const obj = {
690
711
  selector: movePseudoElementsEnd(selector),
691
712
  entries,
692
- parent: handlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0),
693
- layer: handlers.reduce((p, v) => v.layer || p, void 0),
694
- sort: handlers.reduce((p, v) => v.sort || p, void 0)
713
+ parent,
714
+ layer,
715
+ sort
695
716
  };
696
717
  for (const p of this.config.postprocess)
697
718
  p(obj);
698
719
  return obj;
699
720
  }
700
721
  constructCustomCSS(context, body, overrideSelector) {
701
- body = normalizeCSSEntries(body);
702
- const { selector, entries, parent } = this.applyVariants([0, overrideSelector || context.rawSelector, body, void 0, context.variantHandlers]);
722
+ const normalizedBody = normalizeCSSEntries(body);
723
+ if (typeof normalizedBody === "string")
724
+ return normalizedBody;
725
+ const { selector, entries, parent } = this.applyVariants([0, overrideSelector || context.rawSelector, normalizedBody, void 0, context.variantHandlers]);
703
726
  const cssBody = `${selector}{${entriesToCss(entries)}}`;
704
727
  if (parent)
705
728
  return `${parent}{${cssBody}}`;
@@ -715,7 +738,13 @@ class UnoGenerator {
715
738
  if (staticMatch) {
716
739
  if (staticMatch[1] && (internal || !staticMatch[2]?.internal)) {
717
740
  recordRule(staticMatch[3]);
718
- return [[staticMatch[0], raw, normalizeCSSEntries(staticMatch[1]), staticMatch[2], variantHandlers]];
741
+ const index = staticMatch[0];
742
+ const entry = normalizeCSSEntries(staticMatch[1]);
743
+ const meta = staticMatch[2];
744
+ if (typeof entry === "string")
745
+ return [[index, entry, meta]];
746
+ else
747
+ return [[index, raw, entry, meta, variantHandlers]];
719
748
  }
720
749
  }
721
750
  context.variantHandlers = variantHandlers;
@@ -734,11 +763,15 @@ class UnoGenerator {
734
763
  if (!result)
735
764
  continue;
736
765
  recordRule(rule);
737
- if (typeof result === "string")
738
- return [[i, result, meta]];
739
766
  const entries = normalizeCSSValues(result).filter((i2) => i2.length);
740
- if (entries.length)
741
- return entries.map((e2) => [i, raw, e2, meta, variantHandlers]);
767
+ if (entries.length) {
768
+ return entries.map((e2) => {
769
+ if (typeof e2 === "string")
770
+ return [i, e2, meta];
771
+ else
772
+ return [i, raw, e2, meta, variantHandlers];
773
+ });
774
+ }
742
775
  }
743
776
  }
744
777
  stringifyUtil(parsed, context) {
package/dist/index.d.ts CHANGED
@@ -71,7 +71,7 @@ declare class UnoGenerator {
71
71
  matchVariants(raw: string, current?: string): VariantMatchedResult;
72
72
  applyVariants(parsed: ParsedUtil, variantHandlers?: VariantHandler[], raw?: string): UtilObject;
73
73
  constructCustomCSS(context: Readonly<RuleContext>, body: CSSObject | CSSEntries, overrideSelector?: string): string;
74
- parseUtil(input: string | VariantMatchedResult, context: RuleContext, internal?: boolean): Promise<ParsedUtil[] | RawUtil[] | undefined>;
74
+ parseUtil(input: string | VariantMatchedResult, context: RuleContext, internal?: boolean): Promise<(ParsedUtil | RawUtil)[] | undefined>;
75
75
  stringifyUtil(parsed?: ParsedUtil | RawUtil, context?: RuleContext): StringifiedUtil | undefined;
76
76
  expandShortcut(processed: string, context: RuleContext, depth?: number): [string[], RuleMeta | undefined] | undefined;
77
77
  stringifyShortcuts(parent: VariantMatchedResult, context: RuleContext, expanded: string[], meta?: RuleMeta): Promise<StringifiedUtil[] | undefined>;
@@ -90,8 +90,8 @@ declare function escapeRegExp(string: string): string;
90
90
  declare function escapeSelector(str: string): string;
91
91
  declare const e: typeof escapeSelector;
92
92
 
93
- declare function normalizeCSSEntries(obj: CSSEntries | CSSObject): CSSEntries;
94
- declare function normalizeCSSValues(obj: CSSValues): CSSEntries[];
93
+ declare function normalizeCSSEntries(obj: string | CSSEntries | CSSObject): string | CSSEntries;
94
+ declare function normalizeCSSValues(obj: CSSValue | string | (CSSValue | string)[]): (string | CSSEntries)[];
95
95
  declare function clearIdenticalEntries(entry: CSSEntries): CSSEntries;
96
96
  declare function entriesToCss(arr?: CSSEntries): string;
97
97
  declare function isObject(item: any): item is Record<string, any>;
@@ -315,8 +315,9 @@ interface RuleMeta {
315
315
  */
316
316
  internal?: boolean;
317
317
  }
318
- declare type CSSValues = CSSObject | CSSEntries | (CSSObject | CSSEntries)[];
319
- declare type DynamicMatcher<Theme extends {} = {}> = ((match: RegExpMatchArray, context: Readonly<RuleContext<Theme>>) => Awaitable<CSSValues | string | undefined>);
318
+ declare type CSSValue = CSSObject | CSSEntries;
319
+ declare type CSSValues = CSSValue | CSSValue[];
320
+ declare type DynamicMatcher<Theme extends {} = {}> = ((match: RegExpMatchArray, context: Readonly<RuleContext<Theme>>) => Awaitable<CSSValue | string | (CSSValue | string)[] | undefined>);
320
321
  declare type DynamicRule<Theme extends {} = {}> = [RegExp, DynamicMatcher<Theme>] | [RegExp, DynamicMatcher<Theme>, RuleMeta];
321
322
  declare type StaticRule = [string, CSSObject | CSSEntries] | [string, CSSObject | CSSEntries, RuleMeta];
322
323
  declare type Rule<Theme extends {} = {}> = DynamicRule<Theme> | StaticRule;
@@ -688,6 +689,11 @@ declare type StringifiedUtil = readonly [
688
689
  meta: RuleMeta | undefined,
689
690
  context: RuleContext | undefined
690
691
  ];
692
+ declare type PreparedRule = readonly [
693
+ selector: [string, number][],
694
+ body: string,
695
+ noMerge: boolean
696
+ ];
691
697
  interface UtilObject {
692
698
  selector: string;
693
699
  entries: CSSEntries;
@@ -725,4 +731,4 @@ declare const extractorSplit: Extractor;
725
731
 
726
732
  declare const extractorSvelte: Extractor;
727
733
 
728
- export { ArgumentType, Arrayable, AutoCompleteExtractor, AutoCompleteExtractorContext, AutoCompleteExtractorResult, AutoCompleteFunction, AutoCompleteTemplate, Awaitable, BetterMap, BlocklistRule, CONTROL_SHORTCUT_NO_MERGE, CSSColorValue, CSSEntries, CSSObject, CSSValues, ConfigBase, DeepPartial, DetailString, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, ExtractStringOptions, Extractor, ExtractorContext, FilterPattern, FlatObjectTuple, GenerateOptions, GenerateResult, GeneratorOptions, ParsedColorValue, ParsedUtil, PartialByKeys, PluginOptions, Postprocessor, Preflight, PreflightContext, Preprocessor, Preset, PresetOptions, RGBAColorValue, Range, RawUtil, Replacement, RequiredByKey, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, SourceCodeTransformer, SourceMap, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, SuggestResult, ThemeExtender, TransformResult, TwoKeyMap, UnoGenerator, UnocssPluginContext, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, UtilObject, ValueHandler, ValueHandlerCallback, Variant, VariantContext, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, clearIdenticalEntries, clone, createGenerator, createValueHandler, cssIdRE, e, entriesToCss, escapeRegExp, escapeSelector, expandVariantGroup, extractQuoted, extractorSplit, extractorSvelte, hasScopePlaceholder, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, matchingPair, mergeDeep, mergeSet, movePseudoElementsEnd, noop, normalizeCSSEntries, normalizeCSSValues, normalizeVariant, notNull, regexClassGroup, regexScopePlaceholder, toArray, toEscapedSelector, uniq, validateFilterRE, warnOnce, withLayer };
734
+ export { ArgumentType, Arrayable, AutoCompleteExtractor, AutoCompleteExtractorContext, AutoCompleteExtractorResult, AutoCompleteFunction, AutoCompleteTemplate, Awaitable, BetterMap, BlocklistRule, CONTROL_SHORTCUT_NO_MERGE, CSSColorValue, CSSEntries, CSSObject, CSSValue, CSSValues, ConfigBase, DeepPartial, DetailString, DynamicMatcher, DynamicRule, DynamicShortcut, DynamicShortcutMatcher, ExtractStringOptions, Extractor, ExtractorContext, FilterPattern, FlatObjectTuple, GenerateOptions, GenerateResult, GeneratorOptions, ParsedColorValue, ParsedUtil, PartialByKeys, PluginOptions, Postprocessor, Preflight, PreflightContext, PreparedRule, Preprocessor, Preset, PresetOptions, RGBAColorValue, Range, RawUtil, Replacement, RequiredByKey, ResolvedConfig, RestArgs, Rule, RuleContext, RuleMeta, Shift, Shortcut, SourceCodeTransformer, SourceMap, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, SuggestResult, ThemeExtender, TransformResult, TwoKeyMap, UnoGenerator, UnocssPluginContext, UserConfig, UserConfigDefaults, UserOnlyOptions, UserShortcuts, UtilObject, ValueHandler, ValueHandlerCallback, Variant, VariantContext, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, clearIdenticalEntries, clone, createGenerator, createValueHandler, cssIdRE, e, entriesToCss, escapeRegExp, escapeSelector, expandVariantGroup, extractQuoted, extractorSplit, extractorSvelte, hasScopePlaceholder, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, matchingPair, mergeDeep, mergeSet, movePseudoElementsEnd, noop, normalizeCSSEntries, normalizeCSSValues, normalizeVariant, notNull, regexClassGroup, regexScopePlaceholder, toArray, toEscapedSelector, uniq, validateFilterRE, warnOnce, withLayer };
package/dist/index.mjs CHANGED
@@ -36,6 +36,8 @@ function escapeSelector(str) {
36
36
  const e = escapeSelector;
37
37
 
38
38
  function normalizeCSSEntries(obj) {
39
+ if (typeof obj === "string")
40
+ return obj;
39
41
  return (!Array.isArray(obj) ? Object.entries(obj) : obj).filter((i) => i[1] != null);
40
42
  }
41
43
  function normalizeCSSValues(obj) {
@@ -454,7 +456,7 @@ function resolveConfig(userConfig = {}, defaults = {}) {
454
456
  };
455
457
  }
456
458
 
457
- const version = "0.38.2";
459
+ const version = "0.39.0";
458
460
 
459
461
  class UnoGenerator {
460
462
  constructor(userConfig = {}, defaults = {}) {
@@ -608,20 +610,29 @@ class UnoGenerator {
608
610
  return layerCache[layer];
609
611
  let css = Array.from(sheet).sort((a, b) => (this.parentOrders.get(a[0]) ?? 0) - (this.parentOrders.get(b[0]) ?? 0) || a[0]?.localeCompare(b[0] || "") || 0).map(([parent, items]) => {
610
612
  const size = items.length;
611
- const sorted = items.filter((i) => (i[4]?.layer || "default") === layer).sort((a, b) => a[0] - b[0] || (a[4]?.sort || 0) - (b[4]?.sort || 0) || a[1]?.localeCompare(b[1] || "") || 0).map((a) => [a[1] ? applyScope(a[1], scope) : a[1], a[2], !!a[4]?.noMerge]).map((a) => [a[0] == null ? a[0] : [a[0]], a[1], a[2]]);
613
+ const sorted = items.filter((i) => (i[4]?.layer || "default") === layer).sort((a, b) => a[0] - b[0] || (a[4]?.sort || 0) - (b[4]?.sort || 0) || a[1]?.localeCompare(b[1] || "") || a[2]?.localeCompare(b[2] || "") || 0).map(([, selector, body, , meta]) => {
614
+ const scopedSelector = selector ? applyScope(selector, scope) : selector;
615
+ return [
616
+ [[scopedSelector ?? "", meta?.sort ?? 0]],
617
+ body,
618
+ !!meta?.noMerge
619
+ ];
620
+ });
612
621
  if (!sorted.length)
613
622
  return void 0;
614
- const rules = sorted.reverse().map(([selector, body, noMerge], idx) => {
615
- if (!noMerge && selector && this.config.mergeSelectors) {
623
+ const rules = sorted.reverse().map(([selectorSortPair, body, noMerge], idx) => {
624
+ if (!noMerge && this.config.mergeSelectors) {
616
625
  for (let i = idx + 1; i < size; i++) {
617
626
  const current = sorted[i];
618
- if (current && !current[2] && current[0] && current[1] === body) {
619
- current[0].push(...selector);
627
+ if (current && !current[2] && (selectorSortPair && current[0] || selectorSortPair == null && current[0] == null) && current[1] === body) {
628
+ if (selectorSortPair && current[0])
629
+ current[0].push(...selectorSortPair);
620
630
  return null;
621
631
  }
622
632
  }
623
633
  }
624
- return selector ? `${[...new Set(selector)].join(`,${nl}`)}{${body}}` : body;
634
+ const selectors = selectorSortPair ? [...new Set(selectorSortPair.sort((a, b) => a[1] - b[1] || a[0]?.localeCompare(b[0] || "") || 0).map((pair) => pair[0]).filter(Boolean))] : [];
635
+ return selectors.length ? `${selectors.join(`,${nl}`)}{${body}}` : body;
625
636
  }).filter(Boolean).reverse().join(nl);
626
637
  return parent ? `${parent}{${nl}${rules}${nl}}` : rules;
627
638
  }).filter(Boolean).join(nl);
@@ -680,22 +691,34 @@ class UnoGenerator {
680
691
  }
681
692
  applyVariants(parsed, variantHandlers = parsed[4], raw = parsed[1]) {
682
693
  const handlers = [...variantHandlers].sort((a, b) => (a.order || 0) - (b.order || 0));
683
- const entries = handlers.reduce((p, v) => v.body?.(p) || p, parsed[2]);
684
- const selector = handlers.reduce((p, v) => v.selector?.(p, entries) || p, toEscapedSelector(raw));
694
+ let entries = parsed[2];
695
+ let selector = toEscapedSelector(raw);
696
+ let parent;
697
+ let layer;
698
+ let sort;
699
+ handlers.forEach((v) => {
700
+ entries = v.body?.(entries) || entries;
701
+ selector = v.selector?.(selector, entries) || selector;
702
+ parent = Array.isArray(v.parent) ? v.parent[0] : v.parent || parent;
703
+ layer = v.layer || layer;
704
+ sort = v.sort || sort;
705
+ });
685
706
  const obj = {
686
707
  selector: movePseudoElementsEnd(selector),
687
708
  entries,
688
- parent: handlers.reduce((p, v) => Array.isArray(v.parent) ? v.parent[0] : v.parent || p, void 0),
689
- layer: handlers.reduce((p, v) => v.layer || p, void 0),
690
- sort: handlers.reduce((p, v) => v.sort || p, void 0)
709
+ parent,
710
+ layer,
711
+ sort
691
712
  };
692
713
  for (const p of this.config.postprocess)
693
714
  p(obj);
694
715
  return obj;
695
716
  }
696
717
  constructCustomCSS(context, body, overrideSelector) {
697
- body = normalizeCSSEntries(body);
698
- const { selector, entries, parent } = this.applyVariants([0, overrideSelector || context.rawSelector, body, void 0, context.variantHandlers]);
718
+ const normalizedBody = normalizeCSSEntries(body);
719
+ if (typeof normalizedBody === "string")
720
+ return normalizedBody;
721
+ const { selector, entries, parent } = this.applyVariants([0, overrideSelector || context.rawSelector, normalizedBody, void 0, context.variantHandlers]);
699
722
  const cssBody = `${selector}{${entriesToCss(entries)}}`;
700
723
  if (parent)
701
724
  return `${parent}{${cssBody}}`;
@@ -711,7 +734,13 @@ class UnoGenerator {
711
734
  if (staticMatch) {
712
735
  if (staticMatch[1] && (internal || !staticMatch[2]?.internal)) {
713
736
  recordRule(staticMatch[3]);
714
- return [[staticMatch[0], raw, normalizeCSSEntries(staticMatch[1]), staticMatch[2], variantHandlers]];
737
+ const index = staticMatch[0];
738
+ const entry = normalizeCSSEntries(staticMatch[1]);
739
+ const meta = staticMatch[2];
740
+ if (typeof entry === "string")
741
+ return [[index, entry, meta]];
742
+ else
743
+ return [[index, raw, entry, meta, variantHandlers]];
715
744
  }
716
745
  }
717
746
  context.variantHandlers = variantHandlers;
@@ -730,11 +759,15 @@ class UnoGenerator {
730
759
  if (!result)
731
760
  continue;
732
761
  recordRule(rule);
733
- if (typeof result === "string")
734
- return [[i, result, meta]];
735
762
  const entries = normalizeCSSValues(result).filter((i2) => i2.length);
736
- if (entries.length)
737
- return entries.map((e2) => [i, raw, e2, meta, variantHandlers]);
763
+ if (entries.length) {
764
+ return entries.map((e2) => {
765
+ if (typeof e2 === "string")
766
+ return [i, e2, meta];
767
+ else
768
+ return [i, raw, e2, meta, variantHandlers];
769
+ });
770
+ }
738
771
  }
739
772
  }
740
773
  stringifyUtil(parsed, context) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
- "version": "0.38.2",
3
+ "version": "0.39.0",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",