@plumeria/compiler 9.1.2 → 10.0.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/dist/index.js +182 -140
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -178,6 +178,70 @@ function compileCSS(options) {
|
|
|
178
178
|
mergedVariantsTable[key] = importMap[key];
|
|
179
179
|
}
|
|
180
180
|
const localCreateStyles = {};
|
|
181
|
+
const extractStylesFromExpression = (expression) => {
|
|
182
|
+
const results = [];
|
|
183
|
+
if (utils_1.t.isObjectExpression(expression)) {
|
|
184
|
+
const object = (0, utils_1.objectExpressionToObject)(expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
185
|
+
if (object)
|
|
186
|
+
results.push(object);
|
|
187
|
+
}
|
|
188
|
+
else if (utils_1.t.isMemberExpression(expression)) {
|
|
189
|
+
const memberExpr = expression;
|
|
190
|
+
if (utils_1.t.isIdentifier(memberExpr.object) &&
|
|
191
|
+
utils_1.t.isIdentifier(memberExpr.property)) {
|
|
192
|
+
const variableName = memberExpr.object.value;
|
|
193
|
+
const propertyName = memberExpr.property.value;
|
|
194
|
+
const styleSet = localCreateStyles[variableName];
|
|
195
|
+
if (styleSet && styleSet.obj[propertyName]) {
|
|
196
|
+
results.push(styleSet.obj[propertyName]);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const hash = mergedCreateTable[variableName];
|
|
200
|
+
if (hash) {
|
|
201
|
+
const object = scannedTables.createObjectTable[hash];
|
|
202
|
+
if (object && object[propertyName]) {
|
|
203
|
+
results.push(object[propertyName]);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else if (utils_1.t.isIdentifier(expression)) {
|
|
210
|
+
const identifier = expression;
|
|
211
|
+
const object = localCreateStyles[identifier.value];
|
|
212
|
+
if (object)
|
|
213
|
+
results.push(object.obj);
|
|
214
|
+
else {
|
|
215
|
+
const hash = mergedCreateTable[identifier.value];
|
|
216
|
+
if (hash) {
|
|
217
|
+
const objectFromTable = scannedTables.createObjectTable[hash];
|
|
218
|
+
if (objectFromTable)
|
|
219
|
+
results.push(objectFromTable);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
else if (utils_1.t.isConditionalExpression(expression)) {
|
|
224
|
+
const conditionalExpr = expression;
|
|
225
|
+
results.push(...extractStylesFromExpression(conditionalExpr.consequent));
|
|
226
|
+
results.push(...extractStylesFromExpression(conditionalExpr.alternate));
|
|
227
|
+
}
|
|
228
|
+
else if (utils_1.t.isBinaryExpression(expression) &&
|
|
229
|
+
['&&', '||', '??'].includes(expression.operator)) {
|
|
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));
|
|
237
|
+
}
|
|
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
|
+
};
|
|
181
245
|
const processCall = (node) => {
|
|
182
246
|
node._processed = true;
|
|
183
247
|
const callee = node.callee;
|
|
@@ -198,144 +262,7 @@ function compileCSS(options) {
|
|
|
198
262
|
}
|
|
199
263
|
if (propName) {
|
|
200
264
|
const args = node.arguments;
|
|
201
|
-
|
|
202
|
-
const results = [];
|
|
203
|
-
if (utils_1.t.isObjectExpression(expression)) {
|
|
204
|
-
const object = (0, utils_1.objectExpressionToObject)(expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
205
|
-
if (object)
|
|
206
|
-
results.push(object);
|
|
207
|
-
}
|
|
208
|
-
else if (utils_1.t.isMemberExpression(expression)) {
|
|
209
|
-
const memberExpr = expression;
|
|
210
|
-
if (utils_1.t.isIdentifier(memberExpr.object) &&
|
|
211
|
-
utils_1.t.isIdentifier(memberExpr.property)) {
|
|
212
|
-
const variableName = memberExpr.object.value;
|
|
213
|
-
const propertyName = memberExpr.property.value;
|
|
214
|
-
const styleSet = localCreateStyles[variableName];
|
|
215
|
-
if (styleSet && styleSet.obj[propertyName]) {
|
|
216
|
-
results.push(styleSet.obj[propertyName]);
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
const hash = mergedCreateTable[variableName];
|
|
220
|
-
if (hash) {
|
|
221
|
-
const object = scannedTables.createObjectTable[hash];
|
|
222
|
-
if (object && object[propertyName]) {
|
|
223
|
-
results.push(object[propertyName]);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
else if (utils_1.t.isIdentifier(expression)) {
|
|
230
|
-
const identifier = expression;
|
|
231
|
-
const object = localCreateStyles[identifier.value];
|
|
232
|
-
if (object)
|
|
233
|
-
results.push(object.obj);
|
|
234
|
-
else {
|
|
235
|
-
const hash = mergedCreateTable[identifier.value];
|
|
236
|
-
if (hash) {
|
|
237
|
-
const objectFromTable = scannedTables.createObjectTable[hash];
|
|
238
|
-
if (objectFromTable)
|
|
239
|
-
results.push(objectFromTable);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
else if (utils_1.t.isConditionalExpression(expression)) {
|
|
244
|
-
const conditionalExpr = expression;
|
|
245
|
-
results.push(...extractStylesFromExpression(conditionalExpr.consequent));
|
|
246
|
-
results.push(...extractStylesFromExpression(conditionalExpr.alternate));
|
|
247
|
-
}
|
|
248
|
-
else if (utils_1.t.isBinaryExpression(expression) &&
|
|
249
|
-
['&&', '||', '??'].includes(expression.operator)) {
|
|
250
|
-
const binaryExpr = expression;
|
|
251
|
-
results.push(...extractStylesFromExpression(binaryExpr.left));
|
|
252
|
-
results.push(...extractStylesFromExpression(binaryExpr.right));
|
|
253
|
-
}
|
|
254
|
-
else if (expression.type === 'ParenthesisExpression') {
|
|
255
|
-
const parenExpr = expression;
|
|
256
|
-
results.push(...extractStylesFromExpression(parenExpr.expression));
|
|
257
|
-
}
|
|
258
|
-
return results;
|
|
259
|
-
};
|
|
260
|
-
const processStyle = (style) => {
|
|
261
|
-
(0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
|
|
262
|
-
const records = (0, utils_1.getStyleRecords)(style);
|
|
263
|
-
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
264
|
-
};
|
|
265
|
-
if (propName === 'use') {
|
|
266
|
-
const conditionals = [];
|
|
267
|
-
let baseStyle = {};
|
|
268
|
-
const resolveStyleObject = (expression) => {
|
|
269
|
-
const styles = extractStylesFromExpression(expression);
|
|
270
|
-
return styles.length === 1
|
|
271
|
-
? styles[0]
|
|
272
|
-
: null;
|
|
273
|
-
};
|
|
274
|
-
for (const arg of args) {
|
|
275
|
-
const expr = arg.expression;
|
|
276
|
-
const getSource = (node) => sourceBuffer
|
|
277
|
-
.subarray(node.span.start - baseByteOffset, node.span.end - baseByteOffset)
|
|
278
|
-
.toString('utf-8');
|
|
279
|
-
const collectConditions = (node, testStrings = []) => {
|
|
280
|
-
const staticStyle = resolveStyleObject(node);
|
|
281
|
-
if (staticStyle) {
|
|
282
|
-
if (testStrings.length === 0)
|
|
283
|
-
baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
|
|
284
|
-
else
|
|
285
|
-
conditionals.push({
|
|
286
|
-
test: node,
|
|
287
|
-
testString: testStrings.join(' && '),
|
|
288
|
-
truthy: staticStyle,
|
|
289
|
-
falsy: {},
|
|
290
|
-
});
|
|
291
|
-
return true;
|
|
292
|
-
}
|
|
293
|
-
if (node.type === 'ConditionalExpression') {
|
|
294
|
-
const testSource = getSource(node.test);
|
|
295
|
-
collectConditions(node.consequent, [
|
|
296
|
-
...testStrings,
|
|
297
|
-
`(${testSource})`,
|
|
298
|
-
]);
|
|
299
|
-
collectConditions(node.alternate, [
|
|
300
|
-
...testStrings,
|
|
301
|
-
`!(${testSource})`,
|
|
302
|
-
]);
|
|
303
|
-
return true;
|
|
304
|
-
}
|
|
305
|
-
else if (node.type === 'BinaryExpression' &&
|
|
306
|
-
node.operator === '&&') {
|
|
307
|
-
collectConditions(node.right, [
|
|
308
|
-
...testStrings,
|
|
309
|
-
`(${getSource(node.left)})`,
|
|
310
|
-
]);
|
|
311
|
-
return true;
|
|
312
|
-
}
|
|
313
|
-
else if (node.type === 'ParenthesisExpression') {
|
|
314
|
-
return collectConditions(node.expression, testStrings);
|
|
315
|
-
}
|
|
316
|
-
return false;
|
|
317
|
-
};
|
|
318
|
-
if (collectConditions(expr))
|
|
319
|
-
continue;
|
|
320
|
-
const extractedStyles = extractStylesFromExpression(expr);
|
|
321
|
-
if (extractedStyles.length > 0) {
|
|
322
|
-
extractedStyles.forEach(processStyle);
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
if (Object.keys(baseStyle).length > 0) {
|
|
327
|
-
processStyle(baseStyle);
|
|
328
|
-
}
|
|
329
|
-
for (const cond of conditionals) {
|
|
330
|
-
if (cond.truthy && Object.keys(cond.truthy).length > 0) {
|
|
331
|
-
processStyle(cond.truthy);
|
|
332
|
-
}
|
|
333
|
-
if (cond.falsy && Object.keys(cond.falsy).length > 0) {
|
|
334
|
-
processStyle(cond.falsy);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
else if (propName === 'keyframes' &&
|
|
265
|
+
if (propName === 'keyframes' &&
|
|
339
266
|
args.length > 0 &&
|
|
340
267
|
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
341
268
|
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
@@ -395,8 +322,41 @@ function compileCSS(options) {
|
|
|
395
322
|
const obj = (0, utils_1.objectExpressionToObject)(arg, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, resolveVariable);
|
|
396
323
|
if (obj) {
|
|
397
324
|
localCreateStyles[node.id.value] = { type: 'create', obj };
|
|
398
|
-
Object.entries(obj).forEach(([,
|
|
399
|
-
|
|
325
|
+
Object.entries(obj).forEach(([_, style]) => {
|
|
326
|
+
if (typeof style !== 'object' || style === null)
|
|
327
|
+
return;
|
|
328
|
+
(0, utils_1.getStyleRecords)(style).forEach((r) => extractedSheets.push(r.sheet));
|
|
329
|
+
});
|
|
330
|
+
arg.properties.forEach((prop) => {
|
|
331
|
+
if (prop.type !== 'KeyValueProperty' ||
|
|
332
|
+
prop.key.type !== 'Identifier')
|
|
333
|
+
return;
|
|
334
|
+
const isArrow = prop.value.type === 'ArrowFunctionExpression';
|
|
335
|
+
const isFunc = prop.value.type === 'FunctionExpression';
|
|
336
|
+
if (!isArrow && !isFunc)
|
|
337
|
+
return;
|
|
338
|
+
const key = prop.key.value;
|
|
339
|
+
const params = prop.value.params.map((p) => p.type === 'Identifier' ? p.value : (p.pat?.value ?? 'arg'));
|
|
340
|
+
const tempStaticTable = { ...mergedStaticTable };
|
|
341
|
+
params.forEach((paramName) => {
|
|
342
|
+
tempStaticTable[paramName] = `var(--${key}-${paramName})`;
|
|
343
|
+
});
|
|
344
|
+
let actualBody = prop.value.body;
|
|
345
|
+
if (actualBody?.type === 'ParenthesisExpression')
|
|
346
|
+
actualBody = actualBody.expression;
|
|
347
|
+
if (actualBody?.type === 'BlockStatement') {
|
|
348
|
+
const first = actualBody.stmts?.[0];
|
|
349
|
+
if (first?.type === 'ReturnStatement')
|
|
350
|
+
actualBody = first.argument;
|
|
351
|
+
if (actualBody?.type === 'ParenthesisExpression')
|
|
352
|
+
actualBody = actualBody.expression;
|
|
353
|
+
}
|
|
354
|
+
if (!actualBody || actualBody.type !== 'ObjectExpression')
|
|
355
|
+
return;
|
|
356
|
+
const substituted = (0, utils_1.objectExpressionToObject)(actualBody, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
357
|
+
if (!substituted)
|
|
358
|
+
return;
|
|
359
|
+
(0, utils_1.getStyleRecords)(substituted).forEach((r) => extractedSheets.push(r.sheet));
|
|
400
360
|
});
|
|
401
361
|
}
|
|
402
362
|
}
|
|
@@ -431,6 +391,88 @@ function compileCSS(options) {
|
|
|
431
391
|
if (!path.node._processed)
|
|
432
392
|
processCall(path.node);
|
|
433
393
|
},
|
|
394
|
+
JSXAttribute({ node }) {
|
|
395
|
+
if (node.name?.value !== 'styleName')
|
|
396
|
+
return;
|
|
397
|
+
if (!node.value || node.value.type !== 'JSXExpressionContainer')
|
|
398
|
+
return;
|
|
399
|
+
const expr = node.value.expression;
|
|
400
|
+
const args = expr.type === 'ArrayExpression'
|
|
401
|
+
? expr.elements
|
|
402
|
+
.filter(Boolean)
|
|
403
|
+
.map((el) => ({ expression: el.expression ?? el }))
|
|
404
|
+
: [{ expression: expr }];
|
|
405
|
+
const resolveStyleObject = (expression) => {
|
|
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
|
+
}
|
|
475
|
+
},
|
|
434
476
|
});
|
|
435
477
|
return extractedSheets;
|
|
436
478
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/compiler",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
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": "^
|
|
24
|
+
"@plumeria/utils": "^10.0.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@rust-gear/glob": "1.0.0",
|