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.
- package/CHANGELOG.md +30 -0
- package/CODE_OF_CONDUCT.md +21 -0
- package/CONTRIBUTING.md +28 -0
- package/README.md +454 -231
- package/demo/demo/node_modules/caniuse-db/fulldata-json/data-2.0.json +1 -0
- package/demo/index.html +16 -0
- package/demo/package.json +20 -0
- package/demo/src/App.tsx +117 -0
- package/demo/src/chaincss-barrel.ts +9 -0
- package/demo/src/main.tsx +8 -0
- package/demo/src/styles.chain.ts +300 -0
- package/demo/vite.config.ts +46 -0
- package/dist/cli/commands/build.d.ts +0 -1
- package/dist/cli/commands/cache.d.ts +1 -0
- package/dist/cli/commands/init.d.ts +6 -3
- package/dist/cli/commands/timeline.d.ts +0 -1
- package/dist/cli/commands/watch.d.ts +0 -1
- package/dist/cli/index.d.ts +0 -1
- package/dist/cli/index.js +3213 -5296
- package/dist/cli/types.d.ts +51 -20
- package/dist/cli/utils/config-loader.d.ts +0 -1
- package/dist/cli/utils/file-utils.d.ts +27 -3
- package/dist/cli/utils/logger.d.ts +0 -1
- package/dist/compiler/Chain.d.ts +215 -0
- package/dist/compiler/animations.d.ts +76 -0
- package/dist/compiler/atomic-optimizer.d.ts +47 -12
- package/dist/compiler/breakpoints.d.ts +46 -0
- package/dist/compiler/btt.d.ts +36 -60
- package/dist/compiler/cache-manager.d.ts +58 -4
- package/dist/compiler/commonProps.d.ts +0 -1
- package/dist/compiler/content-addressable-cache.d.ts +78 -0
- package/dist/compiler/helpers.d.ts +54 -0
- package/dist/compiler/index.d.ts +16 -9
- package/dist/compiler/index.js +4450 -4316
- package/dist/compiler/prefixer.d.ts +17 -1
- package/dist/compiler/shorthands.d.ts +28 -0
- package/dist/compiler/suggestions.d.ts +43 -0
- package/dist/compiler/theme-contract.d.ts +16 -27
- package/dist/compiler/token-resolver.d.ts +69 -0
- package/dist/compiler/tokens.d.ts +33 -8
- package/dist/core/auto-detector.d.ts +34 -0
- package/dist/core/common-utils.d.ts +97 -0
- package/dist/core/compiler.d.ts +63 -23
- package/dist/core/constants.d.ts +137 -36
- package/dist/core/smart-chain.d.ts +3 -0
- package/dist/core/types.d.ts +122 -15
- package/dist/core/utils.d.ts +134 -17
- package/dist/index.d.ts +52 -8
- package/dist/index.js +7090 -5578
- package/dist/plugins/vite.d.ts +7 -5
- package/dist/plugins/vite.js +2964 -25641
- package/dist/plugins/webpack.d.ts +24 -1
- package/dist/plugins/webpack.js +209 -72
- package/dist/runtime/Chain.d.ts +32 -0
- package/dist/runtime/auto-hooks.d.ts +11 -0
- package/dist/runtime/hmr.d.ts +22 -2
- package/dist/runtime/index.d.ts +3 -2
- package/dist/runtime/index.js +3649 -301
- package/dist/runtime/injector.d.ts +39 -71
- package/dist/runtime/react.d.ts +17 -12
- package/dist/runtime/svelte.d.ts +15 -0
- package/dist/runtime/types.d.ts +126 -4
- package/dist/runtime/utils.d.ts +0 -1
- package/dist/runtime/vue.d.ts +34 -14
- package/package.json +59 -66
- package/src/cli/commands/build.ts +133 -0
- package/src/cli/commands/cache.ts +371 -0
- package/src/cli/commands/init.ts +230 -0
- package/src/cli/commands/timeline.ts +435 -0
- package/src/cli/commands/watch.ts +211 -0
- package/src/cli/index.ts +226 -0
- package/src/cli/types.ts +100 -0
- package/src/cli/utils/config-loader.ts +174 -0
- package/src/cli/utils/file-utils.ts +139 -0
- package/src/cli/utils/logger.ts +74 -0
- package/src/compiler/Chain.ts +831 -0
- package/src/compiler/animations.ts +517 -0
- package/src/compiler/atomic-optimizer.ts +786 -0
- package/src/compiler/breakpoints.ts +347 -0
- package/src/compiler/btt.ts +1147 -0
- package/src/compiler/cache-manager.ts +446 -0
- package/src/compiler/commonProps.ts +18 -0
- package/src/compiler/content-addressable-cache.ts +478 -0
- package/src/compiler/helpers.ts +407 -0
- package/src/compiler/index.ts +72 -0
- package/src/compiler/prefixer.ts +724 -0
- package/src/compiler/shorthands.ts +558 -0
- package/src/compiler/suggestions.ts +436 -0
- package/src/compiler/theme-contract.ts +197 -0
- package/src/compiler/token-resolver.ts +241 -0
- package/src/compiler/tokens.ts +612 -0
- package/src/core/auto-detector.ts +187 -0
- package/src/core/common-utils.ts +423 -0
- package/src/core/compiler.ts +835 -0
- package/src/core/constants.ts +424 -0
- package/src/core/index.ts +107 -0
- package/src/core/smart-chain.ts +163 -0
- package/src/core/types.ts +257 -0
- package/src/core/utils.ts +598 -0
- package/src/index.ts +208 -0
- package/src/plugins/vite.d.ts +316 -0
- package/src/plugins/vite.ts +424 -0
- package/src/plugins/webpack.d.ts +289 -0
- package/src/plugins/webpack.ts +416 -0
- package/src/runtime/Chain.ts +242 -0
- package/src/runtime/auto-hooks.tsx +127 -0
- package/src/runtime/auto-vue.ts +72 -0
- package/src/runtime/hmr.ts +212 -0
- package/src/runtime/index.ts +82 -0
- package/src/runtime/injector.ts +273 -0
- package/src/runtime/react.tsx +269 -0
- package/src/runtime/svelte.ts +15 -0
- package/src/runtime/types.ts +256 -0
- package/src/runtime/utils.ts +128 -0
- package/src/runtime/vite-env.d.ts +120 -0
- package/src/runtime/vue.ts +231 -0
- package/tsconfig.build.json +41 -0
- package/tsconfig.json +25 -0
- package/tsconfig.runtimes.json +18 -0
- package/dist/cli/cli.cjs +0 -7
- package/dist/cli/commands/build.d.ts.map +0 -1
- package/dist/cli/commands/compile.d.ts +0 -3
- package/dist/cli/commands/compile.d.ts.map +0 -1
- package/dist/cli/commands/init.d.ts.map +0 -1
- package/dist/cli/commands/timeline.d.ts.map +0 -1
- package/dist/cli/commands/watch.d.ts.map +0 -1
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/types.d.ts.map +0 -1
- package/dist/cli/utils/config-loader.d.ts.map +0 -1
- package/dist/cli/utils/file-utils.d.ts.map +0 -1
- package/dist/cli/utils/logger.d.ts.map +0 -1
- package/dist/compiler/atomic-optimizer.d.ts.map +0 -1
- package/dist/compiler/btt.d.ts.map +0 -1
- package/dist/compiler/cache-manager.d.ts.map +0 -1
- package/dist/compiler/commonProps.d.ts.map +0 -1
- package/dist/compiler/index.d.ts.map +0 -1
- package/dist/compiler/prefixer.d.ts.map +0 -1
- package/dist/compiler/theme-contract.d.ts.map +0 -1
- package/dist/compiler/tokens.d.ts.map +0 -1
- package/dist/compiler/types.d.ts +0 -57
- package/dist/compiler/types.d.ts.map +0 -1
- package/dist/core/compiler.d.ts.map +0 -1
- package/dist/core/constants.d.ts.map +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/types.d.ts.map +0 -1
- package/dist/core/utils.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/plugins/vite.d.ts.map +0 -1
- package/dist/plugins/webpack.d.ts.map +0 -1
- package/dist/runtime/hmr.d.ts.map +0 -1
- package/dist/runtime/index.d.ts.map +0 -1
- package/dist/runtime/injector.d.ts.map +0 -1
- package/dist/runtime/react.d.ts.map +0 -1
- package/dist/runtime/react.js +0 -270
- package/dist/runtime/types.d.ts.map +0 -1
- package/dist/runtime/utils.d.ts.map +0 -1
- package/dist/runtime/vue.d.ts.map +0 -1
- 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
|
+
};
|