@csszyx/compiler 0.10.4 → 0.10.5
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.cjs +89 -28
- package/dist/index.mjs +89 -28
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -638,16 +638,19 @@ function transformSourceCode(source, filename, options) {
|
|
|
638
638
|
if (!t__namespace.isIdentifier(path.node.id)) {
|
|
639
639
|
return;
|
|
640
640
|
}
|
|
641
|
-
const configArg =
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
if (!config) {
|
|
641
|
+
const configArg = resolveToConstObjectExpression(
|
|
642
|
+
init.arguments[0],
|
|
643
|
+
path.scope
|
|
644
|
+
);
|
|
645
|
+
if (!configArg) {
|
|
647
646
|
return;
|
|
648
647
|
}
|
|
649
|
-
const base =
|
|
650
|
-
const variants =
|
|
648
|
+
const base = readStaticConfigObject(configArg, "base", path.scope) ?? {};
|
|
649
|
+
const variants = readStaticConfigObject(
|
|
650
|
+
configArg,
|
|
651
|
+
"variants",
|
|
652
|
+
path.scope
|
|
653
|
+
) ?? {};
|
|
651
654
|
const classStrings = [];
|
|
652
655
|
const baseResult = transformCore.transform(base);
|
|
653
656
|
const baseCls = typeof baseResult === "string" ? baseResult : baseResult.className;
|
|
@@ -885,6 +888,35 @@ function tryHoistConditionalSpread(node, getBinding) {
|
|
|
885
888
|
}
|
|
886
889
|
return t__namespace.conditionalExpression(conditionalExpr.test, resolvedA, resolvedB);
|
|
887
890
|
}
|
|
891
|
+
function readStaticConfigObject(configExpr, key, scope) {
|
|
892
|
+
for (const prop of configExpr.properties) {
|
|
893
|
+
if (!t__namespace.isObjectProperty(prop) || prop.computed) {
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
const k = t__namespace.isIdentifier(prop.key) ? prop.key.name : t__namespace.isStringLiteral(prop.key) ? prop.key.value : null;
|
|
897
|
+
if (k !== key) {
|
|
898
|
+
continue;
|
|
899
|
+
}
|
|
900
|
+
const obj = resolveToConstObjectExpression(prop.value, scope);
|
|
901
|
+
return obj ? evaluateStaticObject(obj) : null;
|
|
902
|
+
}
|
|
903
|
+
return null;
|
|
904
|
+
}
|
|
905
|
+
function resolveToConstObjectExpression(node, scope) {
|
|
906
|
+
if (t__namespace.isObjectExpression(node)) {
|
|
907
|
+
return node;
|
|
908
|
+
}
|
|
909
|
+
if (t__namespace.isIdentifier(node)) {
|
|
910
|
+
const binding = scope.getBinding(node.name);
|
|
911
|
+
if (binding?.kind === "const" && binding.constant) {
|
|
912
|
+
const declNode = binding.path.node;
|
|
913
|
+
if (t__namespace.isVariableDeclarator(declNode) && t__namespace.isObjectExpression(declNode.init)) {
|
|
914
|
+
return declNode.init;
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
return null;
|
|
919
|
+
}
|
|
888
920
|
function evaluateStaticObject(node) {
|
|
889
921
|
const result = {};
|
|
890
922
|
for (const prop of node.properties) {
|
|
@@ -2266,6 +2298,7 @@ function transformOxc(source, filename, options) {
|
|
|
2266
2298
|
assertAstBudget(parsed.program, effectiveFilename, astBudget);
|
|
2267
2299
|
const edits = new MagicString__default(source);
|
|
2268
2300
|
const objectBindings = collectObjectBindings(parsed.program);
|
|
2301
|
+
const constObjectBindings = collectObjectBindings(parsed.program, true);
|
|
2269
2302
|
const conditionalBindings = collectConditionalBindings(parsed.program);
|
|
2270
2303
|
const reservedCSSVariableNames = options?.mangleVars ? collectStaticStyleCustomPropertyNames(parsed.program) : void 0;
|
|
2271
2304
|
const componentHoists = options?.mangleVars ? planOxcComponentVariableHoists(
|
|
@@ -2294,7 +2327,7 @@ function transformOxc(source, filename, options) {
|
|
|
2294
2327
|
collectSzvCallClasses(
|
|
2295
2328
|
node,
|
|
2296
2329
|
effectiveFilename,
|
|
2297
|
-
|
|
2330
|
+
constObjectBindings,
|
|
2298
2331
|
classes
|
|
2299
2332
|
);
|
|
2300
2333
|
return;
|
|
@@ -3224,18 +3257,9 @@ function collectSzvCallClasses(node, filename, bindings, classes) {
|
|
|
3224
3257
|
if (!configNode) {
|
|
3225
3258
|
return;
|
|
3226
3259
|
}
|
|
3227
|
-
|
|
3228
|
-
try {
|
|
3229
|
-
config = astObjectToSzObject(configNode, filename, bindings);
|
|
3230
|
-
} catch (err) {
|
|
3231
|
-
if (err instanceof OxcNotImplementedError) {
|
|
3232
|
-
return;
|
|
3233
|
-
}
|
|
3234
|
-
throw err;
|
|
3235
|
-
}
|
|
3236
|
-
const base = isSzObject(config.base) ? config.base : {};
|
|
3260
|
+
const base = readConfigSubObject(configNode, "base", filename, bindings);
|
|
3237
3261
|
addCompiledClasses(base, classes);
|
|
3238
|
-
const variants =
|
|
3262
|
+
const variants = readConfigSubObject(configNode, "variants", filename, bindings);
|
|
3239
3263
|
for (const variantValues of Object.values(variants)) {
|
|
3240
3264
|
if (!isSzObject(variantValues)) {
|
|
3241
3265
|
continue;
|
|
@@ -3248,6 +3272,30 @@ function collectSzvCallClasses(node, filename, bindings, classes) {
|
|
|
3248
3272
|
}
|
|
3249
3273
|
}
|
|
3250
3274
|
}
|
|
3275
|
+
function readConfigSubObject(configNode, key, filename, bindings) {
|
|
3276
|
+
for (const propRaw of configNode.properties) {
|
|
3277
|
+
if (propRaw.type !== "Property") {
|
|
3278
|
+
continue;
|
|
3279
|
+
}
|
|
3280
|
+
const prop = propRaw;
|
|
3281
|
+
if (prop.computed || extractKeyName(prop.key) !== key) {
|
|
3282
|
+
continue;
|
|
3283
|
+
}
|
|
3284
|
+
const valueObj = resolveObjectExpression(prop.value, bindings);
|
|
3285
|
+
if (!valueObj) {
|
|
3286
|
+
return {};
|
|
3287
|
+
}
|
|
3288
|
+
try {
|
|
3289
|
+
return astObjectToSzObject(valueObj, filename, bindings);
|
|
3290
|
+
} catch (err) {
|
|
3291
|
+
if (err instanceof OxcNotImplementedError) {
|
|
3292
|
+
return {};
|
|
3293
|
+
}
|
|
3294
|
+
throw err;
|
|
3295
|
+
}
|
|
3296
|
+
}
|
|
3297
|
+
return {};
|
|
3298
|
+
}
|
|
3251
3299
|
function addCompiledClasses(object, classes) {
|
|
3252
3300
|
const result = transformCore.transform(object);
|
|
3253
3301
|
for (const cls of result.className.split(/\s+/)) {
|
|
@@ -4090,20 +4138,33 @@ function astValueToSzValue(node, filename, bindings) {
|
|
|
4090
4138
|
`unsupported value node type ${node.type} at ${filename}:${node.start}`
|
|
4091
4139
|
);
|
|
4092
4140
|
}
|
|
4093
|
-
function collectObjectBindings(root) {
|
|
4141
|
+
function collectObjectBindings(root, constOnly = false) {
|
|
4094
4142
|
const bindings = /* @__PURE__ */ new Map();
|
|
4095
4143
|
walk(root, (node) => {
|
|
4096
|
-
if (node.type !== "
|
|
4144
|
+
if (node.type !== "VariableDeclaration") {
|
|
4097
4145
|
return;
|
|
4098
4146
|
}
|
|
4099
|
-
const
|
|
4100
|
-
|
|
4101
|
-
if (!id || id.type !== "Identifier" || !init) {
|
|
4147
|
+
const decl = node;
|
|
4148
|
+
if (constOnly && decl.kind !== "const") {
|
|
4102
4149
|
return;
|
|
4103
4150
|
}
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4151
|
+
if (!decl.declarations) {
|
|
4152
|
+
return;
|
|
4153
|
+
}
|
|
4154
|
+
for (const declaratorNode of decl.declarations) {
|
|
4155
|
+
const declarator = declaratorNode;
|
|
4156
|
+
const id = declarator.id;
|
|
4157
|
+
const init = declarator.init;
|
|
4158
|
+
if (!id || id.type !== "Identifier" || !init) {
|
|
4159
|
+
continue;
|
|
4160
|
+
}
|
|
4161
|
+
const unwrapped = unwrapExpression(init);
|
|
4162
|
+
if (unwrapped.type === "ObjectExpression") {
|
|
4163
|
+
bindings.set(
|
|
4164
|
+
String(id.name),
|
|
4165
|
+
unwrapped
|
|
4166
|
+
);
|
|
4167
|
+
}
|
|
4107
4168
|
}
|
|
4108
4169
|
});
|
|
4109
4170
|
return bindings;
|
package/dist/index.mjs
CHANGED
|
@@ -619,16 +619,19 @@ function transformSourceCode(source, filename, options) {
|
|
|
619
619
|
if (!t.isIdentifier(path.node.id)) {
|
|
620
620
|
return;
|
|
621
621
|
}
|
|
622
|
-
const configArg =
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (!config) {
|
|
622
|
+
const configArg = resolveToConstObjectExpression(
|
|
623
|
+
init.arguments[0],
|
|
624
|
+
path.scope
|
|
625
|
+
);
|
|
626
|
+
if (!configArg) {
|
|
628
627
|
return;
|
|
629
628
|
}
|
|
630
|
-
const base =
|
|
631
|
-
const variants =
|
|
629
|
+
const base = readStaticConfigObject(configArg, "base", path.scope) ?? {};
|
|
630
|
+
const variants = readStaticConfigObject(
|
|
631
|
+
configArg,
|
|
632
|
+
"variants",
|
|
633
|
+
path.scope
|
|
634
|
+
) ?? {};
|
|
632
635
|
const classStrings = [];
|
|
633
636
|
const baseResult = transform(base);
|
|
634
637
|
const baseCls = typeof baseResult === "string" ? baseResult : baseResult.className;
|
|
@@ -866,6 +869,35 @@ function tryHoistConditionalSpread(node, getBinding) {
|
|
|
866
869
|
}
|
|
867
870
|
return t.conditionalExpression(conditionalExpr.test, resolvedA, resolvedB);
|
|
868
871
|
}
|
|
872
|
+
function readStaticConfigObject(configExpr, key, scope) {
|
|
873
|
+
for (const prop of configExpr.properties) {
|
|
874
|
+
if (!t.isObjectProperty(prop) || prop.computed) {
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
const k = t.isIdentifier(prop.key) ? prop.key.name : t.isStringLiteral(prop.key) ? prop.key.value : null;
|
|
878
|
+
if (k !== key) {
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
const obj = resolveToConstObjectExpression(prop.value, scope);
|
|
882
|
+
return obj ? evaluateStaticObject(obj) : null;
|
|
883
|
+
}
|
|
884
|
+
return null;
|
|
885
|
+
}
|
|
886
|
+
function resolveToConstObjectExpression(node, scope) {
|
|
887
|
+
if (t.isObjectExpression(node)) {
|
|
888
|
+
return node;
|
|
889
|
+
}
|
|
890
|
+
if (t.isIdentifier(node)) {
|
|
891
|
+
const binding = scope.getBinding(node.name);
|
|
892
|
+
if (binding?.kind === "const" && binding.constant) {
|
|
893
|
+
const declNode = binding.path.node;
|
|
894
|
+
if (t.isVariableDeclarator(declNode) && t.isObjectExpression(declNode.init)) {
|
|
895
|
+
return declNode.init;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
return null;
|
|
900
|
+
}
|
|
869
901
|
function evaluateStaticObject(node) {
|
|
870
902
|
const result = {};
|
|
871
903
|
for (const prop of node.properties) {
|
|
@@ -2247,6 +2279,7 @@ function transformOxc(source, filename, options) {
|
|
|
2247
2279
|
assertAstBudget(parsed.program, effectiveFilename, astBudget);
|
|
2248
2280
|
const edits = new MagicString(source);
|
|
2249
2281
|
const objectBindings = collectObjectBindings(parsed.program);
|
|
2282
|
+
const constObjectBindings = collectObjectBindings(parsed.program, true);
|
|
2250
2283
|
const conditionalBindings = collectConditionalBindings(parsed.program);
|
|
2251
2284
|
const reservedCSSVariableNames = options?.mangleVars ? collectStaticStyleCustomPropertyNames(parsed.program) : void 0;
|
|
2252
2285
|
const componentHoists = options?.mangleVars ? planOxcComponentVariableHoists(
|
|
@@ -2275,7 +2308,7 @@ function transformOxc(source, filename, options) {
|
|
|
2275
2308
|
collectSzvCallClasses(
|
|
2276
2309
|
node,
|
|
2277
2310
|
effectiveFilename,
|
|
2278
|
-
|
|
2311
|
+
constObjectBindings,
|
|
2279
2312
|
classes
|
|
2280
2313
|
);
|
|
2281
2314
|
return;
|
|
@@ -3205,18 +3238,9 @@ function collectSzvCallClasses(node, filename, bindings, classes) {
|
|
|
3205
3238
|
if (!configNode) {
|
|
3206
3239
|
return;
|
|
3207
3240
|
}
|
|
3208
|
-
|
|
3209
|
-
try {
|
|
3210
|
-
config = astObjectToSzObject(configNode, filename, bindings);
|
|
3211
|
-
} catch (err) {
|
|
3212
|
-
if (err instanceof OxcNotImplementedError) {
|
|
3213
|
-
return;
|
|
3214
|
-
}
|
|
3215
|
-
throw err;
|
|
3216
|
-
}
|
|
3217
|
-
const base = isSzObject(config.base) ? config.base : {};
|
|
3241
|
+
const base = readConfigSubObject(configNode, "base", filename, bindings);
|
|
3218
3242
|
addCompiledClasses(base, classes);
|
|
3219
|
-
const variants =
|
|
3243
|
+
const variants = readConfigSubObject(configNode, "variants", filename, bindings);
|
|
3220
3244
|
for (const variantValues of Object.values(variants)) {
|
|
3221
3245
|
if (!isSzObject(variantValues)) {
|
|
3222
3246
|
continue;
|
|
@@ -3229,6 +3253,30 @@ function collectSzvCallClasses(node, filename, bindings, classes) {
|
|
|
3229
3253
|
}
|
|
3230
3254
|
}
|
|
3231
3255
|
}
|
|
3256
|
+
function readConfigSubObject(configNode, key, filename, bindings) {
|
|
3257
|
+
for (const propRaw of configNode.properties) {
|
|
3258
|
+
if (propRaw.type !== "Property") {
|
|
3259
|
+
continue;
|
|
3260
|
+
}
|
|
3261
|
+
const prop = propRaw;
|
|
3262
|
+
if (prop.computed || extractKeyName(prop.key) !== key) {
|
|
3263
|
+
continue;
|
|
3264
|
+
}
|
|
3265
|
+
const valueObj = resolveObjectExpression(prop.value, bindings);
|
|
3266
|
+
if (!valueObj) {
|
|
3267
|
+
return {};
|
|
3268
|
+
}
|
|
3269
|
+
try {
|
|
3270
|
+
return astObjectToSzObject(valueObj, filename, bindings);
|
|
3271
|
+
} catch (err) {
|
|
3272
|
+
if (err instanceof OxcNotImplementedError) {
|
|
3273
|
+
return {};
|
|
3274
|
+
}
|
|
3275
|
+
throw err;
|
|
3276
|
+
}
|
|
3277
|
+
}
|
|
3278
|
+
return {};
|
|
3279
|
+
}
|
|
3232
3280
|
function addCompiledClasses(object, classes) {
|
|
3233
3281
|
const result = transform(object);
|
|
3234
3282
|
for (const cls of result.className.split(/\s+/)) {
|
|
@@ -4071,20 +4119,33 @@ function astValueToSzValue(node, filename, bindings) {
|
|
|
4071
4119
|
`unsupported value node type ${node.type} at ${filename}:${node.start}`
|
|
4072
4120
|
);
|
|
4073
4121
|
}
|
|
4074
|
-
function collectObjectBindings(root) {
|
|
4122
|
+
function collectObjectBindings(root, constOnly = false) {
|
|
4075
4123
|
const bindings = /* @__PURE__ */ new Map();
|
|
4076
4124
|
walk(root, (node) => {
|
|
4077
|
-
if (node.type !== "
|
|
4125
|
+
if (node.type !== "VariableDeclaration") {
|
|
4078
4126
|
return;
|
|
4079
4127
|
}
|
|
4080
|
-
const
|
|
4081
|
-
|
|
4082
|
-
if (!id || id.type !== "Identifier" || !init) {
|
|
4128
|
+
const decl = node;
|
|
4129
|
+
if (constOnly && decl.kind !== "const") {
|
|
4083
4130
|
return;
|
|
4084
4131
|
}
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4132
|
+
if (!decl.declarations) {
|
|
4133
|
+
return;
|
|
4134
|
+
}
|
|
4135
|
+
for (const declaratorNode of decl.declarations) {
|
|
4136
|
+
const declarator = declaratorNode;
|
|
4137
|
+
const id = declarator.id;
|
|
4138
|
+
const init = declarator.init;
|
|
4139
|
+
if (!id || id.type !== "Identifier" || !init) {
|
|
4140
|
+
continue;
|
|
4141
|
+
}
|
|
4142
|
+
const unwrapped = unwrapExpression(init);
|
|
4143
|
+
if (unwrapped.type === "ObjectExpression") {
|
|
4144
|
+
bindings.set(
|
|
4145
|
+
String(id.name),
|
|
4146
|
+
unwrapped
|
|
4147
|
+
);
|
|
4148
|
+
}
|
|
4088
4149
|
}
|
|
4089
4150
|
});
|
|
4090
4151
|
return bindings;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@csszyx/compiler",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.5",
|
|
4
4
|
"description": "Core compiler and transformation logic for csszyx",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"csszyx",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"@babel/types": "^7.23.6",
|
|
66
66
|
"magic-string": "0.30.21",
|
|
67
67
|
"oxc-parser": "0.131.0",
|
|
68
|
-
"@csszyx/core": "0.10.
|
|
68
|
+
"@csszyx/core": "0.10.5"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@types/babel__core": "^7.20.5",
|