@unocss/core 66.7.2 → 66.7.4-beta.1

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.mts CHANGED
@@ -190,7 +190,7 @@ declare class UnoGeneratorInternal<Theme extends object = object> {
190
190
  parseUtil(input: string | VariantMatchedResult<Theme>, context: RuleContext<Theme>, internal?: boolean, shortcutPrefix?: string | string[] | undefined): Promise<(ParsedUtil | RawUtil)[] | undefined>;
191
191
  private resolveCSSResult;
192
192
  stringifyUtil(parsed?: ParsedUtil | RawUtil, context?: RuleContext<Theme>): StringifiedUtil<Theme>[] | undefined;
193
- expandShortcut(input: string, context: RuleContext<Theme>, depth?: number): Promise<[(string | ShortcutInlineValue)[], RuleMeta | undefined] | undefined>;
193
+ expandShortcut(input: string, context: RuleContext<Theme>, depth?: number, skipVariantMatch?: boolean): Promise<[(string | ShortcutInlineValue)[], RuleMeta | undefined] | undefined>;
194
194
  stringifyShortcuts(parent: VariantMatchedResult<Theme>, context: RuleContext<Theme>, expanded: (string | ShortcutInlineValue)[], meta?: RuleMeta): Promise<StringifiedUtil<Theme>[] | undefined>;
195
195
  isBlocked(raw: string): boolean;
196
196
  getBlocked(raw: string): [BlocklistValue, BlocklistMeta | undefined] | undefined;
@@ -917,6 +917,12 @@ interface SourceCodeTransformer {
917
917
  * Custom id filter, if not provided, the extraction filter will be applied
918
918
  */
919
919
  idFilter?: (id: string) => boolean;
920
+ /**
921
+ * Cheap source filter evaluated before creating a MagicString instance.
922
+ *
923
+ * It must return true for every source the transformer may modify.
924
+ */
925
+ codeFilter?: (code: string, id: string) => boolean;
920
926
  /**
921
927
  * The transform function
922
928
  */
@@ -1014,6 +1020,14 @@ interface ResolvedConfig<Theme extends object = object> extends Omit<RequiredByK
1014
1020
  rulesSize: number;
1015
1021
  rules: readonly Rule<Theme>[];
1016
1022
  rulesDynamic: readonly DynamicRule<Theme>[];
1023
+ /**
1024
+ * Prefilter for dynamic rules.
1025
+ * @internal
1026
+ */
1027
+ rulesDynamicFilter?: {
1028
+ fallback: readonly DynamicRule<Theme>[];
1029
+ filters: readonly RegExp[];
1030
+ };
1017
1031
  rulesStaticMap: Record<string, StaticRule | undefined>;
1018
1032
  autocomplete: {
1019
1033
  templates: (AutoCompleteFunction | AutoCompleteTemplate)[];
package/dist/index.mjs CHANGED
@@ -423,6 +423,31 @@ function resolveShortcuts(shortcuts) {
423
423
  return Object.entries(s);
424
424
  });
425
425
  }
426
+ function createDynamicRuleFilters(rules) {
427
+ const filterSources = /* @__PURE__ */ new Map();
428
+ const fallback = [];
429
+ for (const rule of rules) {
430
+ const [matcher, , meta] = rule;
431
+ if (meta?.prefix || matcher.global || matcher.sticky || /\\(?:[1-9]|k<)|\(\?</.test(matcher.source)) {
432
+ fallback.push(rule);
433
+ continue;
434
+ }
435
+ const sources = filterSources.get(matcher.flags) ?? [];
436
+ sources.push(`(?:${matcher.source})`);
437
+ filterSources.set(matcher.flags, sources);
438
+ }
439
+ try {
440
+ return {
441
+ fallback,
442
+ filters: Array.from(filterSources, ([flags, sources]) => new RegExp(sources.join("|"), flags))
443
+ };
444
+ } catch {
445
+ return {
446
+ fallback: [...rules],
447
+ filters: []
448
+ };
449
+ }
450
+ }
426
451
  const __RESOLVED = "_uno_resolved";
427
452
  /**
428
453
  * Resolve a single preset, nested presets are ignored
@@ -553,6 +578,7 @@ async function resolveConfig(userConfig = {}, defaults = {}) {
553
578
  const extendThemes = getMerged("extendTheme");
554
579
  for (const extendTheme of extendThemes) resolved.theme = extendTheme(resolved.theme, resolved) || resolved.theme;
555
580
  for (const p of sources) p?.configResolved?.(resolved);
581
+ resolved.rulesDynamicFilter = createDynamicRuleFilters(resolved.rulesDynamic);
556
582
  return resolved;
557
583
  }
558
584
  /**
@@ -602,7 +628,7 @@ function definePreset(preset) {
602
628
  }
603
629
  //#endregion
604
630
  //#region package.json
605
- var version = "66.7.2";
631
+ var version = "66.7.4-beta.1";
606
632
  //#endregion
607
633
  //#region src/generator.ts
608
634
  const symbols = {
@@ -616,6 +642,7 @@ const symbols = {
616
642
  sort: "$$symbol-sort",
617
643
  body: "$$symbol-body"
618
644
  };
645
+ const TOKEN_PARSE_BATCH_SIZE = 4096;
619
646
  var UnoGeneratorInternal = class UnoGeneratorInternal {
620
647
  userConfig;
621
648
  defaults;
@@ -700,7 +727,7 @@ var UnoGeneratorInternal = class UnoGeneratorInternal {
700
727
  matched[3]
701
728
  ]);
702
729
  if (this.config.details) context.variants = [...matched[3]];
703
- const expanded = await this.expandShortcut(context.currentSelector, context);
730
+ const expanded = await this.expandShortcut(context.currentSelector, context, 5, true);
704
731
  return expanded ? await this.stringifyShortcuts(context.variantMatch, context, expanded[0], expanded[1]) : (await this.parseUtil(context.variantMatch, context))?.flatMap((i) => this.stringifyUtil(i, context)).filter(notNull);
705
732
  };
706
733
  const result = (await Promise.all(variantResults.map((i) => handleVariantResult(i)))).flat().filter((x) => !!x);
@@ -724,28 +751,31 @@ var UnoGeneratorInternal = class UnoGeneratorInternal {
724
751
  });
725
752
  }
726
753
  const nl = minify ? "" : "\n";
727
- const layerSet = new Set([LAYER_DEFAULT]);
754
+ const layerSet = /* @__PURE__ */ new Set([LAYER_DEFAULT]);
728
755
  const matched = extendedInfo ? /* @__PURE__ */ new Map() : /* @__PURE__ */ new Set();
729
756
  const sheet = /* @__PURE__ */ new Map();
730
757
  let preflightsMap = {};
731
- const tokenPromises = Array.from(tokens).map(async (raw) => {
732
- if (matched.has(raw)) return;
733
- const payload = await this.parseToken(raw);
734
- if (payload == null) return;
735
- if (matched instanceof Map) matched.set(raw, {
736
- data: payload,
737
- count: isCountableSet(tokens) ? tokens.getCount(raw) : -1
758
+ const tokenList = Array.from(tokens);
759
+ for (let offset = 0; offset < tokenList.length; offset += TOKEN_PARSE_BATCH_SIZE) {
760
+ const tokenPromises = tokenList.slice(offset, offset + TOKEN_PARSE_BATCH_SIZE).map(async (raw) => {
761
+ if (matched.has(raw)) return;
762
+ const payload = await this.parseToken(raw);
763
+ if (payload == null) return;
764
+ if (matched instanceof Map) matched.set(raw, {
765
+ data: payload,
766
+ count: isCountableSet(tokens) ? tokens.getCount(raw) : -1
767
+ });
768
+ else matched.add(raw);
769
+ for (const item of payload) {
770
+ const parent = item[3] || "";
771
+ const layer = item[4]?.layer;
772
+ if (!sheet.has(parent)) sheet.set(parent, []);
773
+ sheet.get(parent).push(item);
774
+ if (layer) layerSet.add(layer);
775
+ }
738
776
  });
739
- else matched.add(raw);
740
- for (const item of payload) {
741
- const parent = item[3] || "";
742
- const layer = item[4]?.layer;
743
- if (!sheet.has(parent)) sheet.set(parent, []);
744
- sheet.get(parent).push(item);
745
- if (layer) layerSet.add(layer);
746
- }
747
- });
748
- await Promise.all(tokenPromises);
777
+ await Promise.all(tokenPromises);
778
+ }
749
779
  await (async () => {
750
780
  if (!preflights) return;
751
781
  const preflightContext = {
@@ -964,7 +994,9 @@ var UnoGeneratorInternal = class UnoGeneratorInternal {
964
994
  if (staticMatch) {
965
995
  if (staticMatch[1] && (internal || !staticMatch[2]?.internal)) return this.resolveCSSResult(raw, staticMatch[1], staticMatch, scopeContext);
966
996
  }
967
- for (const rule of this.config.rulesDynamic) {
997
+ const dynamicRuleFilter = this.config.rulesDynamicFilter;
998
+ const dynamicRules = dynamicRuleFilter?.filters.length && !dynamicRuleFilter.filters.some((filter) => filter.test(processed)) ? dynamicRuleFilter.fallback : this.config.rulesDynamic;
999
+ for (const rule of dynamicRules) {
968
1000
  const [matcher, handler, meta] = rule;
969
1001
  if (meta?.internal && !internal) continue;
970
1002
  let unprefixed = processed;
@@ -1093,7 +1125,7 @@ var UnoGeneratorInternal = class UnoGeneratorInternal {
1093
1125
  }
1094
1126
  return result;
1095
1127
  }
1096
- async expandShortcut(input, context, depth = 5) {
1128
+ async expandShortcut(input, context, depth = 5, skipVariantMatch = false) {
1097
1129
  if (depth === 0) return;
1098
1130
  const recordShortcut = this.config.details ? (s) => {
1099
1131
  context.shortcuts = context.shortcuts ?? [];
@@ -1134,7 +1166,7 @@ var UnoGeneratorInternal = class UnoGeneratorInternal {
1134
1166
  value: i
1135
1167
  }));
1136
1168
  }
1137
- if (!result) {
1169
+ if (!result && !skipVariantMatch) {
1138
1170
  const matched = isString(input) ? await this.matchVariants(input) : [input];
1139
1171
  for (const match of matched) {
1140
1172
  const [raw, inputWithoutVariant, handles] = match;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/core",
3
3
  "type": "module",
4
- "version": "66.7.2",
4
+ "version": "66.7.4-beta.1",
5
5
  "description": "The instant on-demand Atomic CSS engine.",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -37,6 +37,7 @@
37
37
  "unconfig": "^7.5.0"
38
38
  },
39
39
  "scripts": {
40
+ "bench": "pnpm --dir ../.. exec vitest bench packages-engine/core/test/generate.bench.ts --run --project unit",
40
41
  "build": "tsdown",
41
42
  "dev": "tsdown --watch"
42
43
  }