chaincss 1.12.8
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/LICENSE +22 -0
- package/README.md +148 -0
- package/browser/index.js +3 -0
- package/browser/react-hooks.js +138 -0
- package/browser/rtt.js +212 -0
- package/node/atomic-optimizer.js +391 -0
- package/node/btt.js +871 -0
- package/node/cache-manager.js +56 -0
- package/node/chaincss.js +484 -0
- package/node/index.js +2 -0
- package/node/loaders/chaincss-loader.js +62 -0
- package/node/plugins/next-plugin.js +29 -0
- package/node/plugins/vite-plugin.js +215 -0
- package/node/plugins/webpack-plugin.js +41 -0
- package/node/prefixer.js +237 -0
- package/node/strVal.js +43 -0
- package/package.json +133 -0
- package/shared/tokens.cjs +256 -0
- package/shared/tokens.mjs +256 -0
- package/types.d.ts +239 -0
package/node/btt.js
ADDED
|
@@ -0,0 +1,871 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const https = require('https');
|
|
4
|
+
const { tokens, createTokens, responsive } = require('../shared/tokens.cjs');
|
|
5
|
+
const { AtomicOptimizer } = require('./atomic-optimizer');
|
|
6
|
+
|
|
7
|
+
const atomicOptimizer = new AtomicOptimizer({
|
|
8
|
+
enabled: false,
|
|
9
|
+
alwaysAtomic: [],
|
|
10
|
+
neverAtomic: ['content', 'animation']
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
function configureAtomic(opts) {
|
|
14
|
+
Object.assign(atomicOptimizer.options, opts);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const chain = {
|
|
18
|
+
cssOutput: undefined,
|
|
19
|
+
catcher: {},
|
|
20
|
+
cachedValidProperties: [],
|
|
21
|
+
classMap: {},
|
|
22
|
+
atomicStats: null,
|
|
23
|
+
|
|
24
|
+
initializeProperties() {
|
|
25
|
+
try {
|
|
26
|
+
const jsonPath = path.join(__dirname, 'css-properties.json');
|
|
27
|
+
if (fs.existsSync(jsonPath)) {
|
|
28
|
+
const data = fs.readFileSync(jsonPath, 'utf8');
|
|
29
|
+
this.cachedValidProperties = JSON.parse(data);
|
|
30
|
+
} else {
|
|
31
|
+
console.log('⚠️ CSS properties not cached, will load on first use');
|
|
32
|
+
}
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error('Error loading CSS properties:', error.message);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
fetchWithHttps(url) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
https.get(url, (response) => {
|
|
41
|
+
let data = '';
|
|
42
|
+
response.on('data', (chunk) => data += chunk);
|
|
43
|
+
response.on('end', () => {
|
|
44
|
+
try {
|
|
45
|
+
resolve(JSON.parse(data));
|
|
46
|
+
} catch (error) {
|
|
47
|
+
reject(error);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}).on('error', reject);
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
async getCSSProperties() {
|
|
55
|
+
try {
|
|
56
|
+
const jsonPath = path.join(__dirname, 'css-properties.json');
|
|
57
|
+
try {
|
|
58
|
+
await fs.promises.access(jsonPath);
|
|
59
|
+
const existingData = await fs.promises.readFile(jsonPath, 'utf8');
|
|
60
|
+
const objProp = JSON.parse(existingData);
|
|
61
|
+
this.cachedValidProperties = objProp;
|
|
62
|
+
return objProp;
|
|
63
|
+
} catch {
|
|
64
|
+
const url = 'https://raw.githubusercontent.com/mdn/data/main/css/properties.json';
|
|
65
|
+
const data = await this.fetchWithHttps(url);
|
|
66
|
+
const allProperties = Object.keys(data);
|
|
67
|
+
const baseProperties = new Set();
|
|
68
|
+
allProperties.forEach(prop => {
|
|
69
|
+
const baseProp = prop.replace(/^-(webkit|moz|ms|o)-/, '');
|
|
70
|
+
baseProperties.add(baseProp);
|
|
71
|
+
});
|
|
72
|
+
const cleanProperties = Array.from(baseProperties).sort();
|
|
73
|
+
await fs.promises.writeFile(jsonPath, JSON.stringify(cleanProperties, null, 2));
|
|
74
|
+
this.cachedValidProperties = cleanProperties;
|
|
75
|
+
return cleanProperties;
|
|
76
|
+
}
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error('Error loading CSS properties:', error.message);
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
getCachedProperties() {
|
|
84
|
+
return this.cachedValidProperties;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
chain.initializeProperties();
|
|
89
|
+
|
|
90
|
+
const resolveToken = (value, useTokens) => {
|
|
91
|
+
if (!useTokens || typeof value !== 'string' || !value.startsWith('$')) {
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
const tokenPath = value.slice(1);
|
|
95
|
+
const tokenValue = tokens.get(tokenPath);
|
|
96
|
+
return tokenValue || value;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
function $(useTokens = true) {
|
|
100
|
+
const catcher = {};
|
|
101
|
+
const validProperties = chain.cachedValidProperties;
|
|
102
|
+
|
|
103
|
+
const handler = {
|
|
104
|
+
get: (target, prop) => {
|
|
105
|
+
// Handle .block()
|
|
106
|
+
if (prop === 'block') {
|
|
107
|
+
return function(...args) {
|
|
108
|
+
if (args.length === 0) {
|
|
109
|
+
const result = { ...catcher };
|
|
110
|
+
Object.keys(catcher).forEach(key => delete catcher[key]);
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
const result = {
|
|
114
|
+
selectors: args,
|
|
115
|
+
...catcher
|
|
116
|
+
};
|
|
117
|
+
Object.keys(catcher).forEach(key => delete catcher[key]);
|
|
118
|
+
return result;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Handle .hover()
|
|
123
|
+
if (prop === 'hover') {
|
|
124
|
+
return () => {
|
|
125
|
+
const hoverCatcher = {};
|
|
126
|
+
const hoverHandler = {
|
|
127
|
+
get: (hoverTarget, hoverProp) => {
|
|
128
|
+
if (hoverProp === 'end') {
|
|
129
|
+
return () => {
|
|
130
|
+
catcher.hover = { ...hoverCatcher };
|
|
131
|
+
Object.keys(hoverCatcher).forEach(key => delete hoverCatcher[key]);
|
|
132
|
+
return proxy;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const cssProperty = hoverProp.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
136
|
+
if (validProperties && validProperties.length > 0 && !validProperties.includes(cssProperty)) {
|
|
137
|
+
console.warn(`Warning: '${cssProperty}' may not be a valid CSS property`);
|
|
138
|
+
}
|
|
139
|
+
return (value) => {
|
|
140
|
+
hoverCatcher[hoverProp] = resolveToken(value, useTokens);
|
|
141
|
+
return hoverProxy;
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const hoverProxy = new Proxy({}, hoverHandler);
|
|
146
|
+
return hoverProxy;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Handle .end()
|
|
151
|
+
if (prop === 'end') {
|
|
152
|
+
return () => {
|
|
153
|
+
return proxy;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Handle .select() - nested selectors
|
|
158
|
+
if (prop === 'select') {
|
|
159
|
+
return function(selector) {
|
|
160
|
+
const nestedStyles = {};
|
|
161
|
+
const nestedHandler = {
|
|
162
|
+
get: (nestedTarget, nestedProp) => {
|
|
163
|
+
if (nestedProp === 'block') {
|
|
164
|
+
return () => {
|
|
165
|
+
if (!catcher.nestedRules) catcher.nestedRules = [];
|
|
166
|
+
catcher.nestedRules.push({
|
|
167
|
+
selector: selector,
|
|
168
|
+
styles: { ...nestedStyles }
|
|
169
|
+
});
|
|
170
|
+
return proxy;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return (value) => {
|
|
174
|
+
nestedStyles[nestedProp] = resolveToken(value, useTokens);
|
|
175
|
+
return nestedProxy;
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
const nestedProxy = new Proxy({}, nestedHandler);
|
|
180
|
+
return nestedProxy;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// ========== AT-RULES ==========
|
|
185
|
+
|
|
186
|
+
// @media
|
|
187
|
+
if (prop === 'media') {
|
|
188
|
+
return function(query, callback) {
|
|
189
|
+
const subChain = $(useTokens);
|
|
190
|
+
const result = callback(subChain);
|
|
191
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
192
|
+
catcher.atRules.push({
|
|
193
|
+
type: 'media',
|
|
194
|
+
query: query,
|
|
195
|
+
styles: result
|
|
196
|
+
});
|
|
197
|
+
return proxy;
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// @keyframes
|
|
202
|
+
if (prop === 'keyframes') {
|
|
203
|
+
return function(name, callback) {
|
|
204
|
+
const keyframeContext = { _keyframeSteps: {} };
|
|
205
|
+
const keyframeProxy = new Proxy(keyframeContext, {
|
|
206
|
+
get: (target, stepProp) => {
|
|
207
|
+
if (stepProp === 'from' || stepProp === 'to') {
|
|
208
|
+
return function(stepCallback) {
|
|
209
|
+
const subChain = $(useTokens);
|
|
210
|
+
const properties = stepCallback(subChain).block();
|
|
211
|
+
target._keyframeSteps[stepProp] = properties;
|
|
212
|
+
return keyframeProxy;
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
if (stepProp === 'percent') {
|
|
216
|
+
return function(value, stepCallback) {
|
|
217
|
+
const subChain = $(useTokens);
|
|
218
|
+
const properties = stepCallback(subChain).block();
|
|
219
|
+
target._keyframeSteps[`${value}%`] = properties;
|
|
220
|
+
return keyframeProxy;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
callback(keyframeProxy);
|
|
227
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
228
|
+
catcher.atRules.push({
|
|
229
|
+
type: 'keyframes',
|
|
230
|
+
name: name,
|
|
231
|
+
steps: keyframeContext._keyframeSteps
|
|
232
|
+
});
|
|
233
|
+
return proxy;
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// @font-face
|
|
238
|
+
if (prop === 'fontFace') {
|
|
239
|
+
return function(callback) {
|
|
240
|
+
const fontProps = {};
|
|
241
|
+
const fontHandler = {
|
|
242
|
+
get: (target, fontProp) => {
|
|
243
|
+
return (value) => {
|
|
244
|
+
fontProps[fontProp] = resolveToken(value, useTokens);
|
|
245
|
+
return fontProxy;
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
const fontProxy = new Proxy({}, fontHandler);
|
|
250
|
+
callback(fontProxy);
|
|
251
|
+
|
|
252
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
253
|
+
catcher.atRules.push({
|
|
254
|
+
type: 'font-face',
|
|
255
|
+
properties: fontProps
|
|
256
|
+
});
|
|
257
|
+
return proxy;
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// @supports
|
|
262
|
+
if (prop === 'supports') {
|
|
263
|
+
return function(condition, callback) {
|
|
264
|
+
const subChain = $(useTokens);
|
|
265
|
+
const result = callback(subChain);
|
|
266
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
267
|
+
catcher.atRules.push({
|
|
268
|
+
type: 'supports',
|
|
269
|
+
condition: condition,
|
|
270
|
+
styles: result
|
|
271
|
+
});
|
|
272
|
+
return proxy;
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// @container
|
|
277
|
+
if (prop === 'container') {
|
|
278
|
+
return function(condition, callback) {
|
|
279
|
+
const subChain = $(useTokens);
|
|
280
|
+
const result = callback(subChain);
|
|
281
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
282
|
+
catcher.atRules.push({
|
|
283
|
+
type: 'container',
|
|
284
|
+
condition: condition,
|
|
285
|
+
styles: result
|
|
286
|
+
});
|
|
287
|
+
return proxy;
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// @layer
|
|
292
|
+
if (prop === 'layer') {
|
|
293
|
+
return function(name, callback) {
|
|
294
|
+
const subChain = $(useTokens);
|
|
295
|
+
const result = callback(subChain);
|
|
296
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
297
|
+
catcher.atRules.push({
|
|
298
|
+
type: 'layer',
|
|
299
|
+
name: name,
|
|
300
|
+
styles: result
|
|
301
|
+
});
|
|
302
|
+
return proxy;
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// @counter-style
|
|
307
|
+
if (prop === 'counterStyle') {
|
|
308
|
+
return function(name, callback) {
|
|
309
|
+
const counterProps = {};
|
|
310
|
+
const counterHandler = {
|
|
311
|
+
get: (target, counterProp) => {
|
|
312
|
+
return (value) => {
|
|
313
|
+
counterProps[counterProp] = resolveToken(value, useTokens);
|
|
314
|
+
return counterProxy;
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const counterProxy = new Proxy({}, counterHandler);
|
|
319
|
+
callback(counterProxy);
|
|
320
|
+
|
|
321
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
322
|
+
catcher.atRules.push({
|
|
323
|
+
type: 'counter-style',
|
|
324
|
+
name: name,
|
|
325
|
+
properties: counterProps
|
|
326
|
+
});
|
|
327
|
+
return proxy;
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// @property
|
|
332
|
+
if (prop === 'property') {
|
|
333
|
+
return function(name, callback) {
|
|
334
|
+
const propertyDescs = {};
|
|
335
|
+
const propertyHandler = {
|
|
336
|
+
get: (target, descProp) => {
|
|
337
|
+
return (value) => {
|
|
338
|
+
propertyDescs[descProp] = resolveToken(value, useTokens);
|
|
339
|
+
return propertyProxy;
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
const propertyProxy = new Proxy({}, propertyHandler);
|
|
344
|
+
callback(propertyProxy);
|
|
345
|
+
|
|
346
|
+
if (!catcher.atRules) catcher.atRules = [];
|
|
347
|
+
catcher.atRules.push({
|
|
348
|
+
type: 'property',
|
|
349
|
+
name: name,
|
|
350
|
+
descriptors: propertyDescs
|
|
351
|
+
});
|
|
352
|
+
return proxy;
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Regular CSS properties
|
|
357
|
+
const cssProperty = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
358
|
+
if (validProperties && validProperties.length > 0 && !validProperties.includes(cssProperty)) {
|
|
359
|
+
console.warn(`Warning: '${cssProperty}' may not be a valid CSS property`);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return function(value) {
|
|
363
|
+
catcher[prop] = resolveToken(value, useTokens);
|
|
364
|
+
return proxy;
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const proxy = new Proxy({}, handler);
|
|
370
|
+
|
|
371
|
+
if (chain.cachedValidProperties.length === 0) {
|
|
372
|
+
chain.getCSSProperties().catch(err => {
|
|
373
|
+
console.error('Failed to load CSS properties:', err.message);
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return proxy;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Process at-rules for CSS generation
|
|
381
|
+
function processAtRule(rule, parentSelectors = null) {
|
|
382
|
+
let output = '';
|
|
383
|
+
|
|
384
|
+
switch(rule.type) {
|
|
385
|
+
case 'media':
|
|
386
|
+
if (parentSelectors) {
|
|
387
|
+
let mediaBody = '';
|
|
388
|
+
if (rule.styles && typeof rule.styles === 'object') {
|
|
389
|
+
for (let prop in rule.styles) {
|
|
390
|
+
if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
|
|
391
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
392
|
+
mediaBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (mediaBody.trim()) {
|
|
397
|
+
output = `@media ${rule.query} {\n ${parentSelectors.join(', ')} {\n${mediaBody} }\n}\n`;
|
|
398
|
+
}
|
|
399
|
+
} else {
|
|
400
|
+
output = `@media ${rule.query} {\n`;
|
|
401
|
+
if (rule.styles && rule.styles.selectors) {
|
|
402
|
+
let ruleBody = '';
|
|
403
|
+
for (let prop in rule.styles) {
|
|
404
|
+
if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
|
|
405
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
406
|
+
ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (ruleBody.trim()) {
|
|
410
|
+
output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
output += '}\n';
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
|
|
417
|
+
case 'keyframes':
|
|
418
|
+
output = `@keyframes ${rule.name} {\n`;
|
|
419
|
+
for (let step in rule.steps) {
|
|
420
|
+
output += ` ${step} {\n`;
|
|
421
|
+
for (let prop in rule.steps[step]) {
|
|
422
|
+
if (prop !== 'selectors') {
|
|
423
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
424
|
+
output += ` ${kebabKey}: ${rule.steps[step][prop]};\n`;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
output += ' }\n';
|
|
428
|
+
}
|
|
429
|
+
output += '}\n';
|
|
430
|
+
break;
|
|
431
|
+
|
|
432
|
+
case 'font-face':
|
|
433
|
+
output = '@font-face {\n';
|
|
434
|
+
for (let prop in rule.properties) {
|
|
435
|
+
if (prop !== 'selectors') {
|
|
436
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
437
|
+
output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
output += '}\n';
|
|
441
|
+
break;
|
|
442
|
+
|
|
443
|
+
case 'supports':
|
|
444
|
+
output = `@supports ${rule.condition} {\n`;
|
|
445
|
+
if (rule.styles && rule.styles.selectors) {
|
|
446
|
+
let ruleBody = '';
|
|
447
|
+
for (let prop in rule.styles) {
|
|
448
|
+
if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
|
|
449
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
450
|
+
ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (ruleBody.trim()) {
|
|
454
|
+
output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
output += '}\n';
|
|
458
|
+
break;
|
|
459
|
+
|
|
460
|
+
case 'container':
|
|
461
|
+
output = `@container ${rule.condition} {\n`;
|
|
462
|
+
if (rule.styles && rule.styles.selectors) {
|
|
463
|
+
let ruleBody = '';
|
|
464
|
+
for (let prop in rule.styles) {
|
|
465
|
+
if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
|
|
466
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
467
|
+
ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (ruleBody.trim()) {
|
|
471
|
+
output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
output += '}\n';
|
|
475
|
+
break;
|
|
476
|
+
|
|
477
|
+
case 'layer':
|
|
478
|
+
output = `@layer ${rule.name} {\n`;
|
|
479
|
+
if (rule.styles && rule.styles.selectors) {
|
|
480
|
+
let ruleBody = '';
|
|
481
|
+
for (let prop in rule.styles) {
|
|
482
|
+
if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
|
|
483
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
484
|
+
ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (ruleBody.trim()) {
|
|
488
|
+
output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
output += '}\n';
|
|
492
|
+
break;
|
|
493
|
+
|
|
494
|
+
case 'counter-style':
|
|
495
|
+
output = `@counter-style ${rule.name} {\n`;
|
|
496
|
+
for (let prop in rule.properties) {
|
|
497
|
+
if (prop !== 'selectors') {
|
|
498
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
499
|
+
output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
output += '}\n';
|
|
503
|
+
break;
|
|
504
|
+
|
|
505
|
+
case 'property':
|
|
506
|
+
output = `@property ${rule.name} {\n`;
|
|
507
|
+
for (let desc in rule.descriptors) {
|
|
508
|
+
if (desc !== 'selectors') {
|
|
509
|
+
const kebabKey = desc.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
510
|
+
output += ` ${kebabKey}: ${rule.descriptors[desc]};\n`;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
output += '}\n';
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return output;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function processStandaloneAtRule(rule) {
|
|
521
|
+
let output = '';
|
|
522
|
+
|
|
523
|
+
switch(rule.type) {
|
|
524
|
+
case 'font-face':
|
|
525
|
+
output = '@font-face {\n';
|
|
526
|
+
for (let prop in rule.properties) {
|
|
527
|
+
if (prop !== 'selectors') {
|
|
528
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
529
|
+
output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
output += '}\n';
|
|
533
|
+
break;
|
|
534
|
+
|
|
535
|
+
case 'keyframes':
|
|
536
|
+
output = `@keyframes ${rule.name} {\n`;
|
|
537
|
+
for (let step in rule.steps) {
|
|
538
|
+
output += ` ${step} {\n`;
|
|
539
|
+
for (let prop in rule.steps[step]) {
|
|
540
|
+
if (prop !== 'selectors') {
|
|
541
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
542
|
+
output += ` ${kebabKey}: ${rule.steps[step][prop]};\n`;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
output += ' }\n';
|
|
546
|
+
}
|
|
547
|
+
output += '}\n';
|
|
548
|
+
break;
|
|
549
|
+
|
|
550
|
+
case 'counter-style':
|
|
551
|
+
output = `@counter-style ${rule.name} {\n`;
|
|
552
|
+
for (let prop in rule.properties) {
|
|
553
|
+
if (prop !== 'selectors') {
|
|
554
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
555
|
+
output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
output += '}\n';
|
|
559
|
+
break;
|
|
560
|
+
|
|
561
|
+
case 'property':
|
|
562
|
+
output = `@property ${rule.name} {\n`;
|
|
563
|
+
for (let desc in rule.descriptors) {
|
|
564
|
+
if (desc !== 'selectors') {
|
|
565
|
+
const kebabKey = desc.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
566
|
+
output += ` ${kebabKey}: ${rule.descriptors[desc]};\n`;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
output += '}\n';
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return output;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const run = (...args) => {
|
|
577
|
+
let cssOutput = '';
|
|
578
|
+
const styleObjs = [];
|
|
579
|
+
|
|
580
|
+
args.forEach((value) => {
|
|
581
|
+
if (!value) return;
|
|
582
|
+
styleObjs.push(value);
|
|
583
|
+
|
|
584
|
+
if (value.selectors) {
|
|
585
|
+
let mainRuleBody = '';
|
|
586
|
+
let atRulesOutput = '';
|
|
587
|
+
|
|
588
|
+
for (let key in value) {
|
|
589
|
+
if (key === 'selectors' || !value.hasOwnProperty(key)) continue;
|
|
590
|
+
|
|
591
|
+
if (key === 'atRules' && Array.isArray(value[key])) {
|
|
592
|
+
value[key].forEach(rule => {
|
|
593
|
+
atRulesOutput += processAtRule(rule, value.selectors);
|
|
594
|
+
});
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (key === 'nestedRules' && Array.isArray(value[key])) {
|
|
599
|
+
value[key].forEach(rule => {
|
|
600
|
+
let nestedBody = '';
|
|
601
|
+
for (let prop in rule.styles) {
|
|
602
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
603
|
+
nestedBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
|
|
604
|
+
}
|
|
605
|
+
if (nestedBody) {
|
|
606
|
+
atRulesOutput += `${value.selectors.join(', ')} ${rule.selector} {\n${nestedBody} }\n`;
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (key === 'hover' && typeof value[key] === 'object') {
|
|
613
|
+
let hoverBody = '';
|
|
614
|
+
for (let hoverKey in value[key]) {
|
|
615
|
+
const kebabKey = hoverKey.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
616
|
+
hoverBody += ` ${kebabKey}: ${value[key][hoverKey]};\n`;
|
|
617
|
+
}
|
|
618
|
+
if (hoverBody) {
|
|
619
|
+
cssOutput += `${value.selectors.join(', ')}:hover {\n${hoverBody}}\n`;
|
|
620
|
+
}
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const kebabKey = key.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
625
|
+
mainRuleBody += ` ${kebabKey}: ${value[key]};\n`;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
if (mainRuleBody.trim()) {
|
|
629
|
+
cssOutput += `${value.selectors.join(', ')} {\n${mainRuleBody}}\n`;
|
|
630
|
+
}
|
|
631
|
+
cssOutput += atRulesOutput;
|
|
632
|
+
|
|
633
|
+
} else if (value.type) {
|
|
634
|
+
cssOutput += processStandaloneAtRule(value);
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
cssOutput = cssOutput.replace(/\n{3,}/g, '\n\n').trim();
|
|
639
|
+
chain.cssOutput = cssOutput;
|
|
640
|
+
|
|
641
|
+
if (atomicOptimizer.options.enabled) {
|
|
642
|
+
const result = atomicOptimizer.optimize(styleObjs);
|
|
643
|
+
chain.cssOutput = result.css;
|
|
644
|
+
chain.classMap = result.map;
|
|
645
|
+
chain.atomicStats = result.stats;
|
|
646
|
+
return result.css;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return cssOutput;
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
const compile = (obj) => {
|
|
653
|
+
let cssString = '';
|
|
654
|
+
const collected = [];
|
|
655
|
+
|
|
656
|
+
for (const key in obj) {
|
|
657
|
+
if (!obj.hasOwnProperty(key)) continue;
|
|
658
|
+
const element = obj[key];
|
|
659
|
+
|
|
660
|
+
if (element.atRules && Array.isArray(element.atRules)) {
|
|
661
|
+
element.atRules.forEach(rule => {
|
|
662
|
+
cssString += processAtRule(rule, null);
|
|
663
|
+
});
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (element.selectors) {
|
|
668
|
+
collected.push(element);
|
|
669
|
+
let elementCSS = '';
|
|
670
|
+
let atRulesCSS = '';
|
|
671
|
+
|
|
672
|
+
for (let prop in element) {
|
|
673
|
+
if (prop === 'selectors' || !element.hasOwnProperty(prop)) continue;
|
|
674
|
+
|
|
675
|
+
if (prop === 'atRules' && Array.isArray(element[prop])) {
|
|
676
|
+
element[prop].forEach(rule => {
|
|
677
|
+
atRulesCSS += processAtRule(rule, element.selectors);
|
|
678
|
+
});
|
|
679
|
+
} else if (prop === 'nestedRules' && Array.isArray(element[prop])) {
|
|
680
|
+
element[prop].forEach(rule => {
|
|
681
|
+
let nestedBody = '';
|
|
682
|
+
for (let nestedProp in rule.styles) {
|
|
683
|
+
const kebabKey = nestedProp.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
684
|
+
nestedBody += ` ${kebabKey}: ${rule.styles[nestedProp]};\n`;
|
|
685
|
+
}
|
|
686
|
+
if (nestedBody) {
|
|
687
|
+
atRulesCSS += `${element.selectors.join(', ')} ${rule.selector} {\n${nestedBody} }\n`;
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
} else if (prop === 'hover' && typeof element[prop] === 'object') {
|
|
691
|
+
let hoverBody = '';
|
|
692
|
+
for (let hoverKey in element[prop]) {
|
|
693
|
+
const kebabKey = hoverKey.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
694
|
+
hoverBody += ` ${kebabKey}: ${element[prop][hoverKey]};\n`;
|
|
695
|
+
}
|
|
696
|
+
if (hoverBody) {
|
|
697
|
+
cssString += `${element.selectors.join(', ')}:hover {\n${hoverBody}}\n`;
|
|
698
|
+
}
|
|
699
|
+
} else {
|
|
700
|
+
const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
701
|
+
elementCSS += ` ${kebabKey}: ${element[prop]};\n`;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (elementCSS.trim()) {
|
|
706
|
+
cssString += `${element.selectors.join(', ')} {\n${elementCSS}}\n`;
|
|
707
|
+
}
|
|
708
|
+
cssString += atRulesCSS;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
chain.cssOutput = cssString.trim();
|
|
713
|
+
|
|
714
|
+
if (atomicOptimizer.options.enabled) {
|
|
715
|
+
const result = atomicOptimizer.optimize(collected);
|
|
716
|
+
chain.cssOutput = result.css;
|
|
717
|
+
chain.classMap = result.map;
|
|
718
|
+
chain.atomicStats = result.stats;
|
|
719
|
+
return result.css;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
return chain.cssOutput;
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
function recipe(options) {
|
|
726
|
+
const {
|
|
727
|
+
base,
|
|
728
|
+
variants = {},
|
|
729
|
+
defaultVariants = {},
|
|
730
|
+
compoundVariants = []
|
|
731
|
+
} = options;
|
|
732
|
+
|
|
733
|
+
const baseStyle = typeof base === 'function' ? base() : base;
|
|
734
|
+
const variantStyles = {};
|
|
735
|
+
|
|
736
|
+
for (const [variantName, variantMap] of Object.entries(variants)) {
|
|
737
|
+
variantStyles[variantName] = {};
|
|
738
|
+
for (const [variantKey, variantStyle] of Object.entries(variantMap)) {
|
|
739
|
+
variantStyles[variantName][variantKey] = typeof variantStyle === 'function'
|
|
740
|
+
? variantStyle()
|
|
741
|
+
: variantStyle;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
const compoundStyles = compoundVariants.map(cv => ({
|
|
746
|
+
condition: cv.variants || cv,
|
|
747
|
+
style: typeof cv.style === 'function' ? cv.style() : cv.style
|
|
748
|
+
}));
|
|
749
|
+
|
|
750
|
+
function mergeStyles(...styles) {
|
|
751
|
+
const merged = {};
|
|
752
|
+
for (const style of styles) {
|
|
753
|
+
if (!style) continue;
|
|
754
|
+
for (const [key, value] of Object.entries(style)) {
|
|
755
|
+
if (key === 'selectors') {
|
|
756
|
+
merged.selectors = merged.selectors || [];
|
|
757
|
+
merged.selectors.push(...(Array.isArray(value) ? value : [value]));
|
|
758
|
+
} else if (key === 'hover' && typeof value === 'object') {
|
|
759
|
+
if (!merged.hover) merged.hover = {};
|
|
760
|
+
Object.assign(merged.hover, value);
|
|
761
|
+
} else if (key !== 'selectors') {
|
|
762
|
+
merged[key] = value;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
return merged;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
function pick(variantSelection = {}) {
|
|
770
|
+
const selected = { ...defaultVariants, ...variantSelection };
|
|
771
|
+
const stylesToMerge = [];
|
|
772
|
+
|
|
773
|
+
if (baseStyle) stylesToMerge.push(baseStyle);
|
|
774
|
+
for (const [variantName, variantValue] of Object.entries(selected)) {
|
|
775
|
+
const variantStyle = variantStyles[variantName]?.[variantValue];
|
|
776
|
+
if (variantStyle) stylesToMerge.push(variantStyle);
|
|
777
|
+
}
|
|
778
|
+
for (const cv of compoundStyles) {
|
|
779
|
+
const matches = Object.entries(cv.condition).every(
|
|
780
|
+
([key, value]) => selected[key] === value
|
|
781
|
+
);
|
|
782
|
+
if (matches && cv.style) stylesToMerge.push(cv.style);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
const merged = mergeStyles(...stylesToMerge);
|
|
786
|
+
|
|
787
|
+
const styleBuilder = $(true);
|
|
788
|
+
for (const [prop, value] of Object.entries(merged)) {
|
|
789
|
+
if (prop === 'selectors' || prop === 'hover') continue;
|
|
790
|
+
if (styleBuilder[prop]) styleBuilder[prop](value);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
if (merged.hover) {
|
|
794
|
+
styleBuilder.hover();
|
|
795
|
+
for (const [hoverProp, hoverValue] of Object.entries(merged.hover)) {
|
|
796
|
+
if (styleBuilder[hoverProp]) styleBuilder[hoverProp](hoverValue);
|
|
797
|
+
}
|
|
798
|
+
styleBuilder.end();
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
const selectors = merged.selectors || [];
|
|
802
|
+
return styleBuilder.block(...selectors);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
pick.variants = variants;
|
|
806
|
+
pick.defaultVariants = defaultVariants;
|
|
807
|
+
pick.base = baseStyle;
|
|
808
|
+
|
|
809
|
+
pick.getAllVariants = () => {
|
|
810
|
+
const result = [];
|
|
811
|
+
const variantKeys = Object.keys(variants);
|
|
812
|
+
|
|
813
|
+
function generate(current, index) {
|
|
814
|
+
if (index === variantKeys.length) {
|
|
815
|
+
result.push({ ...current });
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
const variantName = variantKeys[index];
|
|
819
|
+
for (const variantValue of Object.keys(variants[variantName])) {
|
|
820
|
+
current[variantName] = variantValue;
|
|
821
|
+
generate(current, index + 1);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
generate({}, 0);
|
|
826
|
+
return result;
|
|
827
|
+
};
|
|
828
|
+
|
|
829
|
+
pick.compileAll = () => {
|
|
830
|
+
const allVariants = pick.getAllVariants();
|
|
831
|
+
const styles = [];
|
|
832
|
+
|
|
833
|
+
if (baseStyle) styles.push(baseStyle);
|
|
834
|
+
for (const variantMap of Object.values(variants)) {
|
|
835
|
+
for (const variantStyle of Object.values(variantMap)) {
|
|
836
|
+
if (variantStyle) styles.push(variantStyle);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
for (const cv of compoundStyles) {
|
|
840
|
+
if (cv.style) styles.push(cv.style);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
if (atomicOptimizer.options.enabled) {
|
|
844
|
+
const styleObj = {};
|
|
845
|
+
styles.forEach((style, i) => {
|
|
846
|
+
const selectors = style.selectors || [`variant-${i}`];
|
|
847
|
+
styleObj[selectors[0].replace(/^\./, '')] = style;
|
|
848
|
+
});
|
|
849
|
+
const result = atomicOptimizer.optimize(styleObj);
|
|
850
|
+
chain.cssOutput = (chain.cssOutput || '') + result.css;
|
|
851
|
+
chain.classMap = { ...chain.classMap, ...result.map };
|
|
852
|
+
return result.css;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
return run(...styles);
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
return pick;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
module.exports = {
|
|
862
|
+
chain,
|
|
863
|
+
$,
|
|
864
|
+
run,
|
|
865
|
+
compile,
|
|
866
|
+
createTokens,
|
|
867
|
+
responsive,
|
|
868
|
+
configureAtomic,
|
|
869
|
+
atomicOptimizer,
|
|
870
|
+
recipe
|
|
871
|
+
};
|