@pandacss/token-dictionary 0.29.1 → 0.30.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.mjs CHANGED
@@ -1,9 +1,13 @@
1
- // src/create-dictionary.ts
2
- import { getDotPath as getDotPath2, mapToJson as mapToJson2, memo as memo2 } from "@pandacss/shared";
3
-
4
1
  // src/dictionary.ts
5
- import { compact, isString, mapObject, memo, walkObject as walkObject2 } from "@pandacss/shared";
6
- import { isMatching as isMatching2, match } from "ts-pattern";
2
+ import {
3
+ compact,
4
+ cssVar,
5
+ isString as isString2,
6
+ mapObject,
7
+ memo,
8
+ walkObject as walkObject2
9
+ } from "@pandacss/shared";
10
+ import { isMatching as isMatching2, match as match2 } from "ts-pattern";
7
11
 
8
12
  // src/is-composite.ts
9
13
  import { P, isMatching } from "ts-pattern";
@@ -39,13 +43,16 @@ var isCompositeTokenValue = (value) => {
39
43
  return isCompositeGradient(value) || isCompositeShadow(value) || isCompositeBorder(value) || isCompositeAsset(value) || Array.isArray(value);
40
44
  };
41
45
 
46
+ // src/middleware.ts
47
+ import { calc, toPx } from "@pandacss/shared";
48
+
42
49
  // src/token.ts
43
50
  import { isBaseCondition, toHash, walkObject } from "@pandacss/shared";
44
51
 
45
52
  // src/utils.ts
46
53
  import { logger } from "@pandacss/logger";
47
54
  import { PandaError, esc, isObject } from "@pandacss/shared";
48
- var REFERENCE_REGEX = /(\$[^\s,]+\w)|({([^}]*)})/g;
55
+ var REFERENCE_REGEX = /({([^}]*)})/g;
49
56
  var curlyBracketRegex = /[{}]/g;
