@plumeria/compiler 10.0.1 → 10.0.3
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 +206 -165
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -40,6 +40,65 @@ const fs = __importStar(require("fs"));
|
|
|
40
40
|
const rs = __importStar(require("@rust-gear/glob"));
|
|
41
41
|
const utils_1 = require("@plumeria/utils");
|
|
42
42
|
const utils_2 = require("@plumeria/utils");
|
|
43
|
+
function extractStylesFromExpression(expression, ctx) {
|
|
44
|
+
const results = [];
|
|
45
|
+
if (utils_1.t.isObjectExpression(expression)) {
|
|
46
|
+
const object = (0, utils_1.objectExpressionToObject)(expression, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
47
|
+
if (object)
|
|
48
|
+
results.push(object);
|
|
49
|
+
}
|
|
50
|
+
else if (utils_1.t.isMemberExpression(expression)) {
|
|
51
|
+
const memberExpr = expression;
|
|
52
|
+
if (utils_1.t.isIdentifier(memberExpr.object) &&
|
|
53
|
+
utils_1.t.isIdentifier(memberExpr.property)) {
|
|
54
|
+
const variableName = memberExpr.object.value;
|
|
55
|
+
const propertyName = memberExpr.property.value;
|
|
56
|
+
const styleSet = ctx.localCreateStyles[variableName];
|
|
57
|
+
if (styleSet && styleSet.obj[propertyName]) {
|
|
58
|
+
results.push(styleSet.obj[propertyName]);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const hash = ctx.mergedCreateTable[variableName];
|
|
62
|
+
if (hash) {
|
|
63
|
+
const object = ctx.scannedTables.createObjectTable[hash];
|
|
64
|
+
if (object && object[propertyName]) {
|
|
65
|
+
results.push(object[propertyName]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else if (utils_1.t.isIdentifier(expression)) {
|
|
72
|
+
const identifier = expression;
|
|
73
|
+
const object = ctx.localCreateStyles[identifier.value];
|
|
74
|
+
if (object)
|
|
75
|
+
results.push(object.obj);
|
|
76
|
+
else {
|
|
77
|
+
const hash = ctx.mergedCreateTable[identifier.value];
|
|
78
|
+
if (hash) {
|
|
79
|
+
const objectFromTable = ctx.scannedTables.createObjectTable[hash];
|
|
80
|
+
if (objectFromTable)
|
|
81
|
+
results.push(objectFromTable);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else if (utils_1.t.isConditionalExpression(expression)) {
|
|
86
|
+
const condExpr = expression;
|
|
87
|
+
results.push(...extractStylesFromExpression(condExpr.consequent, ctx));
|
|
88
|
+
results.push(...extractStylesFromExpression(condExpr.alternate, ctx));
|
|
89
|
+
}
|
|
90
|
+
else if (utils_1.t.isBinaryExpression(expression) &&
|
|
91
|
+
['&&', '||', '??'].includes(expression.operator)) {
|
|
92
|
+
const binaryExpr = expression;
|
|
93
|
+
results.push(...extractStylesFromExpression(binaryExpr.left, ctx));
|
|
94
|
+
results.push(...extractStylesFromExpression(binaryExpr.right, ctx));
|
|
95
|
+
}
|
|
96
|
+
else if (expression.type === 'ParenthesisExpression') {
|
|
97
|
+
const parenExpr = expression;
|
|
98
|
+
results.push(...extractStylesFromExpression(parenExpr.expression, ctx));
|
|
99
|
+
}
|
|
100
|
+
return results;
|
|
101
|
+
}
|
|
43
102
|
function compileCSS(options) {
|
|
44
103
|
const { include, exclude, cwd = process.cwd() } = options;
|
|
45
104
|
const allSheets = new Set();
|
|
@@ -92,32 +151,25 @@ function compileCSS(options) {
|
|
|
92
151
|
: specifier.local.value;
|
|
93
152
|
const localName = specifier.local.value;
|
|
94
153
|
const uniqueKey = `${actualPath}-${importedName}`;
|
|
95
|
-
if (scannedTables.staticTable[uniqueKey])
|
|
154
|
+
if (scannedTables.staticTable[uniqueKey])
|
|
96
155
|
importMap[localName] = scannedTables.staticTable[uniqueKey];
|
|
97
|
-
|
|
98
|
-
if (scannedTables.keyframesHashTable[uniqueKey]) {
|
|
156
|
+
if (scannedTables.keyframesHashTable[uniqueKey])
|
|
99
157
|
importMap[localName] =
|
|
100
158
|
scannedTables.keyframesHashTable[uniqueKey];
|
|
101
|
-
|
|
102
|
-
if (scannedTables.viewTransitionHashTable[uniqueKey]) {
|
|
159
|
+
if (scannedTables.viewTransitionHashTable[uniqueKey])
|
|
103
160
|
importMap[localName] =
|
|
104
161
|
scannedTables.viewTransitionHashTable[uniqueKey];
|
|
105
|
-
|
|
106
|
-
if (scannedTables.createHashTable[uniqueKey]) {
|
|
162
|
+
if (scannedTables.createHashTable[uniqueKey])
|
|
107
163
|
importMap[localName] = scannedTables.createHashTable[uniqueKey];
|
|
108
|
-
|
|
109
|
-
if (scannedTables.variantsHashTable[uniqueKey]) {
|
|
164
|
+
if (scannedTables.variantsHashTable[uniqueKey])
|
|
110
165
|
importMap[localName] =
|
|
111
166
|
scannedTables.variantsHashTable[uniqueKey];
|
|
112
|
-
|
|
113
|
-
if (scannedTables.createThemeHashTable[uniqueKey]) {
|
|
167
|
+
if (scannedTables.createThemeHashTable[uniqueKey])
|
|
114
168
|
importMap[localName] =
|
|
115
169
|
scannedTables.createThemeHashTable[uniqueKey];
|
|
116
|
-
|
|
117
|
-
if (scannedTables.createStaticHashTable[uniqueKey]) {
|
|
170
|
+
if (scannedTables.createStaticHashTable[uniqueKey])
|
|
118
171
|
importMap[localName] =
|
|
119
172
|
scannedTables.createStaticHashTable[uniqueKey];
|
|
120
|
-
}
|
|
121
173
|
}
|
|
122
174
|
});
|
|
123
175
|
}
|
|
@@ -177,70 +229,124 @@ function compileCSS(options) {
|
|
|
177
229
|
for (const key of Object.keys(importMap)) {
|
|
178
230
|
mergedVariantsTable[key] = importMap[key];
|
|
179
231
|
}
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
232
|
+
const ctx = {
|
|
233
|
+
mergedStaticTable,
|
|
234
|
+
mergedKeyframesTable,
|
|
235
|
+
mergedViewTransitionTable,
|
|
236
|
+
mergedCreateThemeHashTable,
|
|
237
|
+
mergedCreateStaticHashTable,
|
|
238
|
+
mergedCreateTable,
|
|
239
|
+
mergedVariantsTable,
|
|
240
|
+
scannedTables,
|
|
241
|
+
localCreateStyles: {},
|
|
242
|
+
sourceBuffer,
|
|
243
|
+
baseByteOffset,
|
|
244
|
+
};
|
|
245
|
+
const processStyle = (style) => {
|
|
246
|
+
(0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
|
|
247
|
+
const records = (0, utils_1.getStyleRecords)(style);
|
|
248
|
+
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
249
|
+
};
|
|
250
|
+
const extractAndProcessConditionals = (args, isStyleName = false) => {
|
|
251
|
+
const conditionals = [];
|
|
252
|
+
let baseStyle = {};
|
|
253
|
+
const resolveStyleObject = (expression) => {
|
|
254
|
+
const styles = extractStylesFromExpression(expression, ctx);
|
|
255
|
+
return styles.length === 1 ? styles[0] : null;
|
|
256
|
+
};
|
|
257
|
+
const getSource = (node) => ctx.sourceBuffer
|
|
258
|
+
.subarray(node.span.start - ctx.baseByteOffset, node.span.end - ctx.baseByteOffset)
|
|
259
|
+
.toString('utf-8');
|
|
260
|
+
const collectConditions = (node, testStrings = []) => {
|
|
261
|
+
const staticStyle = resolveStyleObject(node);
|
|
262
|
+
if (staticStyle) {
|
|
263
|
+
if (testStrings.length === 0) {
|
|
264
|
+
baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
|
|
197
265
|
}
|
|
198
266
|
else {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
267
|
+
conditionals.push({
|
|
268
|
+
test: node,
|
|
269
|
+
testString: testStrings.join(' && '),
|
|
270
|
+
truthy: staticStyle,
|
|
271
|
+
falsy: {},
|
|
272
|
+
});
|
|
206
273
|
}
|
|
274
|
+
return true;
|
|
207
275
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
276
|
+
if (node.type === 'ConditionalExpression') {
|
|
277
|
+
const testSource = getSource(node.test);
|
|
278
|
+
collectConditions(node.consequent, [
|
|
279
|
+
...testStrings,
|
|
280
|
+
`(${testSource})`,
|
|
281
|
+
]);
|
|
282
|
+
collectConditions(node.alternate, [
|
|
283
|
+
...testStrings,
|
|
284
|
+
`!(${testSource})`,
|
|
285
|
+
]);
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
else if (node.type === 'BinaryExpression' &&
|
|
289
|
+
node.operator === '&&') {
|
|
290
|
+
collectConditions(node.right, [
|
|
291
|
+
...testStrings,
|
|
292
|
+
`(${getSource(node.left)})`,
|
|
293
|
+
]);
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
else if (node.type === 'ParenthesisExpression') {
|
|
297
|
+
return collectConditions(node.expression, testStrings);
|
|
298
|
+
}
|
|
299
|
+
return false;
|
|
300
|
+
};
|
|
301
|
+
const checkFunctionKey = (node) => {
|
|
302
|
+
if (isStyleName)
|
|
303
|
+
return;
|
|
304
|
+
if (utils_1.t.isCallExpression(node) &&
|
|
305
|
+
utils_1.t.isMemberExpression(node.callee) &&
|
|
306
|
+
utils_1.t.isIdentifier(node.callee.object) &&
|
|
307
|
+
utils_1.t.isIdentifier(node.callee.property)) {
|
|
308
|
+
const varName = node.callee.object.value;
|
|
309
|
+
const propKey = node.callee.property.value;
|
|
310
|
+
const styleInfo = ctx.localCreateStyles[varName];
|
|
311
|
+
const atomMap = styleInfo?.obj?.[propKey];
|
|
312
|
+
if (atomMap?.['__cssVars__']) {
|
|
313
|
+
throw new Error(`[plumeria] css.use(${getSource(node)}) cannot handle dynamic style functions. Use styleName instead.\n`);
|
|
220
314
|
}
|
|
221
315
|
}
|
|
316
|
+
if (node.type === 'ConditionalExpression') {
|
|
317
|
+
checkFunctionKey(node.consequent);
|
|
318
|
+
checkFunctionKey(node.alternate);
|
|
319
|
+
}
|
|
320
|
+
else if (node.type === 'BinaryExpression' &&
|
|
321
|
+
['&&', '||', '??'].includes(node.operator)) {
|
|
322
|
+
checkFunctionKey(node.left);
|
|
323
|
+
checkFunctionKey(node.right);
|
|
324
|
+
}
|
|
325
|
+
else if (node.type === 'ParenthesisExpression') {
|
|
326
|
+
checkFunctionKey(node.expression);
|
|
327
|
+
}
|
|
328
|
+
else if (node.type === 'ArrayExpression') {
|
|
329
|
+
for (const el of node.elements) {
|
|
330
|
+
if (el && el.expression)
|
|
331
|
+
checkFunctionKey(el.expression);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
for (const arg of args) {
|
|
336
|
+
checkFunctionKey(arg.expression);
|
|
337
|
+
if (collectConditions(arg.expression))
|
|
338
|
+
continue;
|
|
339
|
+
const extractedStyles = extractStylesFromExpression(arg.expression, ctx);
|
|
340
|
+
extractedStyles.forEach(processStyle);
|
|
222
341
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const binaryExpr = expression;
|
|
231
|
-
results.push(...extractStylesFromExpression(binaryExpr.left));
|
|
232
|
-
results.push(...extractStylesFromExpression(binaryExpr.right));
|
|
233
|
-
}
|
|
234
|
-
else if (expression.type === 'ParenthesisExpression') {
|
|
235
|
-
const parenExpr = expression;
|
|
236
|
-
results.push(...extractStylesFromExpression(parenExpr.expression));
|
|
342
|
+
if (Object.keys(baseStyle).length > 0)
|
|
343
|
+
processStyle(baseStyle);
|
|
344
|
+
for (const cond of conditionals) {
|
|
345
|
+
if (cond.truthy && Object.keys(cond.truthy).length > 0)
|
|
346
|
+
processStyle(cond.truthy);
|
|
347
|
+
if (cond.falsy && Object.keys(cond.falsy).length > 0)
|
|
348
|
+
processStyle(cond.falsy);
|
|
237
349
|
}
|
|
238
|
-
return results;
|
|
239
|
-
};
|
|
240
|
-
const processStyle = (style) => {
|
|
241
|
-
(0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
|
|
242
|
-
const records = (0, utils_1.getStyleRecords)(style);
|
|
243
|
-
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
244
350
|
};
|
|
245
351
|
const processCall = (node) => {
|
|
246
352
|
node._processed = true;
|
|
@@ -251,8 +357,7 @@ function compileCSS(options) {
|
|
|
251
357
|
utils_1.t.isIdentifier(callee.property)) {
|
|
252
358
|
const objectName = callee.object.value;
|
|
253
359
|
const propertyName = callee.property.value;
|
|
254
|
-
|
|
255
|
-
if (alias === 'NAMESPACE')
|
|
360
|
+
if (plumeriaAliases[objectName] === 'NAMESPACE')
|
|
256
361
|
propName = propertyName;
|
|
257
362
|
}
|
|
258
363
|
else if (utils_1.t.isIdentifier(callee)) {
|
|
@@ -262,26 +367,29 @@ function compileCSS(options) {
|
|
|
262
367
|
}
|
|
263
368
|
if (propName) {
|
|
264
369
|
const args = node.arguments;
|
|
265
|
-
if (propName === '
|
|
370
|
+
if (propName === 'use') {
|
|
371
|
+
extractAndProcessConditionals(args, false);
|
|
372
|
+
}
|
|
373
|
+
else if (propName === 'keyframes' &&
|
|
266
374
|
args.length > 0 &&
|
|
267
375
|
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
268
|
-
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
376
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
269
377
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
270
|
-
scannedTables.keyframesObjectTable[hash] = obj;
|
|
378
|
+
ctx.scannedTables.keyframesObjectTable[hash] = obj;
|
|
271
379
|
}
|
|
272
380
|
else if (propName === 'viewTransition' &&
|
|
273
381
|
args.length > 0 &&
|
|
274
382
|
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
275
|
-
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
383
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
276
384
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
277
|
-
scannedTables.viewTransitionObjectTable[hash] = obj;
|
|
385
|
+
ctx.scannedTables.viewTransitionObjectTable[hash] = obj;
|
|
278
386
|
}
|
|
279
387
|
else if (propName === 'createTheme' &&
|
|
280
388
|
args.length > 0 &&
|
|
281
389
|
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
282
|
-
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
390
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
283
391
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
284
|
-
scannedTables.createThemeObjectTable[hash] = obj;
|
|
392
|
+
ctx.scannedTables.createThemeObjectTable[hash] = obj;
|
|
285
393
|
}
|
|
286
394
|
}
|
|
287
395
|
};
|
|
@@ -304,24 +412,26 @@ function compileCSS(options) {
|
|
|
304
412
|
if (utils_1.t.isMemberExpression(callee) &&
|
|
305
413
|
utils_1.t.isIdentifier(callee.object) &&
|
|
306
414
|
utils_1.t.isIdentifier(callee.property)) {
|
|
307
|
-
if (plumeriaAliases[callee.object.value] ===
|
|
415
|
+
if (plumeriaAliases[callee.object.value] ===
|
|
416
|
+
'NAMESPACE')
|
|
308
417
|
pName = callee.property.value;
|
|
309
418
|
}
|
|
310
|
-
else if (utils_1.t.isIdentifier(callee) &&
|
|
419
|
+
else if (utils_1.t.isIdentifier(callee) &&
|
|
420
|
+
plumeriaAliases[callee.value]) {
|
|
311
421
|
pName = plumeriaAliases[callee.value];
|
|
312
422
|
}
|
|
313
423
|
if (pName &&
|
|
314
424
|
node.init.arguments.length === 1 &&
|
|
315
425
|
utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
|
|
316
426
|
const arg = node.init.arguments[0].expression;
|
|
317
|
-
const resolveVariable = (name) => localCreateStyles[name]?.obj ||
|
|
318
|
-
(mergedCreateThemeHashTable[name]
|
|
319
|
-
? scannedTables.createAtomicMapTable[mergedCreateThemeHashTable[name]]
|
|
427
|
+
const resolveVariable = (name) => ctx.localCreateStyles[name]?.obj ||
|
|
428
|
+
(ctx.mergedCreateThemeHashTable[name]
|
|
429
|
+
? ctx.scannedTables.createAtomicMapTable[ctx.mergedCreateThemeHashTable[name]]
|
|
320
430
|
: undefined);
|
|
321
431
|
if (pName === 'create') {
|
|
322
|
-
const obj = (0, utils_1.objectExpressionToObject)(arg, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, resolveVariable);
|
|
432
|
+
const obj = (0, utils_1.objectExpressionToObject)(arg, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable, resolveVariable);
|
|
323
433
|
if (obj) {
|
|
324
|
-
localCreateStyles[node.id.value] = { type: 'create', obj };
|
|
434
|
+
ctx.localCreateStyles[node.id.value] = { type: 'create', obj };
|
|
325
435
|
Object.entries(obj).forEach(([_, style]) => {
|
|
326
436
|
if (typeof style !== 'object' || style === null)
|
|
327
437
|
return;
|
|
@@ -337,7 +447,7 @@ function compileCSS(options) {
|
|
|
337
447
|
return;
|
|
338
448
|
const key = prop.key.value;
|
|
339
449
|
const params = prop.value.params.map((p) => p.type === 'Identifier' ? p.value : (p.pat?.value ?? 'arg'));
|
|
340
|
-
const tempStaticTable = { ...mergedStaticTable };
|
|
450
|
+
const tempStaticTable = { ...ctx.mergedStaticTable };
|
|
341
451
|
params.forEach((paramName) => {
|
|
342
452
|
tempStaticTable[paramName] = `var(--${key}-${paramName})`;
|
|
343
453
|
});
|
|
@@ -353,7 +463,7 @@ function compileCSS(options) {
|
|
|
353
463
|
}
|
|
354
464
|
if (!actualBody || actualBody.type !== 'ObjectExpression')
|
|
355
465
|
return;
|
|
356
|
-
const substituted = (0, utils_1.objectExpressionToObject)(actualBody, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
466
|
+
const substituted = (0, utils_1.objectExpressionToObject)(actualBody, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
357
467
|
if (!substituted)
|
|
358
468
|
return;
|
|
359
469
|
(0, utils_1.getStyleRecords)(substituted).forEach((r) => extractedSheets.push(r.sheet));
|
|
@@ -361,19 +471,19 @@ function compileCSS(options) {
|
|
|
361
471
|
}
|
|
362
472
|
}
|
|
363
473
|
else if (pName === 'variants') {
|
|
364
|
-
const obj = (0, utils_1.objectExpressionToObject)(arg, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, resolveVariable);
|
|
474
|
+
const obj = (0, utils_1.objectExpressionToObject)(arg, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable, resolveVariable);
|
|
365
475
|
if (obj)
|
|
366
|
-
localCreateStyles[node.id.value] = { type: 'variant', obj };
|
|
476
|
+
ctx.localCreateStyles[node.id.value] = { type: 'variant', obj };
|
|
367
477
|
}
|
|
368
478
|
else if (pName === 'createTheme') {
|
|
369
|
-
const obj = (0, utils_1.objectExpressionToObject)(arg, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
479
|
+
const obj = (0, utils_1.objectExpressionToObject)(arg, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
370
480
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
371
481
|
const uKey = `${resourcePath}-${node.id.value}`;
|
|
372
|
-
scannedTables.createThemeHashTable[uKey] = hash;
|
|
373
|
-
scannedTables.createThemeObjectTable[hash] = obj;
|
|
374
|
-
localCreateStyles[node.id.value] = {
|
|
482
|
+
ctx.scannedTables.createThemeHashTable[uKey] = hash;
|
|
483
|
+
ctx.scannedTables.createThemeObjectTable[hash] = obj;
|
|
484
|
+
ctx.localCreateStyles[node.id.value] = {
|
|
375
485
|
type: 'create',
|
|
376
|
-
obj: scannedTables.createAtomicMapTable[hash],
|
|
486
|
+
obj: ctx.scannedTables.createAtomicMapTable[hash],
|
|
377
487
|
};
|
|
378
488
|
}
|
|
379
489
|
}
|
|
@@ -402,76 +512,7 @@ function compileCSS(options) {
|
|
|
402
512
|
.filter(Boolean)
|
|
403
513
|
.map((el) => ({ expression: el.expression ?? el }))
|
|
404
514
|
: [{ expression: expr }];
|
|
405
|
-
|
|
406
|
-
const styles = extractStylesFromExpression(expression);
|
|
407
|
-
return styles.length === 1
|
|
408
|
-
? styles[0]
|
|
409
|
-
: null;
|
|
410
|
-
};
|
|
411
|
-
const conditionals = [];
|
|
412
|
-
let baseStyle = {};
|
|
413
|
-
for (const arg of args) {
|
|
414
|
-
const argExpr = arg.expression;
|
|
415
|
-
const getSource = (n) => sourceBuffer
|
|
416
|
-
.subarray(n.span.start - baseByteOffset, n.span.end - baseByteOffset)
|
|
417
|
-
.toString('utf-8');
|
|
418
|
-
const collectConditions = (node, testStrings = []) => {
|
|
419
|
-
const staticStyle = resolveStyleObject(node);
|
|
420
|
-
if (staticStyle) {
|
|
421
|
-
if (testStrings.length === 0)
|
|
422
|
-
baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
|
|
423
|
-
else
|
|
424
|
-
conditionals.push({
|
|
425
|
-
test: node,
|
|
426
|
-
testString: testStrings.join(' && '),
|
|
427
|
-
truthy: staticStyle,
|
|
428
|
-
falsy: {},
|
|
429
|
-
});
|
|
430
|
-
return true;
|
|
431
|
-
}
|
|
432
|
-
if (node.type === 'ConditionalExpression') {
|
|
433
|
-
const testSource = getSource(node.test);
|
|
434
|
-
collectConditions(node.consequent, [
|
|
435
|
-
...testStrings,
|
|
436
|
-
`(${testSource})`,
|
|
437
|
-
]);
|
|
438
|
-
collectConditions(node.alternate, [
|
|
439
|
-
...testStrings,
|
|
440
|
-
`!(${testSource})`,
|
|
441
|
-
]);
|
|
442
|
-
return true;
|
|
443
|
-
}
|
|
444
|
-
else if (node.type === 'BinaryExpression' &&
|
|
445
|
-
node.operator === '&&') {
|
|
446
|
-
collectConditions(node.right, [
|
|
447
|
-
...testStrings,
|
|
448
|
-
`(${getSource(node.left)})`,
|
|
449
|
-
]);
|
|
450
|
-
return true;
|
|
451
|
-
}
|
|
452
|
-
else if (node.type === 'ParenthesisExpression') {
|
|
453
|
-
return collectConditions(node.expression, testStrings);
|
|
454
|
-
}
|
|
455
|
-
return false;
|
|
456
|
-
};
|
|
457
|
-
if (collectConditions(argExpr))
|
|
458
|
-
continue;
|
|
459
|
-
const extractedStyles = extractStylesFromExpression(argExpr);
|
|
460
|
-
if (extractedStyles.length > 0) {
|
|
461
|
-
extractedStyles.forEach(processStyle);
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
if (Object.keys(baseStyle).length > 0) {
|
|
465
|
-
processStyle(baseStyle);
|
|
466
|
-
}
|
|
467
|
-
for (const cond of conditionals) {
|
|
468
|
-
if (cond.truthy && Object.keys(cond.truthy).length > 0) {
|
|
469
|
-
processStyle(cond.truthy);
|
|
470
|
-
}
|
|
471
|
-
if (cond.falsy && Object.keys(cond.falsy).length > 0) {
|
|
472
|
-
processStyle(cond.falsy);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
515
|
+
extractAndProcessConditionals(args, true);
|
|
475
516
|
},
|
|
476
517
|
});
|
|
477
518
|
return extractedSheets;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/compiler",
|
|
3
|
-
"version": "10.0.
|
|
3
|
+
"version": "10.0.3",
|
|
4
4
|
"description": "Plumeria swc based compiler for statically extracting css",
|
|
5
5
|
"author": "Refirst 11",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"dist/"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@plumeria/utils": "^10.0.
|
|
24
|
+
"@plumeria/utils": "^10.0.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@rust-gear/glob": "1.0.0",
|