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