@homebound/truss 2.3.3 → 2.3.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.
@@ -565,18 +565,22 @@ function computeRulePriority(rule) {
565
565
  }
566
566
  function isVariableRule(rule) {
567
567
  if (rule.declarations) {
568
- return rule.declarations.some(function(d) {
569
- return d.cssVarName !== void 0;
570
- });
568
+ return rule.declarations.some((d) => d.cssVarName !== void 0);
571
569
  }
572
570
  return rule.cssVarName !== void 0;
573
571
  }
574
572
  function sortRulesByPriority(rules) {
575
- rules.sort(function(a, b) {
576
- const diff = computeRulePriority(a) - computeRulePriority(b);
573
+ const decorated = rules.map((rule, i) => {
574
+ return { rule, priority: computeRulePriority(rule), index: i };
575
+ });
576
+ decorated.sort((a, b) => {
577
+ const diff = a.priority - b.priority;
577
578
  if (diff !== 0) return diff;
578
- return a.className < b.className ? -1 : a.className > b.className ? 1 : 0;
579
+ return a.rule.className < b.rule.className ? -1 : a.rule.className > b.rule.className ? 1 : 0;
579
580
  });
581
+ for (let i = 0; i < decorated.length; i++) {
582
+ rules[i] = decorated[i].rule;
583
+ }
580
584
  }
581
585
 
582
586
  // src/plugin/emit-truss.ts
@@ -1869,16 +1873,6 @@ function findCssBuilderBinding(ast) {
1869
1873
  }
1870
1874
  return null;
1871
1875
  }
1872
- function hasCssMethodCall(ast, binding, method) {
1873
- let found = false;
1874
- t2.traverseFast(ast, (node) => {
1875
- if (found) return;
1876
- if (t2.isCallExpression(node) && t2.isMemberExpression(node.callee) && !node.callee.computed && t2.isIdentifier(node.callee.object, { name: binding }) && t2.isIdentifier(node.callee.property, { name: method })) {
1877
- found = true;
1878
- }
1879
- });
1880
- return found;
1881
- }
1882
1876
  function removeCssImport(ast, cssBinding) {
1883
1877
  for (let i = 0; i < ast.program.body.length; i++) {
1884
1878
  const node = ast.program.body[i];
@@ -2018,8 +2012,7 @@ function rewriteExpressionSites(options) {
2018
2012
  site.path.replaceWith(styleHash);
2019
2013
  }
2020
2014
  }
2021
- rewriteCssPropsCalls(options);
2022
- rewriteCssAttributeExpressions(options);
2015
+ rewriteCssPropsAndCssAttributes(options);
2023
2016
  }
2024
2017
  function getCssAttributePath(path) {
2025
2018
  const parentPath = path.parentPath;
@@ -2284,13 +2277,13 @@ function removeExistingAttribute(path, attrName) {
2284
2277
  }
2285
2278
  return null;
2286
2279
  }
2287
- function rewriteCssPropsCalls(options) {
2280
+ function rewriteCssPropsAndCssAttributes(options) {
2288
2281
  traverse(options.ast, {
2282
+ // -- Css.props(expr) → trussProps(expr) or mergeProps(...) --
2289
2283
  CallExpression(path) {
2290
2284
  if (!isCssPropsCall(path.node, options.cssBindingName)) return;
2291
2285
  const arg = path.node.arguments[0];
2292
2286
  if (!arg || t3.isSpreadElement(arg) || !t3.isExpression(arg) || path.node.arguments.length !== 1) return;
2293
- const line = path.node.loc?.start.line ?? null;
2294
2287
  options.needsTrussPropsHelper.current = true;
2295
2288
  const classNameExpr = extractSiblingClassName(path);
2296
2289
  if (classNameExpr) {
@@ -2301,18 +2294,15 @@ function rewriteCssPropsCalls(options) {
2301
2294
  } else {
2302
2295
  path.replaceWith(t3.callExpression(t3.identifier(options.trussPropsHelperName), [arg]));
2303
2296
  }
2304
- }
2305
- });
2306
- }
2307
- function rewriteCssAttributeExpressions(options) {
2308
- traverse(options.ast, {
2297
+ },
2298
+ // -- Remaining css={expr} JSX attributes → {...trussProps(expr)} spreads --
2299
+ // I.e. css={someVariable}, css={{ ...a, ...b }}, css={cond ? a : b}
2309
2300
  JSXAttribute(path) {
2310
2301
  if (!t3.isJSXIdentifier(path.node.name, { name: "css" })) return;
2311
2302
  const value = path.node.value;
2312
2303
  if (!t3.isJSXExpressionContainer(value)) return;
2313
2304
  if (!t3.isExpression(value.expression)) return;
2314
2305
  const expr = value.expression;
2315
- const line = path.node.loc?.start.line ?? null;
2316
2306
  const existingClassNameExpr = removeExistingAttribute(path, "className");
2317
2307
  const existingStyleExpr = removeExistingAttribute(path, "style");
2318
2308
  if (existingClassNameExpr || existingStyleExpr) {
@@ -2373,7 +2363,9 @@ function transformTruss(code, filename, mapping, options = {}) {
2373
2363
  const cssIsImported = cssImportBinding !== null;
2374
2364
  const sites = [];
2375
2365
  const errorMessages = [];
2366
+ let hasCssPropsCall = false;
2376
2367
  traverse2(ast, {
2368
+ // -- Css.*.$ chain collection --
2377
2369
  MemberExpression(path) {
2378
2370
  if (!t4.isIdentifier(path.node.property, { name: "$" })) return;
2379
2371
  if (path.node.computed) return;
@@ -2389,9 +2381,16 @@ function transformTruss(code, filename, mapping, options = {}) {
2389
2381
  for (const err of resolvedChain.errors) {
2390
2382
  errorMessages.push({ message: err, line });
2391
2383
  }
2384
+ },
2385
+ // -- Css.props() detection (so we don't bail early when there are no Css.*.$ sites) --
2386
+ CallExpression(path) {
2387
+ if (hasCssPropsCall) return;
2388
+ const callee = path.node.callee;
2389
+ if (t4.isMemberExpression(callee) && !callee.computed && t4.isIdentifier(callee.object, { name: cssBindingName }) && t4.isIdentifier(callee.property, { name: "props" })) {
2390
+ hasCssPropsCall = true;
2391
+ }
2392
2392
  }
2393
2393
  });
2394
- const hasCssPropsCall = hasCssMethodCall(ast, cssBindingName, "props");
2395
2394
  if (sites.length === 0 && !hasCssPropsCall) return null;
2396
2395
  const chains = sites.map((s) => s.resolvedChain);
2397
2396
  const { rules, needsMaybeInc } = collectAtomicRules(chains, mapping);