50
57
  function getReferences(value) {
51
58
  if (typeof value !== "string")
@@ -274,308 +281,17 @@ var TOKEN_TYPES = {
274
281
  aspectRatios: "aspectRatio"
275
282
  };
276
283
 
277
- // src/dictionary.ts
278
- function expandBreakpoints(breakpoints) {
279
- if (!breakpoints)
280
- return { breakpoints: {}, sizes: {} };
281
- return {
282
- breakpoints: mapObject(breakpoints, (value) => ({ value })),
283
- sizes: Object.fromEntries(Object.entries(breakpoints).map(([key, value]) => [`breakpoint-${key}`, { value }]))
284
- };
285
- }
286
- function filterDefault(path) {
287
- if (path[0] === "DEFAULT")
288
- return path;
289
- return path.filter((item) => item !== "DEFAULT");
290
- }
291
- var TokenDictionary = class {
292
- allTokens = [];
293
- prefix;
294
- hash;
295
- get allNames() {
296
- return Array.from(new Set(this.allTokens.map((token) => token.name)));
297
- }
298
- constructor(options) {
299
- const { tokens = {}, semanticTokens = {}, breakpoints, prefix, hash } = options;
300
- const breakpointTokens = expandBreakpoints(breakpoints);
301
- const computedTokens = compact({
302
- ...tokens,
303
- breakpoints: breakpointTokens.breakpoints,
304
- sizes: {
305
- ...tokens.sizes,
306
- ...breakpointTokens.sizes
307
- }
308
- });
309
- this.prefix = prefix;
310
- this.hash = hash;
311
- walkObject2(
312
- computedTokens,
313
- (token, path) => {
314
- path = filterDefault(path);
315
- assertTokenFormat(token);
316
- const category = path[0];
317
- const name = path.join(".");
318
- const node = new Token({ ...token, name, path });
319
- node.setExtensions({
320
- category,
321
- prop: path.slice(1).join(".")
322
- });
323
- this.allTokens.push(node);
324
- },
325
- { stop: isToken }
326
- );
327
- walkObject2(
328
- semanticTokens,
329
- (token, path) => {
330
- path = filterDefault(path);
331
- assertTokenFormat(token);
332
- const category = path[0];
333
- const name = path.join(".");
334
- const normalizedToken = isString(token.value) || isCompositeTokenValue(token.value) ? { value: { base: token.value } } : token;
335
- const { value, ...restData } = normalizedToken;
336
- const node = new Token({
337
- ...restData,
338
- name,
339
- value: value.base || "",
340
- path
341
- });
342
- node.setExtensions({
343
- category,
344
- conditions: value,
345
- prop: path.slice(1).join(".")
346
- });
347
- this.allTokens.push(node);
348
- },
349
- { stop: isToken }
350
- );
351
- }
352
- getByName = memo((name) => {
353
- for (const token of this.allTokens) {
354
- if (token.name === name)
355
- return token;
356
- }
357
- });
358
- transforms = /* @__PURE__ */ new Map();
359
- registerTransform(...transforms2) {
360
- transforms2.forEach((transform) => {
361
- transform.type ||= "value";
362
- transform.enforce ||= "pre";
363
- this.transforms.set(transform.name, transform);
364
- });
365
- return this;
366
- }
367
- execTransform(name) {
368
- const transform = this.transforms.get(name);
369
- if (!transform)
370
- return;
371
- this.allTokens.forEach((token) => {
372
- if (token.extensions.hasReference)
373
- return;
374
- if (typeof transform.match === "function" && !transform.match(token))
375
- return;
376
- const exec = (v) => transform.transform(v, { prefix: this.prefix, hash: this.hash });
377
- const transformed = exec(token);
378
- match(transform).with({ type: "extensions" }, () => {
379
- token.setExtensions(transformed);
380
- }).with({ type: "value" }, () => {
381
- token.value = transformed;
382
- if (token.isComposite) {
383
- token.originalValue = transformed;
384
- }
385
- if (token.extensions.conditions) {
386
- const conditions = token.extensions.conditions;
387
- const transformedConditions = walkObject2(conditions, (value) => exec({ value }), {
388
- stop: isCompositeTokenValue
389
- });
390
- token.setExtensions({
391
- conditions: transformedConditions
392
- });
393
- }
394
- }).otherwise(() => {
395
- token[transform.type] = transformed;
396
- });
397
- });
398
- }
399
- transformTokens(enforce) {
400
- this.transforms.forEach((transform) => {
401
- if (transform.enforce === enforce) {
402
- this.execTransform(transform.name);
403
- }
404
- });
405
- return this;
406
- }
407
- middlewares = [];
408
- registerMiddleware(...middlewares2) {
409
- for (const middleware of middlewares2) {
410
- middleware.enforce ||= "pre";
411
- this.middlewares.push(middleware);
412
- }
413
- return this;
414
- }
415
- applyMiddlewares(enforce) {
416
- this.middlewares.forEach((middleware) => {
417
- if (middleware.enforce === enforce) {
418
- middleware.transform(this, { prefix: this.prefix, hash: this.hash });
419
- }
420
- });
421
- }
422
- getReferences(value) {
423
- const refs = getReferences(value);
424
- return refs.map((ref) => this.getByName(ref)).filter(Boolean);
425
- }
426
- usesReference(value) {
427
- if (!isString(value))
428
- return false;
429
- return this.getReferences(value).length > 0;
430
- }
431
- addReferences() {
432
- this.allTokens.forEach((token) => {
433
- if (!this.usesReference(token.value))
434
- return;
435
- const references = this.getReferences(token.value);
436
- token.setExtensions({
437
- references: references.reduce((object, reference) => {
438
- object[reference.name] = reference;
439
- return object;
440
- }, {})
441
- });
442
- });
443
- return this;
444
- }
445
- filter(pattern) {
446
- const predicate = typeof pattern === "function" ? pattern : isMatching2(pattern);
447
- return this.allTokens.filter(predicate);
448
- }
449
- addConditionalTokens() {
450
- const tokens = [];
451
- this.allTokens.forEach((token) => {
452
- tokens.push(token);
453
- const conditionalTokens = token.getConditionTokens();
454
- if (conditionalTokens && conditionalTokens.length > 0) {
455
- tokens.push(...conditionalTokens);
456
- }
457
- });
458
- this.allTokens = tokens;
459
- return this;
460
- }
461
- expandReferences() {
462
- this.allTokens.forEach((token) => {
463
- token.expandReferences();
464
- });
465
- return this;
466
- }
467
- build() {
468
- this.applyMiddlewares("pre");
469
- this.transformTokens("pre");
470
- this.addConditionalTokens();
471
- this.addReferences();
472
- this.expandReferences();
473
- this.applyMiddlewares("post");
474
- this.transformTokens("post");
475
- }
476
- get isEmpty() {
477
- return this.allTokens.length === 0;
478
- }
479
- };
480
-
481
- // src/format.ts
482
- import { getDotPath } from "@pandacss/shared";
483
- var formats = {
484
- groupByCondition(dictionary) {
485
- const grouped = /* @__PURE__ */ new Map();
486
- dictionary.allTokens.forEach((token) => {
487
- const { condition } = token.extensions;
488
- if (!condition)
489
- return;
490
- grouped.get(condition) || grouped.set(condition, /* @__PURE__ */ new Set());
491
- grouped.set(condition, grouped.get(condition).add(token));
492
- });
493
- return grouped;
494
- },
495
- groupByColorPalette(dictionary) {
496
- const grouped = /* @__PURE__ */ new Map();
497
- dictionary.allTokens.forEach((token) => {
498
- const { colorPalette, colorPaletteRoots } = token.extensions;
499
- if (!colorPalette || token.extensions.isVirtual)
500
- return;
501
- colorPaletteRoots.forEach((colorPaletteRoot) => {
502
- grouped.get(colorPaletteRoot) || grouped.set(colorPaletteRoot, /* @__PURE__ */ new Map());
503
- const virtualName = token.name.replace(colorPaletteRoot, "colorPalette");
504
- const virtualToken = dictionary.getByName(virtualName);
505
- if (!virtualToken)
506
- return;
507
- const virtualVar = virtualToken.extensions.var;
508
- grouped.get(colorPaletteRoot).set(virtualVar, token.extensions.varRef);
509
- });
510
- });
511
- return grouped;
512
- },
513
- groupByCategory(dictionary) {
514
- const grouped = /* @__PURE__ */ new Map();
515
- dictionary.allTokens.forEach((token) => {
516
- const { category, prop } = token.extensions;
517
- if (!category)
518
- return;
519
- grouped.get(category) || grouped.set(category, /* @__PURE__ */ new Map());
520
- grouped.set(category, grouped.get(category).set(prop, token));
521
- });
522
- return grouped;
523
- },
524
- getFlattenedValues(dictionary) {
525
- const grouped = formats.groupByCategory(dictionary);
526
- const result = /* @__PURE__ */ new Map();
527
- grouped.forEach((tokens, category) => {
528
- result.get(category) || result.set(category, /* @__PURE__ */ new Map());
529
- tokens.forEach((token) => {
530
- const { prop, varRef, isNegative } = token.extensions;
531
- const value = isNegative ? token.isConditional ? token.originalValue : token.value : varRef;
532
- result.set(category, result.get(category).set(prop, value));
533
- });
534
- });
535
- return result;
536
- },
537
- getVars(dictionary) {
538
- const grouped = formats.groupByCondition(dictionary);
539
- const result = /* @__PURE__ */ new Map();
540
- grouped.forEach((tokens, condition) => {
541
- result.get(condition) || result.set(condition, /* @__PURE__ */ new Map());
542
- tokens.forEach((token) => {
543
- if (token.extensions.isNegative || token.extensions.isVirtual)
544
- return;
545
- result.get(condition).set(token.extensions.var, token.value);
546
- });
547
- });
548
- return result;
549
- },
550
- createVarGetter(dictionary) {
551
- const flatValues = mapToJson(formats.getFlattenedValues(dictionary));
552
- return function getToken(path, fallback) {
553
- return getDotPath(flatValues, path, fallback);
554
- };
555
- },
556
- getColorPaletteValues(dictionary) {
557
- const values = /* @__PURE__ */ new Set();
558
- dictionary.allTokens.forEach((token) => {
559
- const { colorPalette } = token.extensions;
560
- if (!colorPalette || token.extensions.isVirtual)
561
- return;
562
- values.add(colorPalette);
563
- });
564
- return values;
565
- }
566
- };
567
-
568
284
  // src/middleware.ts
569
- import { calc, cssVar, toPx } from "@pandacss/shared";
570
285
  var addNegativeTokens = {
571
286
  enforce: "pre",
572
- transform(dictionary, { prefix, hash }) {
287
+ transform(dictionary) {
288
+ const { prefix, hash } = dictionary;
573
289
  const tokens = dictionary.filter({
574
290
  extensions: { category: "spacing" }
575
291
  });
576
292
  tokens.forEach((token) => {
577
293
  const originalPath = [...token.path];
578
- const originalVar = cssVar(originalPath.join("-"), { prefix, hash });
294
+ const originalVar = dictionary.formatCssVar(originalPath, { prefix, hash });
579
295
  if (token.value === "0rem") {
580
296
  return;
581
297
  }
@@ -591,9 +307,9 @@ var addNegativeTokens = {
591
307
  node.path[node.path.length - 1] = `-${last}`;
592
308
  }
593
309
  if (node.path) {
594
- node.name = node.path.join(".");
310
+ node.name = dictionary.formatTokenName(node.path);
595
311
  }
596
- dictionary.allTokens.push(node);
312
+ dictionary.registerToken(node);
597
313
  });
598
314
  }
599
315
  };
@@ -614,36 +330,36 @@ var addPixelUnit = {
614
330
  var addVirtualPalette = {
615
331
  enforce: "post",
616
332
  transform(dictionary) {
617
- const tokens = dictionary.filter({
618
- extensions: { category: "colors" }
619
- });
620
- const keys = /* @__PURE__ */ new Set();
333
+ const tokens = dictionary.filter({ extensions: { category: "colors" } });
334
+ const keys = /* @__PURE__ */ new Map();
621
335
  const colorPalettes = /* @__PURE__ */ new Map();
622
336
  tokens.forEach((token) => {
623
337
  const { colorPalette, colorPaletteRoots, colorPaletteTokenKeys } = token.extensions;
624
338
  if (!colorPalette)
625
339
  return;
626
- colorPaletteTokenKeys.forEach(keys.add, keys);
340
+ colorPaletteTokenKeys.forEach((keyPath) => {
341
+ keys.set(dictionary.formatTokenName(keyPath), keyPath);
342
+ });
627
343
  colorPaletteRoots.forEach((colorPaletteRoot) => {
628
- const colorPaletteList = colorPalettes.get(colorPaletteRoot) || [];
344
+ const formated = dictionary.formatTokenName(colorPaletteRoot);
345
+ const colorPaletteList = colorPalettes.get(formated) || [];
629
346
  colorPaletteList.push(token);
630
- colorPalettes.set(colorPaletteRoot, colorPaletteList);
347
+ colorPalettes.set(formated, colorPaletteList);
631
348
  });
632
349
  });
633
- keys.forEach((key) => {
350
+ keys.forEach((segments) => {
634
351
  const node = new Token({
635
- name: ["colors.colorPalette", key].filter(Boolean).join("."),
636
- value: ["colors.colorPalette", key].filter(Boolean).join("."),
637
- path: ["colors", "colorPalette", ...key.split(".")]
352
+ name: dictionary.formatTokenName(["colors", "colorPalette", ...segments].filter(Boolean)),
353
+ value: dictionary.formatTokenName(["colors", "colorPalette", ...segments].filter(Boolean)),
354
+ path: ["colors", "colorPalette", ...segments]
638
355
  });
639
356
  node.setExtensions({
640
357
  category: "colors",
641
- prop: ["colorPalette", key].filter(Boolean).join("."),
358
+ prop: dictionary.formatTokenName(["colorPalette", ...segments].filter(Boolean)),
642
359
  isVirtual: true
643
360
  });
644
- dictionary.allTokens.push(node);
361
+ dictionary.registerToken(node, "pre");
645
362
  });
646
- dictionary.transformTokens("pre");
647
363
  }
648
364
  };
649
365
  var removeEmptyTokens = {
@@ -655,8 +371,8 @@ var removeEmptyTokens = {
655
371
  var middlewares = [addNegativeTokens, addVirtualPalette, removeEmptyTokens, addPixelUnit];
656
372
 
657
373
  // src/transform.ts
658
- import { cssVar as cssVar2, isString as isString2 } from "@pandacss/shared";
659
- import { P as P2, match as match2 } from "ts-pattern";
374
+ import { isString } from "@pandacss/shared";
375
+ import { P as P2, match } from "ts-pattern";
660
376
 
661
377
  // src/mini-svg-uri.ts
662
378
  var shorterNames = {
@@ -764,12 +480,12 @@ var objectKeys = (obj) => Object.keys(obj);
764
480
  var transformShadow = {
765
481
  name: "tokens/shadow",
766
482
  match: (token) => token.extensions.category === "shadows",
767
- transform(token, opts) {
768
- if (isString2(token.value)) {
483
+ transform(token, dict) {
484
+ if (isString(token.value)) {
769
485
  return token.value;
770
486
  }
771
487
  if (Array.isArray(token.value)) {
772
- return token.value.map((value) => this.transform({ value }, opts)).join(", ");
488
+ return token.value.map((value) => this.transform({ value }, dict)).join(", ");
773
489
  }
774
490
  if (isCompositeShadow(token.value)) {
775
491
  const { offsetX, offsetY, blur, spread, color, inset } = token.value;
@@ -782,13 +498,13 @@ var transformGradient = {
782
498
  name: "tokens/gradient",
783
499
  match: (token) => token.extensions.category === "gradients",
784
500
  transform(token) {
785
- if (isString2(token.value)) {
501
+ if (isString(token.value)) {
786
502
  return token.value;
787
503
  }
788
504
  if (isCompositeGradient(token.value)) {
789
505
  const { type, stops, placement } = token.value;
790
506
  const rawStops = stops.map((stop) => {
791
- if (isString2(stop))
507
+ if (isString(stop))
792
508
  return stop;
793
509
  const { color, position } = stop;
794
510
  return `${color} ${position}px`;
@@ -812,7 +528,7 @@ var transformEasings = {
812
528
  name: "tokens/easings",
813
529
  match: (token) => token.extensions.category === "easings",
814
530
  transform(token) {
815
- if (isString2(token.value)) {
531
+ if (isString(token.value)) {
816
532
  return token.value;
817
533
  }
818
534
  if (Array.isArray(token.value)) {
@@ -825,7 +541,7 @@ var transformBorders = {
825
541
  name: "tokens/borders",
826
542
  match: (token) => token.extensions.category === "borders",
827
543
  transform(token) {
828
- if (isString2(token.value)) {
544
+ if (isString(token.value)) {
829
545
  return token.value;
830
546
  }
831
547
  if (isCompositeBorder(token.value)) {
@@ -840,16 +556,17 @@ var transformAssets = {
840
556
  match: (token) => token.extensions.category === "assets",
841
557
  transform(token) {
842
558
  const raw = token.value;
843
- return match2(raw).with(P2.string, (value) => value).with({ type: "url" }, ({ value }) => `url('${value}')`).with({ type: "svg" }, ({ value }) => `url('${svgToDataUri(value)})'`).exhaustive();
559
+ return match(raw).with(P2.string, (value) => value).with({ type: "url" }, ({ value }) => `url('${value}')`).with({ type: "svg" }, ({ value }) => `url('${svgToDataUri(value)})'`).exhaustive();
844
560
  }
845
561
  };
846
562
  var addCssVariables = {
847
563
  type: "extensions",
848
564
  name: "tokens/css-var",
849
- transform(token, { prefix, hash }) {
565
+ transform(token, dictionary) {
566
+ const { prefix, hash } = dictionary;
850
567
  const { isNegative, originalPath } = token.extensions;
851
568
  const pathValue = isNegative ? originalPath : token.path;
852
- const variable = cssVar2(pathValue.filter(Boolean).join("-"), { prefix, hash });
569
+ const variable = dictionary.formatCssVar(pathValue.filter(Boolean), { prefix, hash });
853
570
  return {
854
571
  var: variable.var,
855
572
  varRef: variable.ref
@@ -859,12 +576,13 @@ var addCssVariables = {
859
576
  var addConditionalCssVariables = {
860
577
  enforce: "post",
861
578
  name: "tokens/conditionals",
862
- transform(token, { prefix, hash }) {
579
+ transform(token, dictionary) {
580
+ const { prefix, hash } = dictionary;
863
581
  const refs = getReferences(token.value);
864
582
  if (!refs.length)
865
583
  return token.value;
866
584
  refs.forEach((ref) => {
867
- const variable = cssVar2(ref.split(".").join("-"), { prefix, hash }).ref;
585
+ const variable = dictionary.formatCssVar(ref.split("."), { prefix, hash }).ref;
868
586
  token.value = token.value.replace(`{${ref}}`, variable);
869
587
  });
870
588
  return token.value;
@@ -876,7 +594,7 @@ var addColorPalette = {
876
594
  match(token) {
877
595
  return token.extensions.category === "colors" && !token.extensions.isVirtual;
878
596
  },
879
- transform(token) {
597
+ transform(token, dict) {
880
598
  let tokenPathClone = [...token.path];
881
599
  tokenPathClone.pop();
882
600
  tokenPathClone.shift();
@@ -889,18 +607,18 @@ var addColorPalette = {
889
607
  return {};
890
608
  }
891
609
  const colorPaletteRoots = tokenPathClone.reduce((acc, _, i, arr) => {
892
- const next = arr.slice(0, i + 1).join(".");
610
+ const next = arr.slice(0, i + 1);
893
611
  acc.push(next);
894
612
  return acc;
895
613
  }, []);
896
614
  const colorPaletteRoot = tokenPathClone[0];
897
- const colorPalette = tokenPathClone.join(".");
615
+ const colorPalette = dict.formatTokenName(tokenPathClone);
898
616
  const colorPaletteTokenKeys = token.path.slice(token.path.indexOf(colorPaletteRoot) + 1).reduce((acc, _, i, arr) => {
899
- acc.push(arr.slice(i).join("."));
617
+ acc.push(arr.slice(i));
900
618
  return acc;
901
619
  }, []);
902
620
  if (colorPaletteTokenKeys.length === 0) {
903
- colorPaletteTokenKeys.push("");
621
+ colorPaletteTokenKeys.push([""]);
904
622
  }
905
623
  return {
906
624
  colorPalette,
@@ -921,42 +639,224 @@ var transforms = [
921
639
  addColorPalette
922
640
  ];
923
641
 
924
- // src/create-dictionary.ts
925
- var TokenDictionary2 = class extends TokenDictionary {
926
- get;
927
- conditionMap;
928
- categoryMap;
929
- values;
930
- colorPalettes;
931
- vars;
932
- json;
642
+ // src/dictionary.ts
643
+ function expandBreakpoints(breakpoints) {
644
+ if (!breakpoints)
645
+ return { breakpoints: {}, sizes: {} };
646
+ return {
647
+ breakpoints: mapObject(breakpoints, (value) => ({ value })),
648
+ sizes: Object.fromEntries(Object.entries(breakpoints).map(([key, value]) => [`breakpoint-${key}`, { value }]))
649
+ };
650
+ }
651
+ function filterDefault(path) {
652
+ if (path[0] === "DEFAULT")
653
+ return path;
654
+ return path.filter((item) => item !== "DEFAULT");
655
+ }
656
+ var TokenDictionary = class {
933
657
  constructor(options) {
934
- super(options);
658
+ this.options = options;
659
+ }
660
+ allTokens = [];
661
+ byName = /* @__PURE__ */ new Map();
662
+ init() {
663
+ this.registerTokens();
935
664
  this.registerTransform(...transforms);
936
665
  this.registerMiddleware(...middlewares);
937
666
  this.build();
938
- this.get = formats.createVarGetter(this);
939
- this.conditionMap = formats.groupByCondition(this);
940
- this.categoryMap = formats.groupByCategory(this);
941
- this.values = formats.getFlattenedValues(this);
942
- this.colorPalettes = mapToJson2(formats.groupByColorPalette(this));
943
- this.vars = formats.getVars(this);
944
- this.json = mapToJson2(this.values);
945
- }
946
- getValue = memo2((path) => {
947
- const result = this.values.get(path);
948
- if (result != null) {
949
- return Object.fromEntries(result);
667
+ return this;
668
+ }
669
+ get prefix() {
670
+ return this.options.prefix;
671
+ }
672
+ get hash() {
673
+ return this.options.hash;
674
+ }
675
+ getByName = (path) => {
676
+ return this.byName.get(path);
677
+ };
678
+ formatTokenName = (path) => path.join(".");
679
+ formatCssVar = (path, options) => cssVar(path.join("-"), options);
680
+ registerTokens() {
681
+ const { tokens = {}, semanticTokens = {}, breakpoints } = this.options;
682
+ const breakpointTokens = expandBreakpoints(breakpoints);
683
+ const computedTokens = compact({
684
+ ...tokens,
685
+ breakpoints: breakpointTokens.breakpoints,
686
+ sizes: {
687
+ ...tokens.sizes,
688
+ ...breakpointTokens.sizes
689
+ }
690
+ });
691
+ walkObject2(
692
+ computedTokens,
693
+ (token, path) => {
694
+ path = filterDefault(path);
695
+ assertTokenFormat(token);
696
+ const category = path[0];
697
+ const name = this.formatTokenName(path);
698
+ const node = new Token({ ...token, name, path });
699
+ node.setExtensions({
700
+ category,
701
+ prop: this.formatTokenName(path.slice(1))
702
+ });
703
+ this.registerToken(node);
704
+ },
705
+ { stop: isToken }
706
+ );
707
+ walkObject2(
708
+ semanticTokens,
709
+ (token, path) => {
710
+ path = filterDefault(path);
711
+ assertTokenFormat(token);
712
+ const category = path[0];
713
+ const name = this.formatTokenName(path);
714
+ const normalizedToken = isString2(token.value) || isCompositeTokenValue(token.value) ? { value: { base: token.value } } : token;
715
+ const { value, ...restData } = normalizedToken;
716
+ const node = new Token({
717
+ ...restData,
718
+ name,
719
+ value: value.base || "",
720
+ path
721
+ });
722
+ node.setExtensions({
723
+ category,
724
+ conditions: value,
725
+ prop: this.formatTokenName(path.slice(1))
726
+ });
727
+ this.registerToken(node);
728
+ },
729
+ { stop: isToken }
730
+ );
731
+ return this;
732
+ }
733
+ registerToken = (token, transformPhase) => {
734
+ this.allTokens.push(token);
735
+ this.byName.set(token.name, token);
736
+ if (transformPhase) {
737
+ this.transforms.forEach((transform) => {
738
+ if (transform.enforce === transformPhase) {
739
+ this.execTransformOnToken(transform, token);
740
+ }
741
+ });
950
742
  }
951
- });
952
- getTokenVar = memo2((path) => {
953
- return getDotPath2(this.json, path);
954
- });
743
+ };
744
+ transforms = /* @__PURE__ */ new Map();
745
+ registerTransform(...transforms2) {
746
+ transforms2.forEach((transform) => {
747
+ transform.type ||= "value";
748
+ transform.enforce ||= "pre";
749
+ this.transforms.set(transform.name, transform);
750
+ });
751
+ return this;
752
+ }
753
+ execTransform(name) {
754
+ const transform = this.transforms.get(name);
755
+ if (!transform)
756
+ return;
757
+ this.allTokens.forEach((token) => {
758
+ this.execTransformOnToken(transform, token);
759
+ });
760
+ }
761
+ execTransformOnToken(transform, token) {
762
+ if (token.extensions.hasReference)
763
+ return;
764
+ if (typeof transform.match === "function" && !transform.match(token))
765
+ return;
766
+ const exec = (v) => transform.transform(v, this);
767
+ const transformed = exec(token);
768
+ match2(transform).with({ type: "extensions" }, () => {
769
+ token.setExtensions(transformed);
770
+ }).with({ type: "value" }, () => {
771
+ token.value = transformed;
772
+ if (token.isComposite) {
773
+ token.originalValue = transformed;
774
+ }
775
+ if (token.extensions.conditions) {
776
+ const conditions = token.extensions.conditions;
777
+ const transformedConditions = walkObject2(conditions, (value) => exec({ value }), {
778
+ stop: isCompositeTokenValue
779
+ });
780
+ token.setExtensions({
781
+ conditions: transformedConditions
782
+ });
783
+ }
784
+ }).otherwise(() => {
785
+ token[transform.type] = transformed;
786
+ });
787
+ }
788
+ transformTokens(enforce) {
789
+ this.transforms.forEach((transform) => {
790
+ if (transform.enforce === enforce) {
791
+ this.execTransform(transform.name);
792
+ }
793
+ });
794
+ return this;
795
+ }
796
+ middlewares = [];
797
+ registerMiddleware(...middlewares2) {
798
+ for (const middleware of middlewares2) {
799
+ middleware.enforce ||= "pre";
800
+ this.middlewares.push(middleware);
801
+ }
802
+ return this;
803
+ }
804
+ applyMiddlewares(enforce) {
805
+ this.middlewares.forEach((middleware) => {
806
+ if (middleware.enforce === enforce) {
807
+ middleware.transform(this);
808
+ }
809
+ });
810
+ }
811
+ getReferences(value) {
812
+ const refs = getReferences(value);
813
+ return refs.map((ref) => this.getByName(ref)).filter(Boolean);
814
+ }
815
+ usesReference(value) {
816
+ if (!isString2(value))
817
+ return false;
818
+ return this.getReferences(value).length > 0;
819
+ }
820
+ addReferences() {
821
+ this.allTokens.forEach((token) => {
822
+ if (!this.usesReference(token.value))
823
+ return;
824
+ const references = this.getReferences(token.value);
825
+ token.setExtensions({
826
+ references: references.reduce((object, reference) => {
827
+ object[reference.name] = reference;
828
+ return object;
829
+ }, {})
830
+ });
831
+ });
832
+ return this;
833
+ }
834
+ filter(pattern) {
835
+ const predicate = typeof pattern === "function" ? pattern : isMatching2(pattern);
836
+ return this.allTokens.filter(predicate);
837
+ }
838
+ addConditionalTokens() {
839
+ this.allTokens.forEach((token) => {
840
+ const conditionalTokens = token.getConditionTokens();
841
+ if (conditionalTokens && conditionalTokens.length > 0) {
842
+ conditionalTokens.forEach((token2) => {
843
+ this.registerToken(token2);
844
+ });
845
+ }
846
+ });
847
+ return this;
848
+ }
849
+ expandTokenReferences() {
850
+ this.allTokens.forEach((token) => {
851
+ token.expandReferences();
852
+ });
853
+ return this;
854
+ }
955
855
  /**
956
856
  * Expand token references to their CSS variable
957
857
  */
958
- expandReference(value) {
959
- return expandReferences(value, (key) => this.get(key));
858
+ expandReferenceInValue(value) {
859
+ return expandReferences(value, (path) => this.view.get(path));
960
860
  }
961
861
  /**
962
862
  * Resolve token references to their actual raw value
@@ -964,8 +864,129 @@ var TokenDictionary2 = class extends TokenDictionary {
964
864
  resolveReference(value) {
965
865
  return expandReferences(value, (key) => this.getByName(key)?.value);
966
866
  }
867
+ build() {
868
+ this.applyMiddlewares("pre");
869
+ this.transformTokens("pre");
870
+ this.addConditionalTokens();
871
+ this.addReferences();
872
+ this.expandTokenReferences();
873
+ this.applyMiddlewares("post");
874
+ this.transformTokens("post");
875
+ this.setComputedView();
876
+ return this;
877
+ }
878
+ get isEmpty() {
879
+ return this.allTokens.length === 0;
880
+ }
881
+ view;
882
+ setComputedView() {
883
+ this.view = new TokenDictionaryView(this).getTokensView();
884
+ return this;
885
+ }
886
+ };
887
+ var TokenDictionaryView = class {
888
+ constructor(dictionary) {
889
+ this.dictionary = dictionary;
890
+ this.dictionary = dictionary;
891
+ }
892
+ getTokensView() {
893
+ const conditionMap = /* @__PURE__ */ new Map();
894
+ const categoryMap = /* @__PURE__ */ new Map();
895
+ const colorPalettes = /* @__PURE__ */ new Map();
896
+ const valuesByCategory = /* @__PURE__ */ new Map();
897
+ const flatValues = /* @__PURE__ */ new Map();
898
+ const vars = /* @__PURE__ */ new Map();
899
+ this.dictionary.allTokens.forEach((token) => {
900
+ this.processCondition(token, conditionMap);
901
+ this.processColorPalette(token, colorPalettes, this.dictionary.byName);
902
+ this.processCategory(token, categoryMap);
903
+ this.processValue(token, valuesByCategory, flatValues);
904
+ this.processVars(token, vars);
905
+ });
906
+ const json = mapToJson(valuesByCategory);
907
+ return {
908
+ conditionMap,
909
+ categoryMap,
910
+ colorPalettes,
911
+ vars,
912
+ values: flatValues,
913
+ json,
914
+ get: memo((path, fallback) => {
915
+ return flatValues.get(path) ?? fallback;
916
+ }),
917
+ getCategoryValues: memo((category) => {
918
+ const result = json[category];
919
+ if (result != null) {
920
+ return result;
921
+ }
922
+ })
923
+ };
924
+ }
925
+ processCondition(token, group) {
926
+ const { condition } = token.extensions;
927
+ if (!condition)
928
+ return;
929
+ if (!group.has(condition))
930
+ group.set(condition, /* @__PURE__ */ new Set());
931
+ group.get(condition).add(token);
932
+ }
933
+ processColorPalette(token, group, byName) {
934
+ const { colorPalette, colorPaletteRoots, isVirtual } = token.extensions;
935
+ if (!colorPalette || isVirtual)
936
+ return;
937
+ colorPaletteRoots.forEach((colorPaletteRoot) => {
938
+ const formated = this.dictionary.formatTokenName(colorPaletteRoot);
939
+ if (!group.has(formated)) {
940
+ group.set(formated, /* @__PURE__ */ new Map());
941
+ }
942
+ const virtualPath = replaceRootWithColorPalette([...token.path], [...colorPaletteRoot]);
943
+ const virtualName = this.dictionary.formatTokenName(virtualPath);
944
+ const virtualToken = byName.get(virtualName);
945
+ if (!virtualToken)
946
+ return;
947
+ const virtualVar = virtualToken.extensions.var;
948
+ group.get(formated).set(virtualVar, token.extensions.varRef);
949
+ });
950
+ }
951
+ processCategory(token, group) {
952
+ const { category, prop } = token.extensions;
953
+ if (!category)
954
+ return;
955
+ if (!group.has(category))
956
+ group.set(category, /* @__PURE__ */ new Map());
957
+ group.get(category).set(prop, token);
958
+ }
959
+ processValue(token, byCategory, flat) {
960
+ const { category, prop, varRef, isNegative } = token.extensions;
961
+ if (!category)
962
+ return;
963
+ if (!byCategory.has(category))
964
+ byCategory.set(category, /* @__PURE__ */ new Map());
965
+ const value = isNegative ? token.isConditional ? token.originalValue : token.value : varRef;
966
+ byCategory.get(category).set(prop, value);
967
+ flat.set([category, prop].join("."), value);
968
+ }
969
+ processVars(token, group) {
970
+ const { condition, isNegative, isVirtual, var: varName } = token.extensions;
971
+ if (isNegative || isVirtual || !condition)
972
+ return;
973
+ if (!group.has(condition))
974
+ group.set(condition, /* @__PURE__ */ new Map());
975
+ group.get(condition).set(varName, token.value);
976
+ }
967
977
  };
978
+ function replaceRootWithColorPalette(path, colorPaletteRoot) {
979
+ const startIndex = path.findIndex(
980
+ (element, index) => colorPaletteRoot.every((rootElement, rootIndex) => path[index + rootIndex] === rootElement)
981
+ );
982
+ if (startIndex === -1) {
983
+ return path;
984
+ }
985
+ path.splice(startIndex, colorPaletteRoot.length);
986
+ path.splice(startIndex, 0, "colorPalette");
987
+ return path;
988
+ }
968
989
  export {
969
990
  Token,
970
- TokenDictionary2 as TokenDictionary
991
+ TokenDictionary
971
992
  };