@plumeria/unplugin 18.2.25 → 18.2.26
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/core.js +86 -34
- package/dist/core.mjs +86 -34
- package/package.json +2 -2
package/dist/core.js
CHANGED
|
@@ -47,7 +47,7 @@ const isStaticArgValue = (node) => node.type === 'StringLiteral' ||
|
|
|
47
47
|
(node.type === 'TemplateLiteral' && node.expressions.length === 0) ||
|
|
48
48
|
utils_1.t.isIdentifier(node) ||
|
|
49
49
|
utils_1.t.isMemberExpression(node);
|
|
50
|
-
const namedParamsOf = (params) => {
|
|
50
|
+
const namedParamsOf = (params, defaults) => {
|
|
51
51
|
if (params.length !== 1)
|
|
52
52
|
return undefined;
|
|
53
53
|
const first = params[0];
|
|
@@ -56,16 +56,24 @@ const namedParamsOf = (params) => {
|
|
|
56
56
|
return undefined;
|
|
57
57
|
const named = [];
|
|
58
58
|
for (const prop of pattern.properties ?? []) {
|
|
59
|
-
if (prop.type === 'AssignmentPatternProperty' &&
|
|
60
|
-
utils_1.t.isIdentifier(prop.key) &&
|
|
61
|
-
!prop.value) {
|
|
59
|
+
if (prop.type === 'AssignmentPatternProperty' && utils_1.t.isIdentifier(prop.key)) {
|
|
62
60
|
named.push({ key: prop.key.value, local: prop.key.value });
|
|
61
|
+
if (prop.value)
|
|
62
|
+
defaults[prop.key.value] = prop.value;
|
|
63
63
|
}
|
|
64
64
|
else if (prop.type === 'KeyValuePatternProperty' &&
|
|
65
65
|
utils_1.t.isIdentifier(prop.key) &&
|
|
66
66
|
utils_1.t.isIdentifier(prop.value)) {
|
|
67
67
|
named.push({ key: prop.key.value, local: prop.value.value });
|
|
68
68
|
}
|
|
69
|
+
else if (prop.type === 'KeyValuePatternProperty' &&
|
|
70
|
+
utils_1.t.isIdentifier(prop.key) &&
|
|
71
|
+
prop.value?.type === 'AssignmentPattern' &&
|
|
72
|
+
utils_1.t.isIdentifier(prop.value.left)) {
|
|
73
|
+
const pattern = prop.value;
|
|
74
|
+
named.push({ key: String(prop.key.value), local: pattern.left.value });
|
|
75
|
+
defaults[pattern.left.value] = pattern.right;
|
|
76
|
+
}
|
|
69
77
|
else {
|
|
70
78
|
return undefined;
|
|
71
79
|
}
|
|
@@ -81,14 +89,16 @@ const styleFunctionsOf = (objExpr) => {
|
|
|
81
89
|
if (func.type !== 'ArrowFunctionExpression' &&
|
|
82
90
|
func.type !== 'FunctionExpression')
|
|
83
91
|
return;
|
|
92
|
+
const defaults = {};
|
|
84
93
|
const params = func.params.map((p) => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return
|
|
94
|
+
const pattern = typeof p === 'object' && p !== null && 'pat' in p ? p.pat : p;
|
|
95
|
+
if (utils_1.t.isIdentifier(pattern))
|
|
96
|
+
return pattern.value;
|
|
97
|
+
if (pattern?.type === 'AssignmentPattern' &&
|
|
98
|
+
utils_1.t.isIdentifier(pattern.left)) {
|
|
99
|
+
defaults[pattern.left.value] = pattern.right;
|
|
100
|
+
return pattern.left.value;
|
|
101
|
+
}
|
|
92
102
|
return 'arg';
|
|
93
103
|
});
|
|
94
104
|
let actualBody = func.body;
|
|
@@ -104,7 +114,8 @@ const styleFunctionsOf = (objExpr) => {
|
|
|
104
114
|
if (actualBody && actualBody.type === 'ObjectExpression') {
|
|
105
115
|
styleFunctions[prop.key.value] = {
|
|
106
116
|
params,
|
|
107
|
-
named: namedParamsOf(func.params),
|
|
117
|
+
named: namedParamsOf(func.params, defaults),
|
|
118
|
+
defaults,
|
|
108
119
|
body: actualBody,
|
|
109
120
|
};
|
|
110
121
|
}
|
|
@@ -132,6 +143,19 @@ const foldDynamicVars = (vars) => {
|
|
|
132
143
|
return `"${cssVar}": ${value}`;
|
|
133
144
|
});
|
|
134
145
|
};
|
|
146
|
+
const replaceVarValue = (style, cssVar, next) => {
|
|
147
|
+
for (const [prop, value] of Object.entries(style)) {
|
|
148
|
+
if (typeof value === 'string' && value.includes(cssVar)) {
|
|
149
|
+
style[prop] = next;
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
if (value !== null && typeof value === 'object') {
|
|
153
|
+
if (replaceVarValue(value, cssVar, next))
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
};
|
|
135
159
|
const findVarProp = (style, cssVar) => {
|
|
136
160
|
for (const [prop, value] of Object.entries(style)) {
|
|
137
161
|
if (typeof value === 'string' && value.includes(cssVar))
|
|
@@ -309,11 +333,12 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
309
333
|
}
|
|
310
334
|
}
|
|
311
335
|
const appliedStyleProps = new Set();
|
|
336
|
+
const ownerComponentOf = (at) => components.find((c) => at.span.start >= c.node.span.start &&
|
|
337
|
+
at.span.start <= c.node.span.end)?.name;
|
|
312
338
|
const markStylePropApplied = (name, at) => {
|
|
313
|
-
const owner =
|
|
314
|
-
at.span.start <= c.node.span.end);
|
|
339
|
+
const owner = ownerComponentOf(at);
|
|
315
340
|
if (owner) {
|
|
316
|
-
appliedStyleProps.add(`${owner
|
|
341
|
+
appliedStyleProps.add(`${owner}:${name}`);
|
|
317
342
|
}
|
|
318
343
|
};
|
|
319
344
|
const extractedSheets = [];
|
|
@@ -747,7 +772,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
747
772
|
}
|
|
748
773
|
if (propName) {
|
|
749
774
|
const args = node.arguments;
|
|
750
|
-
if (propName === 'keyframes') {
|
|
775
|
+
if (propName === 'keyframes' && args.length > 0) {
|
|
751
776
|
const expr = args[0].expression;
|
|
752
777
|
if (utils_1.t.isObjectExpression(expr)) {
|
|
753
778
|
const obj = (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
@@ -912,18 +937,19 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
912
937
|
if (!func)
|
|
913
938
|
return null;
|
|
914
939
|
const callArgs = expr.arguments;
|
|
915
|
-
if (callArgs.some((a) => a.spread)
|
|
940
|
+
if (callArgs.some((a) => a.spread))
|
|
916
941
|
return null;
|
|
917
942
|
const tempStaticTable = { ...mergedStaticTable };
|
|
918
943
|
const runtime = [];
|
|
919
944
|
const resolveObjectArg = (argExpr) => (0, utils_1.objectExpressionToObject)(argExpr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
920
945
|
if (func.named) {
|
|
921
|
-
const argExpr = callArgs[0]
|
|
922
|
-
if (callArgs.length
|
|
946
|
+
const argExpr = callArgs[0]?.expression;
|
|
947
|
+
if (callArgs.length > 1 ||
|
|
948
|
+
(argExpr && argExpr.type !== 'ObjectExpression')) {
|
|
923
949
|
throwCompilationError(`Plumeria: ${getSource(expr)} takes one object argument, because ${callee.property.value} destructures its parameter.`, expr);
|
|
924
950
|
}
|
|
925
951
|
const given = new Map();
|
|
926
|
-
argExpr
|
|
952
|
+
(argExpr?.properties ?? []).forEach((prop) => {
|
|
927
953
|
if (prop.type === 'Identifier') {
|
|
928
954
|
given.set(prop.value, prop);
|
|
929
955
|
}
|
|
@@ -936,6 +962,8 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
936
962
|
func.named.forEach(({ key, local }) => {
|
|
937
963
|
const source = given.get(key);
|
|
938
964
|
if (!source) {
|
|
965
|
+
if (func.defaults?.[local])
|
|
966
|
+
return;
|
|
939
967
|
throwCompilationError(`Plumeria: ${getSource(expr)} leaves "${key}" unset, and a dynamic style function has no value to fall back on.`, expr);
|
|
940
968
|
return;
|
|
941
969
|
}
|
|
@@ -961,12 +989,17 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
961
989
|
runtime.push({ param: p, source: callArg.expression });
|
|
962
990
|
});
|
|
963
991
|
}
|
|
992
|
+
const withDefault = Object.entries(func.defaults ?? {}).filter(([param]) => tempStaticTable[param] === undefined);
|
|
964
993
|
const cssVars = {};
|
|
965
|
-
|
|
966
|
-
runtime.
|
|
994
|
+
const varParams = Array.from(new Set([
|
|
995
|
+
...runtime.map(({ param }) => param),
|
|
996
|
+
...withDefault.map(([param]) => param),
|
|
997
|
+
]));
|
|
998
|
+
if (varParams.length > 0) {
|
|
999
|
+
varParams.forEach((param) => (tempStaticTable[param] = param));
|
|
967
1000
|
const probe = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
968
1001
|
const hash = (0, zss_engine_1.genBase36Hash)(probe ?? {}, 1, 8);
|
|
969
|
-
|
|
1002
|
+
varParams.forEach((param) => {
|
|
970
1003
|
const cssVar = `--${hash}-${param}`;
|
|
971
1004
|
tempStaticTable[param] = `var(${cssVar})`;
|
|
972
1005
|
cssVars[param] = cssVar;
|
|
@@ -975,6 +1008,18 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
975
1008
|
const style = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
976
1009
|
if (!style)
|
|
977
1010
|
return null;
|
|
1011
|
+
withDefault.forEach(([param, expr]) => {
|
|
1012
|
+
const cssVar = cssVars[param];
|
|
1013
|
+
const targetProp = cssVar ? findVarProp(style, cssVar) : undefined;
|
|
1014
|
+
if (!targetProp)
|
|
1015
|
+
return;
|
|
1016
|
+
const literal = expr.type === 'StringLiteral' || expr.type === 'NumericLiteral'
|
|
1017
|
+
? expr.value
|
|
1018
|
+
: undefined;
|
|
1019
|
+
if (literal === undefined)
|
|
1020
|
+
return;
|
|
1021
|
+
replaceVarValue(style, cssVar, `var(${cssVar}, ${(0, zss_engine_1.applyCssValue)(literal, (0, zss_engine_1.camelToKebabCase)(targetProp))})`);
|
|
1022
|
+
});
|
|
978
1023
|
const vars = [];
|
|
979
1024
|
runtime.forEach(({ param, source }) => {
|
|
980
1025
|
const cssVar = cssVars[param];
|
|
@@ -1216,13 +1261,18 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1216
1261
|
: expr.property.value;
|
|
1217
1262
|
const source = utils_1.t.isIdentifier(expr) ? varName : getSource(expr);
|
|
1218
1263
|
markStylePropApplied(varName, expr);
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1264
|
+
const owner = ownerComponentOf(expr);
|
|
1265
|
+
let possibilities = owner
|
|
1266
|
+
? scannedTables.componentPropsTable?.[`${resourcePath}-${owner}`]?.[varName]
|
|
1267
|
+
: undefined;
|
|
1268
|
+
if (!possibilities) {
|
|
1269
|
+
for (const key of Object.keys(scannedTables.componentPropsTable || {})) {
|
|
1270
|
+
if (!key.startsWith(`${resourcePath}-`))
|
|
1271
|
+
continue;
|
|
1272
|
+
if (scannedTables.componentPropsTable?.[key]?.[varName]) {
|
|
1273
|
+
possibilities = scannedTables.componentPropsTable[key][varName];
|
|
1274
|
+
break;
|
|
1275
|
+
}
|
|
1226
1276
|
}
|
|
1227
1277
|
}
|
|
1228
1278
|
if (!possibilities || possibilities.length === 0)
|
|
@@ -1605,7 +1655,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1605
1655
|
});
|
|
1606
1656
|
if (Object.keys(lookupMap).length > 0) {
|
|
1607
1657
|
const entries = Object.entries(lookupMap)
|
|
1608
|
-
.map(([k, v]) =>
|
|
1658
|
+
.map(([k, v]) => `${JSON.stringify(k)}:${JSON.stringify(v)}`)
|
|
1609
1659
|
.join(',');
|
|
1610
1660
|
classParts.push(`({${entries}}[${commonTestExpr}] || "")`);
|
|
1611
1661
|
}
|
|
@@ -2091,9 +2141,8 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
2091
2141
|
throwCompilationError(`Plumeria: css.use(${getSource(expr)}) does not support dynamic function keys.`, expr);
|
|
2092
2142
|
}
|
|
2093
2143
|
}
|
|
2094
|
-
const { classParts, isOptimizable
|
|
2095
|
-
if (isOptimizable
|
|
2096
|
-
(args.length > 0 || Object.keys(baseStyle).length > 0)) {
|
|
2144
|
+
const { classParts, isOptimizable } = buildClassParts(args);
|
|
2145
|
+
if (isOptimizable) {
|
|
2097
2146
|
const replacement = classParts.length > 0 ? classParts.join(' + " " + ') : '""';
|
|
2098
2147
|
replacements.push({
|
|
2099
2148
|
start: node.span.start - baseByteOffset,
|
|
@@ -2101,6 +2150,9 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
2101
2150
|
content: replacement,
|
|
2102
2151
|
});
|
|
2103
2152
|
}
|
|
2153
|
+
else {
|
|
2154
|
+
throwCompilationError(`Plumeria: Dynamic or unresolvable style object "${getSource(node)}" is not supported.`, node);
|
|
2155
|
+
}
|
|
2104
2156
|
},
|
|
2105
2157
|
});
|
|
2106
2158
|
for (const { name, node } of components) {
|
package/dist/core.mjs
CHANGED
|
@@ -11,7 +11,7 @@ const isStaticArgValue = (node) => node.type === 'StringLiteral' ||
|
|
|
11
11
|
(node.type === 'TemplateLiteral' && node.expressions.length === 0) ||
|
|
12
12
|
t.isIdentifier(node) ||
|
|
13
13
|
t.isMemberExpression(node);
|
|
14
|
-
const namedParamsOf = (params) => {
|
|
14
|
+
const namedParamsOf = (params, defaults) => {
|
|
15
15
|
if (params.length !== 1)
|
|
16
16
|
return undefined;
|
|
17
17
|
const first = params[0];
|
|
@@ -20,16 +20,24 @@ const namedParamsOf = (params) => {
|
|
|
20
20
|
return undefined;
|
|
21
21
|
const named = [];
|
|
22
22
|
for (const prop of pattern.properties ?? []) {
|
|
23
|
-
if (prop.type === 'AssignmentPatternProperty' &&
|
|
24
|
-
t.isIdentifier(prop.key) &&
|
|
25
|
-
!prop.value) {
|
|
23
|
+
if (prop.type === 'AssignmentPatternProperty' && t.isIdentifier(prop.key)) {
|
|
26
24
|
named.push({ key: prop.key.value, local: prop.key.value });
|
|
25
|
+
if (prop.value)
|
|
26
|
+
defaults[prop.key.value] = prop.value;
|
|
27
27
|
}
|
|
28
28
|
else if (prop.type === 'KeyValuePatternProperty' &&
|
|
29
29
|
t.isIdentifier(prop.key) &&
|
|
30
30
|
t.isIdentifier(prop.value)) {
|
|
31
31
|
named.push({ key: prop.key.value, local: prop.value.value });
|
|
32
32
|
}
|
|
33
|
+
else if (prop.type === 'KeyValuePatternProperty' &&
|
|
34
|
+
t.isIdentifier(prop.key) &&
|
|
35
|
+
prop.value?.type === 'AssignmentPattern' &&
|
|
36
|
+
t.isIdentifier(prop.value.left)) {
|
|
37
|
+
const pattern = prop.value;
|
|
38
|
+
named.push({ key: String(prop.key.value), local: pattern.left.value });
|
|
39
|
+
defaults[pattern.left.value] = pattern.right;
|
|
40
|
+
}
|
|
33
41
|
else {
|
|
34
42
|
return undefined;
|
|
35
43
|
}
|
|
@@ -45,14 +53,16 @@ const styleFunctionsOf = (objExpr) => {
|
|
|
45
53
|
if (func.type !== 'ArrowFunctionExpression' &&
|
|
46
54
|
func.type !== 'FunctionExpression')
|
|
47
55
|
return;
|
|
56
|
+
const defaults = {};
|
|
48
57
|
const params = func.params.map((p) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return
|
|
58
|
+
const pattern = typeof p === 'object' && p !== null && 'pat' in p ? p.pat : p;
|
|
59
|
+
if (t.isIdentifier(pattern))
|
|
60
|
+
return pattern.value;
|
|
61
|
+
if (pattern?.type === 'AssignmentPattern' &&
|
|
62
|
+
t.isIdentifier(pattern.left)) {
|
|
63
|
+
defaults[pattern.left.value] = pattern.right;
|
|
64
|
+
return pattern.left.value;
|
|
65
|
+
}
|
|
56
66
|
return 'arg';
|
|
57
67
|
});
|
|
58
68
|
let actualBody = func.body;
|
|
@@ -68,7 +78,8 @@ const styleFunctionsOf = (objExpr) => {
|
|
|
68
78
|
if (actualBody && actualBody.type === 'ObjectExpression') {
|
|
69
79
|
styleFunctions[prop.key.value] = {
|
|
70
80
|
params,
|
|
71
|
-
named: namedParamsOf(func.params),
|
|
81
|
+
named: namedParamsOf(func.params, defaults),
|
|
82
|
+
defaults,
|
|
72
83
|
body: actualBody,
|
|
73
84
|
};
|
|
74
85
|
}
|
|
@@ -96,6 +107,19 @@ const foldDynamicVars = (vars) => {
|
|
|
96
107
|
return `"${cssVar}": ${value}`;
|
|
97
108
|
});
|
|
98
109
|
};
|
|
110
|
+
const replaceVarValue = (style, cssVar, next) => {
|
|
111
|
+
for (const [prop, value] of Object.entries(style)) {
|
|
112
|
+
if (typeof value === 'string' && value.includes(cssVar)) {
|
|
113
|
+
style[prop] = next;
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
if (value !== null && typeof value === 'object') {
|
|
117
|
+
if (replaceVarValue(value, cssVar, next))
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
};
|
|
99
123
|
const findVarProp = (style, cssVar) => {
|
|
100
124
|
for (const [prop, value] of Object.entries(style)) {
|
|
101
125
|
if (typeof value === 'string' && value.includes(cssVar))
|
|
@@ -273,11 +297,12 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
273
297
|
}
|
|
274
298
|
}
|
|
275
299
|
const appliedStyleProps = new Set();
|
|
300
|
+
const ownerComponentOf = (at) => components.find((c) => at.span.start >= c.node.span.start &&
|
|
301
|
+
at.span.start <= c.node.span.end)?.name;
|
|
276
302
|
const markStylePropApplied = (name, at) => {
|
|
277
|
-
const owner =
|
|
278
|
-
at.span.start <= c.node.span.end);
|
|
303
|
+
const owner = ownerComponentOf(at);
|
|
279
304
|
if (owner) {
|
|
280
|
-
appliedStyleProps.add(`${owner
|
|
305
|
+
appliedStyleProps.add(`${owner}:${name}`);
|
|
281
306
|
}
|
|
282
307
|
};
|
|
283
308
|
const extractedSheets = [];
|
|
@@ -711,7 +736,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
711
736
|
}
|
|
712
737
|
if (propName) {
|
|
713
738
|
const args = node.arguments;
|
|
714
|
-
if (propName === 'keyframes') {
|
|
739
|
+
if (propName === 'keyframes' && args.length > 0) {
|
|
715
740
|
const expr = args[0].expression;
|
|
716
741
|
if (t.isObjectExpression(expr)) {
|
|
717
742
|
const obj = objectExpressionToObject(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
@@ -876,18 +901,19 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
876
901
|
if (!func)
|
|
877
902
|
return null;
|
|
878
903
|
const callArgs = expr.arguments;
|
|
879
|
-
if (callArgs.some((a) => a.spread)
|
|
904
|
+
if (callArgs.some((a) => a.spread))
|
|
880
905
|
return null;
|
|
881
906
|
const tempStaticTable = { ...mergedStaticTable };
|
|
882
907
|
const runtime = [];
|
|
883
908
|
const resolveObjectArg = (argExpr) => objectExpressionToObject(argExpr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
884
909
|
if (func.named) {
|
|
885
|
-
const argExpr = callArgs[0]
|
|
886
|
-
if (callArgs.length
|
|
910
|
+
const argExpr = callArgs[0]?.expression;
|
|
911
|
+
if (callArgs.length > 1 ||
|
|
912
|
+
(argExpr && argExpr.type !== 'ObjectExpression')) {
|
|
887
913
|
throwCompilationError(`Plumeria: ${getSource(expr)} takes one object argument, because ${callee.property.value} destructures its parameter.`, expr);
|
|
888
914
|
}
|
|
889
915
|
const given = new Map();
|
|
890
|
-
argExpr
|
|
916
|
+
(argExpr?.properties ?? []).forEach((prop) => {
|
|
891
917
|
if (prop.type === 'Identifier') {
|
|
892
918
|
given.set(prop.value, prop);
|
|
893
919
|
}
|
|
@@ -900,6 +926,8 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
900
926
|
func.named.forEach(({ key, local }) => {
|
|
901
927
|
const source = given.get(key);
|
|
902
928
|
if (!source) {
|
|
929
|
+
if (func.defaults?.[local])
|
|
930
|
+
return;
|
|
903
931
|
throwCompilationError(`Plumeria: ${getSource(expr)} leaves "${key}" unset, and a dynamic style function has no value to fall back on.`, expr);
|
|
904
932
|
return;
|
|
905
933
|
}
|
|
@@ -925,12 +953,17 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
925
953
|
runtime.push({ param: p, source: callArg.expression });
|
|
926
954
|
});
|
|
927
955
|
}
|
|
956
|
+
const withDefault = Object.entries(func.defaults ?? {}).filter(([param]) => tempStaticTable[param] === undefined);
|
|
928
957
|
const cssVars = {};
|
|
929
|
-
|
|
930
|
-
runtime.
|
|
958
|
+
const varParams = Array.from(new Set([
|
|
959
|
+
...runtime.map(({ param }) => param),
|
|
960
|
+
...withDefault.map(([param]) => param),
|
|
961
|
+
]));
|
|
962
|
+
if (varParams.length > 0) {
|
|
963
|
+
varParams.forEach((param) => (tempStaticTable[param] = param));
|
|
931
964
|
const probe = objectExpressionToObject(func.body, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
932
965
|
const hash = genBase36Hash(probe ?? {}, 1, 8);
|
|
933
|
-
|
|
966
|
+
varParams.forEach((param) => {
|
|
934
967
|
const cssVar = `--${hash}-${param}`;
|
|
935
968
|
tempStaticTable[param] = `var(${cssVar})`;
|
|
936
969
|
cssVars[param] = cssVar;
|
|
@@ -939,6 +972,18 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
939
972
|
const style = objectExpressionToObject(func.body, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
940
973
|
if (!style)
|
|
941
974
|
return null;
|
|
975
|
+
withDefault.forEach(([param, expr]) => {
|
|
976
|
+
const cssVar = cssVars[param];
|
|
977
|
+
const targetProp = cssVar ? findVarProp(style, cssVar) : undefined;
|
|
978
|
+
if (!targetProp)
|
|
979
|
+
return;
|
|
980
|
+
const literal = expr.type === 'StringLiteral' || expr.type === 'NumericLiteral'
|
|
981
|
+
? expr.value
|
|
982
|
+
: undefined;
|
|
983
|
+
if (literal === undefined)
|
|
984
|
+
return;
|
|
985
|
+
replaceVarValue(style, cssVar, `var(${cssVar}, ${applyCssValue(literal, camelToKebabCase(targetProp))})`);
|
|
986
|
+
});
|
|
942
987
|
const vars = [];
|
|
943
988
|
runtime.forEach(({ param, source }) => {
|
|
944
989
|
const cssVar = cssVars[param];
|
|
@@ -1180,13 +1225,18 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1180
1225
|
: expr.property.value;
|
|
1181
1226
|
const source = t.isIdentifier(expr) ? varName : getSource(expr);
|
|
1182
1227
|
markStylePropApplied(varName, expr);
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1228
|
+
const owner = ownerComponentOf(expr);
|
|
1229
|
+
let possibilities = owner
|
|
1230
|
+
? scannedTables.componentPropsTable?.[`${resourcePath}-${owner}`]?.[varName]
|
|
1231
|
+
: undefined;
|
|
1232
|
+
if (!possibilities) {
|
|
1233
|
+
for (const key of Object.keys(scannedTables.componentPropsTable || {})) {
|
|
1234
|
+
if (!key.startsWith(`${resourcePath}-`))
|
|
1235
|
+
continue;
|
|
1236
|
+
if (scannedTables.componentPropsTable?.[key]?.[varName]) {
|
|
1237
|
+
possibilities = scannedTables.componentPropsTable[key][varName];
|
|
1238
|
+
break;
|
|
1239
|
+
}
|
|
1190
1240
|
}
|
|
1191
1241
|
}
|
|
1192
1242
|
if (!possibilities || possibilities.length === 0)
|
|
@@ -1569,7 +1619,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1569
1619
|
});
|
|
1570
1620
|
if (Object.keys(lookupMap).length > 0) {
|
|
1571
1621
|
const entries = Object.entries(lookupMap)
|
|
1572
|
-
.map(([k, v]) =>
|
|
1622
|
+
.map(([k, v]) => `${JSON.stringify(k)}:${JSON.stringify(v)}`)
|
|
1573
1623
|
.join(',');
|
|
1574
1624
|
classParts.push(`({${entries}}[${commonTestExpr}] || "")`);
|
|
1575
1625
|
}
|
|
@@ -2055,9 +2105,8 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
2055
2105
|
throwCompilationError(`Plumeria: css.use(${getSource(expr)}) does not support dynamic function keys.`, expr);
|
|
2056
2106
|
}
|
|
2057
2107
|
}
|
|
2058
|
-
const { classParts, isOptimizable
|
|
2059
|
-
if (isOptimizable
|
|
2060
|
-
(args.length > 0 || Object.keys(baseStyle).length > 0)) {
|
|
2108
|
+
const { classParts, isOptimizable } = buildClassParts(args);
|
|
2109
|
+
if (isOptimizable) {
|
|
2061
2110
|
const replacement = classParts.length > 0 ? classParts.join(' + " " + ') : '""';
|
|
2062
2111
|
replacements.push({
|
|
2063
2112
|
start: node.span.start - baseByteOffset,
|
|
@@ -2065,6 +2114,9 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
2065
2114
|
content: replacement,
|
|
2066
2115
|
});
|
|
2067
2116
|
}
|
|
2117
|
+
else {
|
|
2118
|
+
throwCompilationError(`Plumeria: Dynamic or unresolvable style object "${getSource(node)}" is not supported.`, node);
|
|
2119
|
+
}
|
|
2068
2120
|
},
|
|
2069
2121
|
});
|
|
2070
2122
|
for (const { name, node } of components) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/unplugin",
|
|
3
|
-
"version": "18.2.
|
|
3
|
+
"version": "18.2.26",
|
|
4
4
|
"description": "Universal Plumeria plugin for various build tools",
|
|
5
5
|
"author": "Refirst 11",
|
|
6
6
|
"license": "MIT",
|
|
@@ -89,7 +89,7 @@
|
|
|
89
89
|
"dependencies": {
|
|
90
90
|
"@rollup/pluginutils": "^5.4.0",
|
|
91
91
|
"unplugin": "^3.0.0",
|
|
92
|
-
"@plumeria/utils": "^18.2.
|
|
92
|
+
"@plumeria/utils": "^18.2.26"
|
|
93
93
|
},
|
|
94
94
|
"devDependencies": {
|
|
95
95
|
"@swc/core": "1.15.47",
|