chaincss 2.0.6 → 2.1.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.
Files changed (159) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/CODE_OF_CONDUCT.md +21 -0
  3. package/CONTRIBUTING.md +28 -0
  4. package/README.md +454 -231
  5. package/demo/demo/node_modules/caniuse-db/fulldata-json/data-2.0.json +1 -0
  6. package/demo/index.html +16 -0
  7. package/demo/package.json +20 -0
  8. package/demo/src/App.tsx +117 -0
  9. package/demo/src/chaincss-barrel.ts +9 -0
  10. package/demo/src/main.tsx +8 -0
  11. package/demo/src/styles.chain.ts +300 -0
  12. package/demo/vite.config.ts +46 -0
  13. package/dist/cli/commands/build.d.ts +0 -1
  14. package/dist/cli/commands/cache.d.ts +1 -0
  15. package/dist/cli/commands/init.d.ts +6 -3
  16. package/dist/cli/commands/timeline.d.ts +0 -1
  17. package/dist/cli/commands/watch.d.ts +0 -1
  18. package/dist/cli/index.d.ts +0 -1
  19. package/dist/cli/index.js +3213 -5296
  20. package/dist/cli/types.d.ts +51 -20
  21. package/dist/cli/utils/config-loader.d.ts +0 -1
  22. package/dist/cli/utils/file-utils.d.ts +27 -3
  23. package/dist/cli/utils/logger.d.ts +0 -1
  24. package/dist/compiler/Chain.d.ts +215 -0
  25. package/dist/compiler/animations.d.ts +76 -0
  26. package/dist/compiler/atomic-optimizer.d.ts +47 -12
  27. package/dist/compiler/breakpoints.d.ts +46 -0
  28. package/dist/compiler/btt.d.ts +36 -60
  29. package/dist/compiler/cache-manager.d.ts +58 -4
  30. package/dist/compiler/commonProps.d.ts +0 -1
  31. package/dist/compiler/content-addressable-cache.d.ts +78 -0
  32. package/dist/compiler/helpers.d.ts +54 -0
  33. package/dist/compiler/index.d.ts +16 -9
  34. package/dist/compiler/index.js +4450 -4316
  35. package/dist/compiler/prefixer.d.ts +17 -1
  36. package/dist/compiler/shorthands.d.ts +28 -0
  37. package/dist/compiler/suggestions.d.ts +43 -0
  38. package/dist/compiler/theme-contract.d.ts +16 -27
  39. package/dist/compiler/token-resolver.d.ts +69 -0
  40. package/dist/compiler/tokens.d.ts +33 -8
  41. package/dist/core/auto-detector.d.ts +34 -0
  42. package/dist/core/common-utils.d.ts +97 -0
  43. package/dist/core/compiler.d.ts +63 -23
  44. package/dist/core/constants.d.ts +137 -36
  45. package/dist/core/smart-chain.d.ts +3 -0
  46. package/dist/core/types.d.ts +122 -15
  47. package/dist/core/utils.d.ts +134 -17
  48. package/dist/index.d.ts +52 -8
  49. package/dist/index.js +7090 -5578
  50. package/dist/plugins/vite.d.ts +7 -5
  51. package/dist/plugins/vite.js +2964 -25641
  52. package/dist/plugins/webpack.d.ts +24 -1
  53. package/dist/plugins/webpack.js +209 -72
  54. package/dist/runtime/Chain.d.ts +32 -0
  55. package/dist/runtime/auto-hooks.d.ts +11 -0
  56. package/dist/runtime/hmr.d.ts +22 -2
  57. package/dist/runtime/index.d.ts +3 -2
  58. package/dist/runtime/index.js +3649 -301
  59. package/dist/runtime/injector.d.ts +39 -71
  60. package/dist/runtime/react.d.ts +17 -12
  61. package/dist/runtime/svelte.d.ts +15 -0
  62. package/dist/runtime/types.d.ts +126 -4
  63. package/dist/runtime/utils.d.ts +0 -1
  64. package/dist/runtime/vue.d.ts +34 -14
  65. package/package.json +59 -66
  66. package/src/cli/commands/build.ts +133 -0
  67. package/src/cli/commands/cache.ts +371 -0
  68. package/src/cli/commands/init.ts +230 -0
  69. package/src/cli/commands/timeline.ts +435 -0
  70. package/src/cli/commands/watch.ts +211 -0
  71. package/src/cli/index.ts +226 -0
  72. package/src/cli/types.ts +100 -0
  73. package/src/cli/utils/config-loader.ts +174 -0
  74. package/src/cli/utils/file-utils.ts +139 -0
  75. package/src/cli/utils/logger.ts +74 -0
  76. package/src/compiler/Chain.ts +831 -0
  77. package/src/compiler/animations.ts +517 -0
  78. package/src/compiler/atomic-optimizer.ts +786 -0
  79. package/src/compiler/breakpoints.ts +347 -0
  80. package/src/compiler/btt.ts +1147 -0
  81. package/src/compiler/cache-manager.ts +446 -0
  82. package/src/compiler/commonProps.ts +18 -0
  83. package/src/compiler/content-addressable-cache.ts +478 -0
  84. package/src/compiler/helpers.ts +407 -0
  85. package/src/compiler/index.ts +72 -0
  86. package/src/compiler/prefixer.ts +724 -0
  87. package/src/compiler/shorthands.ts +558 -0
  88. package/src/compiler/suggestions.ts +436 -0
  89. package/src/compiler/theme-contract.ts +197 -0
  90. package/src/compiler/token-resolver.ts +241 -0
  91. package/src/compiler/tokens.ts +612 -0
  92. package/src/core/auto-detector.ts +187 -0
  93. package/src/core/common-utils.ts +423 -0
  94. package/src/core/compiler.ts +835 -0
  95. package/src/core/constants.ts +424 -0
  96. package/src/core/index.ts +107 -0
  97. package/src/core/smart-chain.ts +163 -0
  98. package/src/core/types.ts +257 -0
  99. package/src/core/utils.ts +598 -0
  100. package/src/index.ts +208 -0
  101. package/src/plugins/vite.d.ts +316 -0
  102. package/src/plugins/vite.ts +424 -0
  103. package/src/plugins/webpack.d.ts +289 -0
  104. package/src/plugins/webpack.ts +416 -0
  105. package/src/runtime/Chain.ts +242 -0
  106. package/src/runtime/auto-hooks.tsx +127 -0
  107. package/src/runtime/auto-vue.ts +72 -0
  108. package/src/runtime/hmr.ts +212 -0
  109. package/src/runtime/index.ts +82 -0
  110. package/src/runtime/injector.ts +273 -0
  111. package/src/runtime/react.tsx +269 -0
  112. package/src/runtime/svelte.ts +15 -0
  113. package/src/runtime/types.ts +256 -0
  114. package/src/runtime/utils.ts +128 -0
  115. package/src/runtime/vite-env.d.ts +120 -0
  116. package/src/runtime/vue.ts +231 -0
  117. package/tsconfig.build.json +41 -0
  118. package/tsconfig.json +25 -0
  119. package/tsconfig.runtimes.json +18 -0
  120. package/dist/cli/cli.cjs +0 -7
  121. package/dist/cli/commands/build.d.ts.map +0 -1
  122. package/dist/cli/commands/compile.d.ts +0 -3
  123. package/dist/cli/commands/compile.d.ts.map +0 -1
  124. package/dist/cli/commands/init.d.ts.map +0 -1
  125. package/dist/cli/commands/timeline.d.ts.map +0 -1
  126. package/dist/cli/commands/watch.d.ts.map +0 -1
  127. package/dist/cli/index.d.ts.map +0 -1
  128. package/dist/cli/types.d.ts.map +0 -1
  129. package/dist/cli/utils/config-loader.d.ts.map +0 -1
  130. package/dist/cli/utils/file-utils.d.ts.map +0 -1
  131. package/dist/cli/utils/logger.d.ts.map +0 -1
  132. package/dist/compiler/atomic-optimizer.d.ts.map +0 -1
  133. package/dist/compiler/btt.d.ts.map +0 -1
  134. package/dist/compiler/cache-manager.d.ts.map +0 -1
  135. package/dist/compiler/commonProps.d.ts.map +0 -1
  136. package/dist/compiler/index.d.ts.map +0 -1
  137. package/dist/compiler/prefixer.d.ts.map +0 -1
  138. package/dist/compiler/theme-contract.d.ts.map +0 -1
  139. package/dist/compiler/tokens.d.ts.map +0 -1
  140. package/dist/compiler/types.d.ts +0 -57
  141. package/dist/compiler/types.d.ts.map +0 -1
  142. package/dist/core/compiler.d.ts.map +0 -1
  143. package/dist/core/constants.d.ts.map +0 -1
  144. package/dist/core/index.d.ts +0 -4
  145. package/dist/core/index.d.ts.map +0 -1
  146. package/dist/core/types.d.ts.map +0 -1
  147. package/dist/core/utils.d.ts.map +0 -1
  148. package/dist/index.d.ts.map +0 -1
  149. package/dist/plugins/vite.d.ts.map +0 -1
  150. package/dist/plugins/webpack.d.ts.map +0 -1
  151. package/dist/runtime/hmr.d.ts.map +0 -1
  152. package/dist/runtime/index.d.ts.map +0 -1
  153. package/dist/runtime/injector.d.ts.map +0 -1
  154. package/dist/runtime/react.d.ts.map +0 -1
  155. package/dist/runtime/react.js +0 -270
  156. package/dist/runtime/types.d.ts.map +0 -1
  157. package/dist/runtime/utils.d.ts.map +0 -1
  158. package/dist/runtime/vue.d.ts.map +0 -1
  159. package/dist/runtime/vue.js +0 -232
