@pandacss/token-dictionary 0.0.0-dev-20240124004908 → 0.0.0-dev-20240126170252

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
@@ -154,7 +154,14 @@ declare class TokenDictionary extends TokenDictionary$1 {
154
154
  [k: string]: string;
155
155
  } | undefined;
156
156
  getTokenVar: (path: string) => any;
157
+ /**
158
+ * Expand token references to their CSS variable
159
+ */
157
160
  expandReference(value: string): string;
161
+ /**
162
+ * Resolve token references to their actual raw value
163
+ */
164
+ resolveReference(value: string): string;
158
165
  }
159
166
 
160
167
  export { Token, TokenDictionary };
package/dist/index.d.ts CHANGED
@@ -154,7 +154,14 @@ declare class TokenDictionary extends TokenDictionary$1 {
154
154
  [k: string]: string;
155
155
  } | undefined;
156
156
  getTokenVar: (path: string) => any;
157
+ /**
158
+ * Expand token references to their CSS variable
159
+ */
157
160
  expandReference(value: string): string;
161
+ /**
162
+ * Resolve token references to their actual raw value
163
+ */
164
+ resolveReference(value: string): string;
158
165
  }
159
166
 
160
167
  export { Token, TokenDictionary };
package/dist/index.js CHANGED
@@ -70,6 +70,7 @@ var isCompositeTokenValue = (value) => {
70
70
  var import_shared2 = require("@pandacss/shared");
71
71
 
72
72
  // src/utils.ts
73
+ var import_logger = require("@pandacss/logger");
73
74
  var import_shared = require("@pandacss/shared");
74
75
  var REFERENCE_REGEX = /(\$[^\s,]+\w)|({([^}]*)})/g;
75
76
  var curlyBracketRegex = /[{}]/g;
