@csszyx/compiler 0.10.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const core = require('@csszyx/core');
4
- const transformCore = require('./shared/compiler.DmlsP739.cjs');
4
+ const transformCore = require('./shared/compiler.BCOOXYBh.cjs');
5
5
  const oxcParser = require('oxc-parser');
6
6
  const t = require('@babel/types');
7
7
  const node_crypto = require('node:crypto');
@@ -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 = init.arguments[0];
642
- if (!t__namespace.isObjectExpression(configArg)) {
643
- return;
644
- }
645
- const config = evaluateStaticObject(configArg);
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 = config.base ?? {};
650
- const variants = config.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
- objectBindings,
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
- let config;
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 = isSzObject(config.variants) ? config.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 !== "VariableDeclarator") {
4144
+ if (node.type !== "VariableDeclaration") {
4097
4145
  return;
4098
4146
  }
4099
- const id = node.id;
4100
- const init = node.init;
4101
- if (!id || id.type !== "Identifier" || !init) {
4147
+ const decl = node;
4148
+ if (constOnly && decl.kind !== "const") {
4102
4149
  return;
4103
4150
  }
4104
- const unwrapped = unwrapExpression(init);
4105
- if (unwrapped.type === "ObjectExpression") {
4106
- bindings.set(String(id.name), unwrapped);
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
@@ -1,6 +1,6 @@
1
1
  import { init, version, transform_sz, encode } from '@csszyx/core';
2
- import { t as transform, C as COLOR_PROPERTIES, P as PROPERTY_MAP, g as getCSSVariableName, a as PropertyCategory, K as KNOWN_VARIANTS, b as getVariantPrefix, c as getPropertyCategory, s as stripInvalidColorStrings } from './shared/compiler.BUCRKtMU.mjs';
3
- export { B as BOOLEAN_SHORTHANDS, d as PROPERTY_CATEGORY_MAP, R as REMOVED_BOOLEAN_SUGAR, S as SPECIAL_VARIANTS, e as SUGGESTION_MAP, i as isValidSzProp, n as normalizeClassName } from './shared/compiler.BUCRKtMU.mjs';
2
+ import { t as transform, C as COLOR_PROPERTIES, P as PROPERTY_MAP, g as getCSSVariableName, a as PropertyCategory, K as KNOWN_VARIANTS, b as getVariantPrefix, c as getPropertyCategory, s as stripInvalidColorStrings } from './shared/compiler.DGInsDP8.mjs';
3
+ export { B as BOOLEAN_SHORTHANDS, d as PROPERTY_CATEGORY_MAP, R as REMOVED_BOOLEAN_SUGAR, S as SPECIAL_VARIANTS, e as SUGGESTION_MAP, i as isValidSzProp, n as normalizeClassName } from './shared/compiler.DGInsDP8.mjs';
4
4
  import { parseSync } from 'oxc-parser';
5
5
  import * as t from '@babel/types';
6
6
  import { createHash } from 'node:crypto';
@@ -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 = init.arguments[0];
623
- if (!t.isObjectExpression(configArg)) {
624
- return;
625
- }
626
- const config = evaluateStaticObject(configArg);
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 = config.base ?? {};
631
- const variants = config.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
- objectBindings,
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
- let config;
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 = isSzObject(config.variants) ? config.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 !== "VariableDeclarator") {
4125
+ if (node.type !== "VariableDeclaration") {
4078
4126
  return;
4079
4127
  }
4080
- const id = node.id;
4081
- const init = node.init;
4082
- if (!id || id.type !== "Identifier" || !init) {
4128
+ const decl = node;
4129
+ if (constOnly && decl.kind !== "const") {
4083
4130
  return;
4084
4131
  }
4085
- const unwrapped = unwrapExpression(init);
4086
- if (unwrapped.type === "ObjectExpression") {
4087
- bindings.set(String(id.name), unwrapped);
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;
@@ -1041,6 +1041,41 @@ const REMOVED_BOOLEAN_SUGAR = {
1041
1041
  antialiased: { key: "fontSmoothing", value: "grayscale" },
1042
1042
  subpixelAntialiased: { key: "fontSmoothing", value: "subpixel" }
1043
1043
  };
1044
+ const ALIGNMENT_KEYS = /* @__PURE__ */ new Set([
1045
+ "justify",
1046
+ "items",
1047
+ "self",
1048
+ "alignContent",
1049
+ "placeItems",
1050
+ "placeContent",
1051
+ "justifyItems",
1052
+ "justifySelf"
1053
+ ]);
1054
+ const ALIGNMENT_CSS_VALUE_HINT = {
1055
+ "flex-start": "start",
1056
+ "flex-end": "end",
1057
+ "space-between": "between",
1058
+ "space-around": "around",
1059
+ "space-evenly": "evenly"
1060
+ };
1061
+ const warnedAlignmentValues = /* @__PURE__ */ new Set();
1062
+ function warnAlignmentValue(rawKey, value) {
1063
+ if (process.env.NODE_ENV === "production" || typeof value !== "string") {
1064
+ return;
1065
+ }
1066
+ const hint = ALIGNMENT_CSS_VALUE_HINT[value];
1067
+ if (!hint || !ALIGNMENT_KEYS.has(rawKey)) {
1068
+ return;
1069
+ }
1070
+ const sig = `${rawKey}:${value}`;
1071
+ if (warnedAlignmentValues.has(sig)) {
1072
+ return;
1073
+ }
1074
+ warnedAlignmentValues.add(sig);
1075
+ console.warn(
1076
+ `[csszyx] ${rawKey}: '${value}' is a CSS value \u2014 use the short form '${hint}' (e.g. { ${rawKey}: '${hint}' }). '${rawKey}-${value}' has no Tailwind utility and renders nothing.`
1077
+ );
1078
+ }
1044
1079
  const BOOLEAN_TO_CLASS = {
1045
1080
  backdropBlur: "backdrop-blur",
1046
1081
  backdropGrayscale: "backdrop-grayscale",
@@ -1483,6 +1518,7 @@ function transformImpl(szProp, prefix, mangleMap) {
1483
1518
  if (value === false || value === null || value === void 0) {
1484
1519
  continue;
1485
1520
  }
1521
+ warnAlignmentValue(rawKey, value);
1486
1522
  if (value === true) {
1487
1523
  const removed = REMOVED_BOOLEAN_SUGAR[rawKey];
1488
1524
  if (removed) {
@@ -1039,6 +1039,41 @@ const REMOVED_BOOLEAN_SUGAR = {
1039
1039
  antialiased: { key: "fontSmoothing", value: "grayscale" },
1040
1040
  subpixelAntialiased: { key: "fontSmoothing", value: "subpixel" }
1041
1041
  };
1042
+ const ALIGNMENT_KEYS = /* @__PURE__ */ new Set([
1043
+ "justify",
1044
+ "items",
1045
+ "self",
1046
+ "alignContent",
1047
+ "placeItems",
1048
+ "placeContent",
1049
+ "justifyItems",
1050
+ "justifySelf"
1051
+ ]);
1052
+ const ALIGNMENT_CSS_VALUE_HINT = {
1053
+ "flex-start": "start",
1054
+ "flex-end": "end",
1055
+ "space-between": "between",
1056
+ "space-around": "around",
1057
+ "space-evenly": "evenly"
1058
+ };
1059
+ const warnedAlignmentValues = /* @__PURE__ */ new Set();
1060
+ function warnAlignmentValue(rawKey, value) {
1061
+ if (process.env.NODE_ENV === "production" || typeof value !== "string") {
1062
+ return;
1063
+ }
1064
+ const hint = ALIGNMENT_CSS_VALUE_HINT[value];
1065
+ if (!hint || !ALIGNMENT_KEYS.has(rawKey)) {
1066
+ return;
1067
+ }
1068
+ const sig = `${rawKey}:${value}`;
1069
+ if (warnedAlignmentValues.has(sig)) {
1070
+ return;
1071
+ }
1072
+ warnedAlignmentValues.add(sig);
1073
+ console.warn(
1074
+ `[csszyx] ${rawKey}: '${value}' is a CSS value \u2014 use the short form '${hint}' (e.g. { ${rawKey}: '${hint}' }). '${rawKey}-${value}' has no Tailwind utility and renders nothing.`
1075
+ );
1076
+ }
1042
1077
  const BOOLEAN_TO_CLASS = {
1043
1078
  backdropBlur: "backdrop-blur",
1044
1079
  backdropGrayscale: "backdrop-grayscale",
@@ -1481,6 +1516,7 @@ function transformImpl(szProp, prefix, mangleMap) {
1481
1516
  if (value === false || value === null || value === void 0) {
1482
1517
  continue;
1483
1518
  }
1519
+ warnAlignmentValue(rawKey, value);
1484
1520
  if (value === true) {
1485
1521
  const removed = REMOVED_BOOLEAN_SUGAR[rawKey];
1486
1522
  if (removed) {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const transformCore = require('./shared/compiler.DmlsP739.cjs');
3
+ const transformCore = require('./shared/compiler.BCOOXYBh.cjs');
4
4
 
5
5
 
6
6
 
@@ -1 +1 @@
1
- export { B as BOOLEAN_SHORTHANDS, K as KNOWN_VARIANTS, M as MAX_SZ_DEPTH, P as PROPERTY_MAP, R as REMOVED_BOOLEAN_SUGAR, S as SPECIAL_VARIANTS, e as SUGGESTION_MAP, f as SzDepthError, V as VARIANT_MAP, b as getVariantPrefix, h as isForbiddenSzKey, i as isValidSzProp, j as normalizeArbitraryValue, k as normalizeArbitraryVariant, n as normalizeClassName, t as transform } from './shared/compiler.BUCRKtMU.mjs';
1
+ export { B as BOOLEAN_SHORTHANDS, K as KNOWN_VARIANTS, M as MAX_SZ_DEPTH, P as PROPERTY_MAP, R as REMOVED_BOOLEAN_SUGAR, S as SPECIAL_VARIANTS, e as SUGGESTION_MAP, f as SzDepthError, V as VARIANT_MAP, b as getVariantPrefix, h as isForbiddenSzKey, i as isValidSzProp, j as normalizeArbitraryValue, k as normalizeArbitraryVariant, n as normalizeClassName, t as transform } from './shared/compiler.DGInsDP8.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@csszyx/compiler",
3
- "version": "0.10.3",
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.3"
68
+ "@csszyx/core": "0.10.5"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@types/babel__core": "^7.20.5",