@@ -0,0 +1,241 @@
1
+ // src/compiler/token-resolver.ts
2
+
3
+ import { tokens as globalTokens } from './tokens.js';
4
+ import type { DesignTokens } from './tokens.js';
5
+
6
+ // Current token context (for runtime resolution)
7
+ let currentTokenContext: DesignTokens | null = null;
8
+
9
+ /**
10
+ * Set the current token context for resolution
11
+ */
12
+ export function setTokenContext(context: DesignTokens | null): void {
13
+ currentTokenContext = context;
14
+ }
15
+
16
+ /**
17
+ * Get the current token context
18
+ */
19
+ export function getTokenContext(): DesignTokens | null {
20
+ return currentTokenContext;
21
+ }
22
+
23
+ /**
24
+ * Clear the current token context
25
+ */
26
+ export function clearTokenContext(): void {
27
+ currentTokenContext = null;
28
+ }
29
+
30
+ /**
31
+ * Resolve token references in a value
32
+ * Supports formats:
33
+ * - $token.path (e.g., $colors.primary)
34
+ * - token('path') function style
35
+ * - Nested references within strings
36
+ */
37
+ export function resolveToken(
38
+ value: any,
39
+ useTokens: boolean = true,
40
+ tokenContext?: DesignTokens | null
41
+ ): any {
42
+ // Early return if tokens are disabled or value is not a string
43
+ if (!useTokens || typeof value !== 'string') return value;
44
+
45
+ // Handle function-style: token('colors.primary')
46
+ const functionMatch = value.match(/^(?:token|\$token)\s*\(\s*['"]([^'"]+)['"]\s*\)$/);
47
+ if (functionMatch) {
48
+ const tokenPath = functionMatch[1];
49
+ const resolved = resolveTokenPath(tokenPath, tokenContext);
50
+ return resolved !== undefined ? resolved : value;
51
+ }
52
+
53
+ // Handle inline token references within strings
54
+ if (value.includes('$')) {
55
+ return value.replace(/\$([a-zA-Z0-9.-]+)/g, (match: string, path: string) => {
56
+ const resolved = resolveTokenPath(path, tokenContext);
57
+ if (resolved !== undefined && resolved !== null) {
58
+ return String(resolved);
59
+ }
60
+ // Token not found - warn in development
61
+ if (process.env.NODE_ENV !== 'production') {
62
+ console.warn(`[ChainCSS] Token not found: ${path}`);
63
+ }
64
+ return match;
65
+ });
66
+ }
67
+
68
+ return value;
69
+ }
70
+
71
+ /**
72
+ * Resolve a token path to its value
73
+ */
74
+ function resolveTokenPath(
75
+ path: string,
76
+ tokenContext?: DesignTokens | null
77
+ ): any {
78
+ let resolved: any = null;
79
+
80
+ // First try the provided token context
81
+ if (tokenContext && typeof tokenContext.get === 'function') {
82
+ resolved = tokenContext.get(path);
83
+ }
84
+
85
+ // If not found, try current global context
86
+ if ((resolved === undefined || resolved === null) && currentTokenContext) {
87
+ resolved = currentTokenContext.get(path);
88
+ }
89
+
90
+ // If still not found, try global tokens
91
+ if (resolved === undefined || resolved === null) {
92
+ if (globalTokens && typeof globalTokens.get === 'function') {
93
+ resolved = globalTokens.get(path);
94
+ }
95
+ }
96
+
97
+ return resolved;
98
+ }
99
+
100
+ /**
101
+ * Resolve multiple token references in an object
102
+ */
103
+ export function resolveTokens(
104
+ obj: Record<string, any>,
105
+ useTokens: boolean = true,
106
+ tokenContext?: DesignTokens | null
107
+ ): Record<string, any> {
108
+ const result: Record<string, any> = {};
109
+
110
+ for (const [key, value] of Object.entries(obj)) {
111
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
112
+ result[key] = resolveTokens(value, useTokens, tokenContext);
113
+ } else {
114
+ result[key] = resolveToken(value, useTokens, tokenContext);
115
+ }
116
+ }
117
+
118
+ return result;
119
+ }
120
+
121
+ /**
122
+ * Check if a value contains token references
123
+ */
124
+ export function hasTokenReferences(value: any): boolean {
125
+ if (typeof value !== 'string') return false;
126
+ return value.includes('$') || /token\(['"][^'"]+['"]\)/.test(value);
127
+ }
128
+
129
+ /**
130
+ * Extract all token paths from a string
131
+ */
132
+ export function extractTokenPaths(value: string): string[] {
133
+ const paths: string[] = [];
134
+
135
+ // Match $token.path format
136
+ const dollarRegex = /\$([a-zA-Z0-9.-]+)/g;
137
+ let match;
138
+ while ((match = dollarRegex.exec(value)) !== null) {
139
+ paths.push(match[1]);
140
+ }
141
+
142
+ // Match token('path') format
143
+ const functionRegex = /token\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
144
+ while ((match = functionRegex.exec(value)) !== null) {
145
+ paths.push(match[1]);
146
+ }
147
+
148
+ return [...new Set(paths)]; // Remove duplicates
149
+ }
150
+
151
+ /**
152
+ * Create a token resolver function for a specific context
153
+ */
154
+ export function createTokenResolver(tokenContext?: DesignTokens | null): (value: any) => any {
155
+ const context = tokenContext || currentTokenContext || globalTokens;
156
+
157
+ return (value: any) => {
158
+ if (typeof value === 'string') {
159
+ return resolveToken(value, true, context);
160
+ }
161
+ return value;
162
+ };
163
+ }
164
+
165
+ /**
166
+ * Batch resolve multiple values
167
+ */
168
+ export function resolveBatch(
169
+ values: string[],
170
+ useTokens: boolean = true,
171
+ tokenContext?: DesignTokens | null
172
+ ): string[] {
173
+ return values.map(v => resolveToken(v, useTokens, tokenContext));
174
+ }
175
+
176
+ /**
177
+ * Token resolver class for caching and batch operations
178
+ */
179
+ export class TokenResolver {
180
+ private cache: Map<string, any> = new Map();
181
+ private context: DesignTokens | null;
182
+
183
+ constructor(context?: DesignTokens | null) {
184
+ this.context = context || currentTokenContext || globalTokens;
185
+ }
186
+
187
+ resolve(value: any): any {
188
+ if (typeof value !== 'string') return value;
189
+
190
+ // Check cache
191
+ if (this.cache.has(value)) {
192
+ return this.cache.get(value);
193
+ }
194
+
195
+ const resolved = resolveToken(value, true, this.context);
196
+ this.cache.set(value, resolved);
197
+
198
+ return resolved;
199
+ }
200
+
201
+ resolveObject(obj: Record<string, any>): Record<string, any> {
202
+ const result: Record<string, any> = {};
203
+
204
+ for (const [key, val] of Object.entries(obj)) {
205
+ if (val && typeof val === 'object' && !Array.isArray(val)) {
206
+ result[key] = this.resolveObject(val);
207
+ } else {
208
+ result[key] = this.resolve(val);
209
+ }
210
+ }
211
+
212
+ return result;
213
+ }
214
+
215
+ clearCache(): void {
216
+ this.cache.clear();
217
+ }
218
+
219
+ updateContext(context: DesignTokens | null): void {
220
+ this.context = context || globalTokens;
221
+ this.clearCache();
222
+ }
223
+
224
+ getStats(): { cacheSize: number } {
225
+ return { cacheSize: this.cache.size };
226
+ }
227
+ }
228
+
229
+ // Export default with all utilities
230
+ export default {
231
+ resolveToken,
232
+ setTokenContext,
233
+ getTokenContext,
234
+ clearTokenContext,
235
+ resolveTokens,
236
+ hasTokenReferences,
237
+ extractTokenPaths,
238
+ createTokenResolver,
239
+ resolveBatch,
240
+ TokenResolver
241
+ };