@@ -84,14 +85,40 @@ function getReferences(value) {
84
85
  function hasReference(value) {
85
86
  return REFERENCE_REGEX.test(value);
86
87
  }
88
+ var tokenFunctionRegex = /token\(([^)]+)\)/g;
89
+ var closingParenthesisRegex = /\)$/g;
90
+ var hasTokenReference = (str) => str.includes("token(");
91
+ var tokenReplacer = (a, b) => b ? a.endsWith(")") ? a.replace(closingParenthesisRegex, `, ${b})`) : `var(${a}, ${b})` : a;
92
+ var notFoundMessage = (key, value) => `Reference not found: \`${key}\` in "${value}"`;
87
93
  function expandReferences(value, fn) {
88
- if (!hasReference(value))
94
+ if (!hasReference(value) && !hasTokenReference(value))
89
95
  return value;
90
96
  const references = getReferences(value);
91
- return references.reduce((valueStr, key) => {
92
- const value2 = fn(key) ?? key;
93
- return valueStr.replace(`{${key}}`, value2);
97
+ const expanded = references.reduce((valueStr, key) => {
98
+ const resolved = fn(key);
99
+ if (!resolved) {
100
+ import_logger.logger.warn("token", notFoundMessage(key, value));
101
+ }
102
+ const expandedValue = resolved ?? (0, import_shared.esc)(key);
103
+ return valueStr.replace(`{${key}}`, expandedValue);
94
104
  }, value);
105
+ if (!expanded.includes(`token(`))
106
+ return expanded;
107
+ return expanded.replace(tokenFunctionRegex, (_, token) => {
108
+ const [tokenValue, tokenFallback] = token.split(",").map((s) => s.trim());
109
+ const result = [tokenValue, tokenFallback].filter(Boolean).map((key) => {
110
+ const resolved = fn(key);
111
+ if (!resolved) {
112
+ import_logger.logger.warn("token", notFoundMessage(key, value));
113
+ }
114
+ return resolved ?? (0, import_shared.esc)(key);
115
+ });
116
+ if (result.length > 1) {
117
+ const [a, b] = result;
118
+ return tokenReplacer(a, b);
119
+ }
120
+ return tokenReplacer(result[0]);
121
+ });
95
122
  }
96
123
  function mapToJson(map) {
97
124
  const obj = {};
@@ -953,9 +980,18 @@ var TokenDictionary2 = class extends TokenDictionary {
953
980
  getTokenVar = (0, import_shared7.memo)((path) => {
954
981
  return (0, import_shared7.getDotPath)(this.json, path);
955
982
  });
983
+ /**
984
+ * Expand token references to their CSS variable
985
+ */
956
986
  expandReference(value) {
957
987
  return expandReferences(value, (key) => this.get(key));
958
988
  }
989
+ /**
990
+ * Resolve token references to their actual raw value
991
+ */
992
+ resolveReference(value) {
993
+ return expandReferences(value, (key) => this.getByName(key)?.value);
994
+ }
959
995
  };
960
996
  // Annotate the CommonJS export names for ESM import in node:
961
997
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -43,7 +43,8 @@ var isCompositeTokenValue = (value) => {
43
43
  import { isBaseCondition, toHash, walkObject } from "@pandacss/shared";
44
44
 
45
45
  // src/utils.ts
46
- import { isObject } from "@pandacss/shared";
46
+ import { logger } from "@pandacss/logger";
47
+ import { esc, isObject } from "@pandacss/shared";
47
48
  var REFERENCE_REGEX = /(\$[^\s,]+\w)|({([^}]*)})/g;
48
49
  var curlyBracketRegex = /[{}]/g;
49
50
  function getReferences(value) {
@@ -57,14 +58,40 @@ function getReferences(value) {
57
58
  function hasReference(value) {
58
59
  return REFERENCE_REGEX.test(value);
59
60
  }
61
+ var tokenFunctionRegex = /token\(([^)]+)\)/g;
62
+ var closingParenthesisRegex = /\)$/g;
63
+ var hasTokenReference = (str) => str.includes("token(");
64
+ var tokenReplacer = (a, b) => b ? a.endsWith(")") ? a.replace(closingParenthesisRegex, `, ${b})`) : `var(${a}, ${b})` : a;
65
+ var notFoundMessage = (key, value) => `Reference not found: \`${key}\` in "${value}"`;
60
66
  function expandReferences(value, fn) {
61
- if (!hasReference(value))
67
+ if (!hasReference(value) && !hasTokenReference(value))
62
68
  return value;
63
69
  const references = getReferences(value);
64
- return references.reduce((valueStr, key) => {
65
- const value2 = fn(key) ?? key;
66
- return valueStr.replace(`{${key}}`, value2);
70
+ const expanded = references.reduce((valueStr, key) => {
71
+ const resolved = fn(key);
72
+ if (!resolved) {
73
+ logger.warn("token", notFoundMessage(key, value));
74
+ }
75
+ const expandedValue = resolved ?? esc(key);
76
+ return valueStr.replace(`{${key}}`, expandedValue);
67
77
  }, value);
78
+ if (!expanded.includes(`token(`))
79
+ return expanded;
80
+ return expanded.replace(tokenFunctionRegex, (_, token) => {
81
+ const [tokenValue, tokenFallback] = token.split(",").map((s) => s.trim());
82
+ const result = [tokenValue, tokenFallback].filter(Boolean).map((key) => {
83
+ const resolved = fn(key);
84
+ if (!resolved) {
85
+ logger.warn("token", notFoundMessage(key, value));
86
+ }
87
+ return resolved ?? esc(key);
88
+ });
89
+ if (result.length > 1) {
90
+ const [a, b] = result;
91
+ return tokenReplacer(a, b);
92
+ }
93
+ return tokenReplacer(result[0]);
94
+ });
68
95
  }
69
96
  function mapToJson(map) {
70
97
  const obj = {};
@@ -926,9 +953,18 @@ var TokenDictionary2 = class extends TokenDictionary {
926
953
  getTokenVar = memo2((path) => {
927
954
  return getDotPath2(this.json, path);
928
955
  });
956
+ /**
957
+ * Expand token references to their CSS variable
958
+ */
929
959
  expandReference(value) {
930
960
  return expandReferences(value, (key) => this.get(key));
931
961
  }
962
+ /**
963
+ * Resolve token references to their actual raw value
964
+ */
965
+ resolveReference(value) {
966
+ return expandReferences(value, (key) => this.getByName(key)?.value);
967
+ }
932
968
  };
933
969
  export {
934
970
  Token,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pandacss/token-dictionary",
3
- "version": "0.0.0-dev-20240124004908",
3
+ "version": "0.0.0-dev-20240126170252",
4
4
  "description": "Common error messages for css panda",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -33,8 +33,9 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "ts-pattern": "5.0.5",
36
- "@pandacss/shared": "0.0.0-dev-20240124004908",
37
- "@pandacss/types": "0.0.0-dev-20240124004908"
36
+ "@pandacss/logger": "^0.0.0-dev-20240126170252",
37
+ "@pandacss/shared": "0.0.0-dev-20240126170252",
38
+ "@pandacss/types": "0.0.0-dev-20240126170252"
38
39
  },
39
40
  "scripts": {
40
41
  "build": "tsup src/index.ts --format=esm,cjs --dts",