@homebound/truss 2.13.0 → 2.15.0

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.
@@ -1,6 +1,7 @@
1
1
  // src/plugin/index.ts
2
- import { readFileSync as readFileSync3, existsSync } from "fs";
3
- import { resolve as resolve2, dirname, isAbsolute } from "path";
2
+ import { readFileSync as readFileSync3, writeFileSync as writeFileSync2, existsSync, readdirSync } from "fs";
3
+ import { resolve as resolve2, dirname, isAbsolute, join as join2 } from "path";
4
+ import { createHash } from "crypto";
4
5
 
5
6
  // src/plugin/emit-truss.ts
6
7
  import * as t from "@babel/types";
@@ -3012,10 +3013,16 @@ function isInsideRuntimeStyleCssObject(path) {
3012
3013
  if (!attrPath || !attrPath.isJSXAttribute()) return false;
3013
3014
  return t4.isObjectExpression(current.node.expression) && isRuntimeStyleCssAttribute2(attrPath);
3014
3015
  }
3016
+ if (current.isCallExpression() && isUseRuntimeStyleCall(current.node)) {
3017
+ return true;
3018
+ }
3015
3019
  current = current.parentPath;
3016
3020
  }
3017
3021
  return false;
3018
3022
  }
3023
+ function isUseRuntimeStyleCall(node) {
3024
+ return t4.isIdentifier(node.callee, { name: "useRuntimeStyle" });
3025
+ }
3019
3026
  function isRuntimeStyleCssAttribute2(path) {
3020
3027
  if (!t4.isJSXIdentifier(path.node.name, { name: "css" })) return false;
3021
3028
  const openingElementPath = path.parentPath;
@@ -3477,6 +3484,7 @@ function loaderForPath(filePath) {
3477
3484
  // src/plugin/index.ts
3478
3485
  var VIRTUAL_CSS_PREFIX = "\0truss-css:";
3479
3486
  var CSS_TS_QUERY = "?truss-css";
3487
+ var TRUSS_CSS_PLACEHOLDER = "__TRUSS_CSS_HASH__";
3480
3488
  var VIRTUAL_CSS_ENDPOINT = "/virtual:truss.css";
3481
3489
  var VIRTUAL_RUNTIME_ID = "virtual:truss:runtime";
3482
3490
  var RESOLVED_VIRTUAL_RUNTIME_ID = "\0" + VIRTUAL_RUNTIME_ID;
@@ -3489,6 +3497,7 @@ function trussPlugin(opts) {
3489
3497
  let isTest = false;
3490
3498
  let isBuild = false;
3491
3499
  const libraryPaths = opts.libraries ?? [];
3500
+ let emittedCssFileName = null;
3492
3501
  const cssRegistry = /* @__PURE__ */ new Map();
3493
3502
  let cssVersion = 0;
3494
3503
  let lastSentVersion = 0;
@@ -3556,8 +3565,9 @@ function trussPlugin(opts) {
3556
3565
  },
3557
3566
  transformIndexHtml(html) {
3558
3567
  if (isBuild) {
3559
- const link = `<link rel="stylesheet" href="./truss.css">`;
3560
- return html.replace("</head>", ` ${link}
3568
+ const stripped = html.replace(/\s*<link[^>]*href=["'][^"']*virtual:truss\.css["'][^>]*\/?>/, "");
3569
+ const link = `<link rel="stylesheet" href="${TRUSS_CSS_PLACEHOLDER}">`;
3570
+ return stripped.replace("</head>", ` ${link}
3561
3571
  </head>`);
3562
3572
  }
3563
3573
  const tag = `<script type="module" src="/${VIRTUAL_RUNTIME_ID}"></script>`;
@@ -3674,11 +3684,27 @@ __injectTrussCSS(${JSON.stringify(css)});
3674
3684
  if (!isBuild) return;
3675
3685
  const css = collectCss();
3676
3686
  if (!css) return;
3687
+ const hash = createHash("sha256").update(css).digest("hex").slice(0, 8);
3688
+ const fileName = `assets/truss-${hash}.css`;
3689
+ emittedCssFileName = fileName;
3677
3690
  this.emitFile({
3678
3691
  type: "asset",
3679
- fileName: "truss.css",
3692
+ fileName,
3680
3693
  source: css
3681
3694
  });
3695
+ },
3696
+ /** Patch HTML files on disk to replace the CSS placeholder with the hashed filename. */
3697
+ writeBundle(options, _bundle) {
3698
+ if (!emittedCssFileName) return;
3699
+ const outDir = options.dir || join2(projectRoot, "dist");
3700
+ for (const entry of readdirSync(outDir)) {
3701
+ if (!entry.endsWith(".html")) continue;
3702
+ const htmlPath = join2(outDir, entry);
3703
+ const html = readFileSync3(htmlPath, "utf8");
3704
+ if (html.includes(TRUSS_CSS_PLACEHOLDER)) {
3705
+ writeFileSync2(htmlPath, html.replace(TRUSS_CSS_PLACEHOLDER, `/${emittedCssFileName}`), "utf8");
3706
+ }
3707
+ }
3682
3708
  }
3683
3709
  };
3684
3710
  }