@unocss/core 0.20.2 → 0.21.2

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
@@ -93,6 +93,32 @@ function mergeDeep(original, patch) {
93
93
  }
94
94
  return output;
95
95
  }
96
+ function clone(val) {
97
+ let k, out, tmp;
98
+ if (Array.isArray(val)) {
99
+ out = Array(k = val.length);
100
+ while (k--)
101
+ out[k] = (tmp = val[k]) && typeof tmp === "object" ? clone(tmp) : tmp;
102
+ return out;
103
+ }
104
+ if (Object.prototype.toString.call(val) === "[object Object]") {
105
+ out = {};
106
+ for (k in val) {
107
+ if (k === "__proto__") {
108
+ Object.defineProperty(out, k, {
109
+ value: clone(val[k]),
110
+ configurable: true,
111
+ enumerable: true,
112
+ writable: true
113
+ });
114
+ } else {
115
+ out[k] = (tmp = val[k]) && typeof tmp === "object" ? clone(tmp) : tmp;
116
+ }
117
+ }
118
+ return out;
119
+ }
120
+ return val;
121
+ }
96
122
  function isStaticRule(rule) {
97
123
  return typeof rule[0] === "string";
98
124
  }
@@ -249,7 +275,6 @@ function createValueHandler(handlers) {
249
275
  if (res != null)
250
276
  return res;
251
277
  }
252
- return void 0;
253
278
  };
254
279
  function addProcessor(that, name) {
255
280
  if (!that.__options) {
@@ -333,11 +358,11 @@ function resolveConfig(userConfig = {}, defaults = {}) {
333
358
  delete rules[i];
334
359
  }
335
360
  });
336
- const theme = [
361
+ const theme = clone([
337
362
  ...sortedPresets.map((p) => p.theme || {}),
338
363
  config.theme || {}
339
- ].reduce((a, p) => mergeDeep(a, p), {});
340
- const options = Object.assign({}, ...config.presets?.map((p) => Array.isArray(p) ? p : [p]).flat(1).map((p) => p.options ?? {}) || []);
364
+ ].reduce((a, p) => mergeDeep(a, p), {}));
365
+ mergePresets("extendTheme").forEach((extendTheme) => extendTheme(theme));
341
366
  return {
342
367
  mergeSelectors: true,
343
368
  warn: true,
@@ -358,12 +383,11 @@ function resolveConfig(userConfig = {}, defaults = {}) {
358
383
  preflights: mergePresets("preflights"),
359
384
  variants: mergePresets("variants").map(normalizeVariant),
360
385
  shortcuts: resolveShortcuts(mergePresets("shortcuts")),
361
- extractors,
362
- options
386
+ extractors
363
387
  };
364
388
  }
365
389
 
366
- const version = "0.20.2";
390
+ const version = "0.21.2";
367
391
 
368
392
  class UnoGenerator {
369
393
  constructor(userConfig = {}, defaults = {}) {
@@ -392,8 +416,7 @@ class UnoGenerator {
392
416
  return code;
393
417
  },
394
418
  code,
395
- id,
396
- options: this.config.options
419
+ id
397
420
  };
398
421
  for (const extractor of this.config.extractors) {
399
422
  const result = await extractor.extract(context);
@@ -454,8 +477,7 @@ class UnoGenerator {
454
477
  theme: this.config.theme,
455
478
  generator: this,
456
479
  variantHandlers: applied[2],
457
- constructCSS: (...args) => this.constructCustomCSS(context, ...args),
458
- options: this.config.options
480
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args)
459
481
  };
460
482
  const expanded = this.expandShortcut(applied[1], context);
461
483
  if (expanded) {
@@ -480,13 +502,7 @@ class UnoGenerator {
480
502
  const getLayer = (layer) => {
481
503
  if (layerCache[layer])
482
504
  return layerCache[layer];
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]) => {
505
+ 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]) => {
490
506
  const size = items.length;
491
507
  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]]);
492
508
  if (!sorted.length)
