@homebound/truss 2.3.2 → 2.3.4

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.
@@ -1869,16 +1869,6 @@ function findCssBuilderBinding(ast) {
1869
1869
  }
1870
1870
  return null;
1871
1871
  }
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
1872
  function removeCssImport(ast, cssBinding) {
1883
1873
  for (let i = 0; i < ast.program.body.length; i++) {
1884
1874
  const node = ast.program.body[i];
@@ -2018,8 +2008,7 @@ function rewriteExpressionSites(options) {
2018
2008
  site.path.replaceWith(styleHash);
2019
2009
  }
2020
2010
  }
2021
- rewriteCssPropsCalls(options);
2022
- rewriteCssAttributeExpressions(options);
2011
+ rewriteCssPropsAndCssAttributes(options);
2023
2012
  }
2024
2013
  function getCssAttributePath(path) {
2025
2014
  const parentPath = path.parentPath;
@@ -2284,13 +2273,13 @@ function removeExistingAttribute(path, attrName) {
2284
2273
  }
2285
2274
  return null;
2286
2275
  }
2287
- function rewriteCssPropsCalls(options) {
2276
+ function rewriteCssPropsAndCssAttributes(options) {
2288
2277
  traverse(options.ast, {
2278
+ // -- Css.props(expr) → trussProps(expr) or mergeProps(...) --
2289
2279
  CallExpression(path) {
2290
2280
  if (!isCssPropsCall(path.node, options.cssBindingName)) return;
2291
2281
  const arg = path.node.arguments[0];
2292
2282
  if (!arg || t3.isSpreadElement(arg) || !t3.isExpression(arg) || path.node.arguments.length !== 1) return;
2293
- const line = path.node.loc?.start.line ?? null;
2294
2283
  options.needsTrussPropsHelper.current = true;
2295
2284
  const classNameExpr = extractSiblingClassName(path);
2296
2285
  if (classNameExpr) {
@@ -2301,18 +2290,15 @@ function rewriteCssPropsCalls(options) {
2301
2290
  } else {
2302
2291
  path.replaceWith(t3.callExpression(t3.identifier(options.trussPropsHelperName), [arg]));
2303
2292
  }
2304
- }
2305
- });
2306
- }
2307
- function rewriteCssAttributeExpressions(options) {
2308
- traverse(options.ast, {
2293
+ },
2294
+ // -- Remaining css={expr} JSX attributes → {...trussProps(expr)} spreads --
2295
+ // I.e. css={someVariable}, css={{ ...a, ...b }}, css={cond ? a : b}
2309
2296
  JSXAttribute(path) {
2310
2297
  if (!t3.isJSXIdentifier(path.node.name, { name: "css" })) return;
2311
2298
  const value = path.node.value;
2312
2299
  if (!t3.isJSXExpressionContainer(value)) return;
2313
2300
  if (!t3.isExpression(value.expression)) return;
2314
2301
  const expr = value.expression;
2315
- const line = path.node.loc?.start.line ?? null;
2316
2302
  const existingClassNameExpr = removeExistingAttribute(path, "className");
2317
2303
  const existingStyleExpr = removeExistingAttribute(path, "style");
2318
2304
  if (existingClassNameExpr || existingStyleExpr) {
@@ -2373,7 +2359,9 @@ function transformTruss(code, filename, mapping, options = {}) {
2373
2359
  const cssIsImported = cssImportBinding !== null;
2374
2360
  const sites = [];
2375
2361
  const errorMessages = [];
2362
+ let hasCssPropsCall = false;
2376
2363
  traverse2(ast, {
2364
+ // -- Css.*.$ chain collection --
2377
2365
  MemberExpression(path) {
2378
2366
  if (!t4.isIdentifier(path.node.property, { name: "$" })) return;
2379
2367
  if (path.node.computed) return;
@@ -2389,9 +2377,16 @@ function transformTruss(code, filename, mapping, options = {}) {
2389
2377
  for (const err of resolvedChain.errors) {
2390
2378
  errorMessages.push({ message: err, line });
2391
2379
  }
2380
+ },
2381
+ // -- Css.props() detection (so we don't bail early when there are no Css.*.$ sites) --
2382
+ CallExpression(path) {
2383
+ if (hasCssPropsCall) return;
2384
+ const callee = path.node.callee;
2385
+ if (t4.isMemberExpression(callee) && !callee.computed && t4.isIdentifier(callee.object, { name: cssBindingName }) && t4.isIdentifier(callee.property, { name: "props" })) {
2386
+ hasCssPropsCall = true;
2387
+ }
2392
2388
  }
2393
2389
  });
2394
- const hasCssPropsCall = hasCssMethodCall(ast, cssBindingName, "props");
2395
2390
  if (sites.length === 0 && !hasCssPropsCall) return null;
2396
2391
  const chains = sites.map((s) => s.resolvedChain);
2397
2392
  const { rules, needsMaybeInc } = collectAtomicRules(chains, mapping);
@@ -2429,13 +2424,13 @@ function transformTruss(code, filename, mapping, options = {}) {
2429
2424
  runtimeLookupNames
2430
2425
  });
2431
2426
  const runtimeImports = [];
2432
- if (needsTrussPropsHelper.current) {
2427
+ if (needsTrussPropsHelper.current && !existingTrussPropsHelperName) {
2433
2428
  runtimeImports.push({ importedName: "trussProps", localName: trussPropsHelperName });
2434
2429
  }
2435
- if (needsMergePropsHelper.current) {
2430
+ if (needsMergePropsHelper.current && !existingMergePropsHelperName) {
2436
2431
  runtimeImports.push({ importedName: "mergeProps", localName: mergePropsHelperName });
2437
2432
  }
2438
- if (needsTrussDebugInfo.current) {
2433
+ if (needsTrussDebugInfo.current && !existingTrussDebugInfoName) {
2439
2434
  runtimeImports.push({ importedName: "TrussDebugInfo", localName: trussDebugInfoName });
2440
2435
  }
2441
2436
  if (options.injectCss) {