@plumeria/compiler 18.1.4 → 18.1.6
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 +105 -39
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -41,6 +41,37 @@ const path = __importStar(require("path"));
|
|
|
41
41
|
const rs = __importStar(require("@rust-gear/glob"));
|
|
42
42
|
const utils_1 = require("@plumeria/utils");
|
|
43
43
|
const utils_2 = require("@plumeria/utils");
|
|
44
|
+
const isStaticArgValue = (node) => node.type === 'StringLiteral' ||
|
|
45
|
+
node.type === 'NumericLiteral' ||
|
|
46
|
+
node.type === 'BooleanLiteral' ||
|
|
47
|
+
(node.type === 'TemplateLiteral' && node.expressions.length === 0) ||
|
|
48
|
+
utils_1.t.isIdentifier(node) ||
|
|
49
|
+
utils_1.t.isMemberExpression(node);
|
|
50
|
+
const namedParamsOf = (params) => {
|
|
51
|
+
if (params.length !== 1)
|
|
52
|
+
return undefined;
|
|
53
|
+
const first = params[0];
|
|
54
|
+
const pattern = (first?.pat ?? first);
|
|
55
|
+
if (pattern?.type !== 'ObjectPattern')
|
|
56
|
+
return undefined;
|
|
57
|
+
const named = [];
|
|
58
|
+
for (const prop of pattern.properties ?? []) {
|
|
59
|
+
if (prop.type === 'AssignmentPatternProperty' &&
|
|
60
|
+
utils_1.t.isIdentifier(prop.key) &&
|
|
61
|
+
!prop.value) {
|
|
62
|
+
named.push({ key: prop.key.value, local: prop.key.value });
|
|
63
|
+
}
|
|
64
|
+
else if (prop.type === 'KeyValuePatternProperty' &&
|
|
65
|
+
utils_1.t.isIdentifier(prop.key) &&
|
|
66
|
+
utils_1.t.isIdentifier(prop.value)) {
|
|
67
|
+
named.push({ key: prop.key.value, local: prop.value.value });
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return named.length > 0 ? named : undefined;
|
|
74
|
+
};
|
|
44
75
|
function extractStylesFromExpression(expression, ctx) {
|
|
45
76
|
let expr = expression;
|
|
46
77
|
if (ctx.localStyleAliases &&
|
|
@@ -464,6 +495,74 @@ function compileCSS(options) {
|
|
|
464
495
|
return null;
|
|
465
496
|
return Object.values(obj).filter((v) => typeof v === 'object' && v !== null);
|
|
466
497
|
};
|
|
498
|
+
const resolveDynamicCall = (expr) => {
|
|
499
|
+
if (!utils_1.t.isCallExpression(expr) || !utils_1.t.isMemberExpression(expr.callee))
|
|
500
|
+
return null;
|
|
501
|
+
const callee = expr.callee;
|
|
502
|
+
if (!utils_1.t.isIdentifier(callee.object) || !utils_1.t.isIdentifier(callee.property))
|
|
503
|
+
return null;
|
|
504
|
+
const styleInfo = ctx.localCreateStyles[callee.object.value];
|
|
505
|
+
const func = styleInfo?.functions?.[callee.property.value];
|
|
506
|
+
if (!func)
|
|
507
|
+
return null;
|
|
508
|
+
const callArgs = expr.arguments;
|
|
509
|
+
if (callArgs.some((a) => a.spread) || callArgs.length === 0)
|
|
510
|
+
return null;
|
|
511
|
+
const tempStaticTable = { ...ctx.mergedStaticTable };
|
|
512
|
+
const runtime = [];
|
|
513
|
+
if (func.named) {
|
|
514
|
+
const argExpr = callArgs[0].expression;
|
|
515
|
+
if (callArgs.length !== 1 || argExpr.type !== 'ObjectExpression') {
|
|
516
|
+
throw new Error(`[plumeria] ${getSource(expr)} takes one object argument, because ${callee.property.value} destructures its parameter.\n`);
|
|
517
|
+
}
|
|
518
|
+
const given = new Map();
|
|
519
|
+
argExpr.properties.forEach((prop) => {
|
|
520
|
+
if (prop.type === 'Identifier') {
|
|
521
|
+
given.set(prop.value, prop);
|
|
522
|
+
}
|
|
523
|
+
else if (prop.type === 'KeyValueProperty' &&
|
|
524
|
+
(utils_1.t.isIdentifier(prop.key) || utils_1.t.isStringLiteral(prop.key))) {
|
|
525
|
+
given.set(String(prop.key.value), prop.value);
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
const argObj = (0, utils_1.objectExpressionToObject)(argExpr, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable) ?? {};
|
|
529
|
+
func.named.forEach(({ key, local }) => {
|
|
530
|
+
const source = given.get(key);
|
|
531
|
+
if (!source) {
|
|
532
|
+
throw new Error(`[plumeria] ${getSource(expr)} leaves "${key}" unset, and a dynamic style function has no value to fall back on.\n`);
|
|
533
|
+
}
|
|
534
|
+
if (isStaticArgValue(source) && argObj[key] !== undefined)
|
|
535
|
+
tempStaticTable[local] = argObj[key];
|
|
536
|
+
else
|
|
537
|
+
runtime.push(local);
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
else if (callArgs.length === 1 &&
|
|
541
|
+
callArgs[0].expression.type === 'ObjectExpression') {
|
|
542
|
+
const argObj = (0, utils_1.objectExpressionToObject)(callArgs[0].expression, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
543
|
+
func.params.forEach((p) => {
|
|
544
|
+
if (argObj[p] !== undefined)
|
|
545
|
+
tempStaticTable[p] = argObj[p];
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
callArgs.forEach((_callArg, i) => {
|
|
550
|
+
const p = func.params[i];
|
|
551
|
+
if (!p)
|
|
552
|
+
return;
|
|
553
|
+
runtime.push(p);
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
if (runtime.length > 0) {
|
|
557
|
+
runtime.forEach((p) => (tempStaticTable[p] = p));
|
|
558
|
+
const probe = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
559
|
+
const hash = (0, zss_engine_1.genBase36Hash)(probe ?? {}, 1, 8);
|
|
560
|
+
runtime.forEach((p) => {
|
|
561
|
+
tempStaticTable[p] = `var(--${hash}-${p})`;
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
return ((0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable) ?? null);
|
|
565
|
+
};
|
|
467
566
|
const collectConditions = (node, currentTestStrings = []) => {
|
|
468
567
|
if (node.type === 'ConditionalExpression') {
|
|
469
568
|
const testSource = getSource(node.test);
|
|
@@ -501,7 +600,7 @@ function compileCSS(options) {
|
|
|
501
600
|
else if (node.type === 'ParenthesisExpression') {
|
|
502
601
|
return collectConditions(node.expression, currentTestStrings);
|
|
503
602
|
}
|
|
504
|
-
const staticStyle = resolveStyleObject(node);
|
|
603
|
+
const staticStyle = resolveStyleObject(node) ?? resolveDynamicCall(node);
|
|
505
604
|
if (staticStyle) {
|
|
506
605
|
if (currentTestStrings.length === 0) {
|
|
507
606
|
baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
|
|
@@ -605,44 +704,10 @@ function compileCSS(options) {
|
|
|
605
704
|
continue;
|
|
606
705
|
}
|
|
607
706
|
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
utils_1.t.isIdentifier(callee.property)) {
|
|
613
|
-
const varName = callee.object.value;
|
|
614
|
-
const propName = callee.property.value;
|
|
615
|
-
const styleInfo = ctx.localCreateStyles[varName];
|
|
616
|
-
if (styleInfo && styleInfo.functions?.[propName]) {
|
|
617
|
-
const func = styleInfo.functions[propName];
|
|
618
|
-
const callArgs = expr.arguments;
|
|
619
|
-
const hasSpread = callArgs.some((a) => a.spread);
|
|
620
|
-
if (!hasSpread && callArgs.length >= 1) {
|
|
621
|
-
const tempStaticTable = { ...ctx.mergedStaticTable };
|
|
622
|
-
if (callArgs.length === 1 &&
|
|
623
|
-
callArgs[0].expression.type === 'ObjectExpression') {
|
|
624
|
-
const argObj = (0, utils_1.objectExpressionToObject)(callArgs[0].expression, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
625
|
-
func.params.forEach((p) => {
|
|
626
|
-
if (argObj[p] !== undefined)
|
|
627
|
-
tempStaticTable[p] = argObj[p];
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
else {
|
|
631
|
-
callArgs.forEach((_callArg, i) => {
|
|
632
|
-
const p = func.params[i];
|
|
633
|
-
if (!p)
|
|
634
|
-
return;
|
|
635
|
-
tempStaticTable[p] = `var(--${propName}-${p})`;
|
|
636
|
-
});
|
|
637
|
-
}
|
|
638
|
-
const resolved = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
|
|
639
|
-
if (resolved)
|
|
640
|
-
baseStyle = (0, utils_1.deepMerge)(baseStyle, resolved);
|
|
641
|
-
continue;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
}
|
|
707
|
+
const dynamicStyle = resolveDynamicCall(expr);
|
|
708
|
+
if (dynamicStyle) {
|
|
709
|
+
baseStyle = (0, utils_1.deepMerge)(baseStyle, dynamicStyle);
|
|
710
|
+
continue;
|
|
646
711
|
}
|
|
647
712
|
if (collectConditions(arg.expression))
|
|
648
713
|
continue;
|
|
@@ -799,6 +864,7 @@ function compileCSS(options) {
|
|
|
799
864
|
if (actualBody && actualBody.type === 'ObjectExpression') {
|
|
800
865
|
styleFunctions[prop.key.value] = {
|
|
801
866
|
params,
|
|
867
|
+
named: namedParamsOf(func.params),
|
|
802
868
|
body: actualBody,
|
|
803
869
|
};
|
|
804
870
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/compiler",
|
|
3
|
-
"version": "18.1.
|
|
3
|
+
"version": "18.1.6",
|
|
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": "^18.1.
|
|
24
|
+
"@plumeria/utils": "^18.1.6"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@rust-gear/glob": "1.1.0",
|