@@ -496,7 +512,7 @@ class UnoGenerator {
496
512
  for (let i = idx + 1; i < size; i++) {
497
513
  const current = sorted[i];
498
514
  if (current && current[0] && current[1] === body) {
499
- current[0] = `${current[0]},${selector}`;
515
+ current[0] = `${current[0]},${nl}${selector}`;
500
516
  return null;
501
517
  }
502
518
  }
@@ -534,8 +550,7 @@ class UnoGenerator {
534
550
  const context = {
535
551
  rawSelector: raw,
536
552
  theme: this.config.theme,
537
- generator: this,
538
- options: this.config.options
553
+ generator: this
539
554
  };
540
555
  while (true) {
541
556
  applied = false;
@@ -707,6 +722,7 @@ exports.TwoKeyMap = TwoKeyMap;
707
722
  exports.UnoGenerator = UnoGenerator;
708
723
  exports.attributifyRE = attributifyRE;
709
724
  exports.clearIdenticalEntries = clearIdenticalEntries;
725
+ exports.clone = clone;
710
726
  exports.createGenerator = createGenerator;
711
727
  exports.createValueHandler = createValueHandler;
712
728
  exports.e = e;
package/dist/index.d.ts CHANGED
@@ -23,6 +23,7 @@ declare function createGenerator(config?: UserConfig, defaults?: UserConfigDefau
23
23
  declare const hasScopePlaceholder: (css: string) => RegExpMatchArray | null;
24
24
 
25
25
  declare type Awaitable<T> = T | Promise<T>;
26
+ declare type Arrayable<T> = T | T[];
26
27
  declare type ArgumentType<T> = T extends ((...args: infer A) => any) ? A : never;
27
28
  declare type Shift<T> = T extends [_: any, ...args: infer A] ? A : never;
28
29
  declare type RestArgs<T> = Shift<ArgumentType<T>>;
@@ -91,10 +92,6 @@ interface RuleContext<Theme extends {} = {}> {
91
92
  * Variants and selector escaping will be handled automatically.
92
93
  */
93
94
  constructCSS: (body: CSSEntries | CSSObject, overrideSelector?: string) => string;
94
- /**
95
- * User-provided options from preset.
96
- */
97
- readonly options: PresetOptions;
98
95
  }
99
96
  interface VariantContext<Theme extends {} = {}> {
100
97
  /**
@@ -109,16 +106,11 @@ interface VariantContext<Theme extends {} = {}> {
109
106
  * The theme object
110
107
  */
111
108
  theme: Theme;
112
- /**
113
- * User-provided options from preset.
114
- */
115
- readonly options: PresetOptions;
116
109
  }
117
110
  interface ExtractorContext {
118
111
  readonly original: string;
119
112
  code: string;
120
113
  id?: string;
121
- readonly options: PresetOptions;
122
114
  }
123
115
  interface Extractor {
124
116
  name: string;
@@ -192,6 +184,7 @@ declare type VariantObject<Theme extends {} = {}> = {
192
184
  declare type Variant<Theme extends {} = {}> = VariantFunction<Theme> | VariantObject<Theme>;
193
185
  declare type Preprocessor = (matcher: string) => string | undefined;
194
186
  declare type Postprocessor = (util: UtilObject) => void;
187
+ declare type ThemeExtender<T> = (theme: T) => void;
195
188
  interface ConfigBase<Theme extends {} = {}> {
196
189
  /**
197
190
  * Rules to generate CSS utilities
@@ -240,11 +233,15 @@ interface ConfigBase<Theme extends {} = {}> {
240
233
  /**
241
234
  * Preprocess the incoming utilities, return falsy value to exclude
242
235
  */
243
- preprocess?: Preprocessor | Preprocessor[];
236
+ preprocess?: Arrayable<Preprocessor>;
244
237
  /**
245
238
  * Process the generate utils object
246
239
  */
247
- postprocess?: Postprocessor | Postprocessor[];
240
+ postprocess?: Arrayable<Postprocessor>;
241
+ /**
242
+ * Custom functions to extend the theme object
243
+ */
244
+ extendTheme?: Arrayable<ThemeExtender<Theme>>;
248
245
  }
249
246
  interface Preset<Theme extends {} = {}> extends ConfigBase<Theme> {
250
247
  name: string;
@@ -325,7 +322,6 @@ interface ResolvedConfig extends Omit<RequiredByKey<UserConfig, 'mergeSelectors'
325
322
  rulesSize: number;
326
323
  rulesDynamic: (DynamicRule | undefined)[];
327
324
  rulesStaticMap: Record<string, [number, CSSObject | CSSEntries, RuleMeta | undefined] | undefined>;
328
- options: PresetOptions;
329
325
  }
330
326
  interface GenerateResult {
331
327
  css: string;
@@ -402,6 +398,7 @@ declare function clearIdenticalEntries(entry: CSSEntries): CSSEntries;
402
398
  declare function entriesToCss(arr?: CSSEntries): string;
403
399
  declare function isObject(item: any): item is Record<string, any>;
404
400
  declare function mergeDeep<T>(original: T, patch: DeepPartial<T>): T;
401
+ declare function clone<T>(val: T): T;
405
402
  declare function isStaticRule(rule: Rule): rule is StaticRule;
406
403
  declare function isStaticShortcut(sc: Shortcut): sc is StaticShortcut;
407
404
 
@@ -455,4 +452,4 @@ declare const extractorSplit: Extractor;
455
452
 
456
453
  declare const extractorSvelte: Extractor;
457
454
 
458
- 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 };
455
+ export { ArgumentType, Arrayable, 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, 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, hex2rgba, isAttributifySelector, isObject, isRawUtil, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeCSSEntries, normalizeCSSValues, normalizeVariant, notNull, regexClassGroup, toArray, uniq, validateFilterRE, warnOnce, withLayer };
package/dist/index.mjs CHANGED
@@ -89,6 +89,32 @@ function mergeDeep(original, patch) {
89
89
  }
90
90
  return output;
91
91
  }
92
+ function clone(val) {
93
+ let k, out, tmp;
94
+ if (Array.isArray(val)) {
95
+ out = Array(k = val.length);
96
+ while (k--)
97
+ out[k] = (tmp = val[k]) && typeof tmp === "object" ? clone(tmp) : tmp;
98
+ return out;
99
+ }
100
+ if (Object.prototype.toString.call(val) === "[object Object]") {
101
+ out = {};
102
+ for (k in val) {
103
+ if (k === "__proto__") {
104
+ Object.defineProperty(out, k, {
105
+ value: clone(val[k]),
106
+ configurable: true,
107
+ enumerable: true,
108
+ writable: true
109
+ });
110
+ } else {
111
+ out[k] = (tmp = val[k]) && typeof tmp === "object" ? clone(tmp) : tmp;
112
+ }
113
+ }
114
+ return out;
115
+ }
116
+ return val;
117
+ }
92
118
  function isStaticRule(rule) {
93
119
  return typeof rule[0] === "string";
94
120
  }
@@ -245,7 +271,6 @@ function createValueHandler(handlers) {
245
271
  if (res != null)
246
272
  return res;
247
273
  }
248
- return void 0;
249
274
  };
250
275
  function addProcessor(that, name) {
251
276
  if (!that.__options) {
@@ -329,11 +354,11 @@ function resolveConfig(userConfig = {}, defaults = {}) {
329
354
  delete rules[i];
330
355
  }
331
356
  });
332
- const theme = [
357
+ const theme = clone([
333
358
  ...sortedPresets.map((p) => p.theme || {}),
334
359
  config.theme || {}
335
- ].reduce((a, p) => mergeDeep(a, p), {});
336
- const options = Object.assign({}, ...config.presets?.map((p) => Array.isArray(p) ? p : [p]).flat(1).map((p) => p.options ?? {}) || []);
360
+ ].reduce((a, p) => mergeDeep(a, p), {}));
361
+ mergePresets("extendTheme").forEach((extendTheme) => extendTheme(theme));
337
362
  return {
338
363
  mergeSelectors: true,
339
364
  warn: true,
@@ -354,12 +379,11 @@ function resolveConfig(userConfig = {}, defaults = {}) {
354
379
  preflights: mergePresets("preflights"),
355
380
  variants: mergePresets("variants").map(normalizeVariant),
356
381
  shortcuts: resolveShortcuts(mergePresets("shortcuts")),
357
- extractors,
358
- options
382
+ extractors
359
383
  };
360
384
  }
361
385
 
362
- const version = "0.20.2";
386
+ const version = "0.21.2";
363
387
 
364
388
  class UnoGenerator {
365
389
  constructor(userConfig = {}, defaults = {}) {
@@ -388,8 +412,7 @@ class UnoGenerator {
388
412
  return code;
389
413
  },
390
414
  code,
391
- id,
392
- options: this.config.options
415
+ id
393
416
  };
394
417
  for (const extractor of this.config.extractors) {
395
418
  const result = await extractor.extract(context);
@@ -450,8 +473,7 @@ class UnoGenerator {
450
473
  theme: this.config.theme,
451
474
  generator: this,
452
475
  variantHandlers: applied[2],
453
- constructCSS: (...args) => this.constructCustomCSS(context, ...args),
454
- options: this.config.options
476
+ constructCSS: (...args) => this.constructCustomCSS(context, ...args)
455
477
  };
456
478
  const expanded = this.expandShortcut(applied[1], context);
457
479
  if (expanded) {
@@ -476,13 +498,7 @@ class UnoGenerator {
476
498
  const getLayer = (layer) => {
477
499
  if (layerCache[layer])
478
500
  return layerCache[layer];
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]) => {
501
+ 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]) => {
486
502
  const size = items.length;
487
503
  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]]);
488
504
  if (!sorted.length)
@@ -492,7 +508,7 @@ class UnoGenerator {
492
508
  for (let i = idx + 1; i < size; i++) {
493
509
  const current = sorted[i];
494
510
  if (current && current[0] && current[1] === body) {
495
- current[0] = `${current[0]},${selector}`;
511
+ current[0] = `${current[0]},${nl}${selector}`;
496
512
  return null;
497
513
  }
498
514
  }
@@ -530,8 +546,7 @@ class UnoGenerator {
530
546
  const context = {
531
547
  rawSelector: raw,
532
548
  theme: this.config.theme,
533
- generator: this,
534
- options: this.config.options
549
+ generator: this
535
550
  };
536
551
  while (true) {
537
552
  applied = false;
@@ -698,4 +713,4 @@ function toEscapedSelector(raw) {
698
713
  return `.${e(raw)}`;
699
714
  }
700
715
 
701
- 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 };
716
+ export { BetterMap, TwoKeyMap, UnoGenerator, attributifyRE, clearIdenticalEntries, clone, 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.20.2",
3
+ "version": "0.21.2",
4
4
  "description": "The instant on-demand Atomic CSS engine.",
5
5
  "keywords": [
6
6
  "unocss",