@homebound/truss 2.4.0 → 2.5.1

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,6 @@
1
1
  // src/plugin/index.ts
2
- import { readFileSync as readFileSync2, writeFileSync, existsSync } from "fs";
3
- import { resolve, dirname, isAbsolute, join } from "path";
2
+ import { readFileSync as readFileSync3, writeFileSync as writeFileSync2, existsSync } from "fs";
3
+ import { resolve as resolve2, dirname, isAbsolute, join as join2 } from "path";
4
4
 
5
5
  // src/plugin/emit-truss.ts
6
6
  import * as t from "@babel/types";
@@ -521,7 +521,7 @@ function getPropertyPriority(property) {
521
521
  }
522
522
  function getPseudoClassPriority(pseudo) {
523
523
  const leadingPseudo = pseudo.trim().match(/^::?[a-zA-Z-]+/)?.[0] ?? pseudo.split("(")[0];
524
- const base = leadingPseudo.replace(/[A-Z]/g, function(match) {
524
+ const base = leadingPseudo.replace(/[A-Z]/g, (match) => {
525
525
  return `-${match.toLowerCase()}`;
526
526
  });
527
527
  return PSEUDO_CLASS_PRIORITIES[base] ?? 40;
@@ -621,7 +621,7 @@ function whenPrefix(whenPseudo) {
621
621
  return `wh_${rel}_${pseudoTag}_${markerPart}`;
622
622
  }
623
623
  function pseudoSelectorTag(pseudo) {
624
- const replaced = pseudo.trim().replace(/::?[a-zA-Z-]+/g, function(match) {
624
+ const replaced = pseudo.trim().replace(/::?[a-zA-Z-]+/g, (match) => {
625
625
  return `_${pseudoIdentifierTag(match)}_`;
626
626
  });
627
627
  const cleaned = replaced.replace(/[^a-zA-Z0-9]/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "");
@@ -638,7 +638,7 @@ function pseudoIdentifierTag(pseudo) {
638
638
  function normalizePseudoIdentifier(pseudo) {
639
639
  const prefixMatch = pseudo.match(/^::?/);
640
640
  const prefix = prefixMatch?.[0] ?? "";
641
- const name = pseudo.slice(prefix.length).replace(/[A-Z]/g, function(match) {
641
+ const name = pseudo.slice(prefix.length).replace(/[A-Z]/g, (match) => {
642
642
  return `-${match.toLowerCase()}`;
643
643
  });
644
644
  return `${prefix}${name}`;
@@ -813,7 +813,7 @@ function collectVariableRules(rules, seg, mapping) {
813
813
  cssVarName: existingRule.cssVarName
814
814
  }
815
815
  ];
816
- if (!existingRule.declarations.some(function(entry) {
816
+ if (!existingRule.declarations.some((entry) => {
817
817
  return entry.cssProperty === declaration.cssProperty;
818
818
  })) {
819
819
  existingRule.declarations.push(declaration);
@@ -914,13 +914,13 @@ function getRuleDeclarations(rule) {
914
914
  return rule.declarations ?? [{ cssProperty: rule.cssProperty, cssValue: rule.cssValue, cssVarName: rule.cssVarName }];
915
915
  }
916
916
  function formatRuleBlock(selector, rule) {
917
- const body = getRuleDeclarations(rule).map(function(declaration) {
917
+ const body = getRuleDeclarations(rule).map((declaration) => {
918
918
  return `${declaration.cssProperty}: ${declaration.cssValue};`;
919
919
  }).join(" ");
920
920
  return `${selector} { ${body} }`;
921
921
  }
922
922
  function formatNestedRuleBlock(wrapper, selector, rule) {
923
- const body = getRuleDeclarations(rule).map(function(declaration) {
923
+ const body = getRuleDeclarations(rule).map((declaration) => {
924
924
  return `${declaration.cssProperty}: ${declaration.cssValue};`;
925
925
  }).join(" ");
926
926
  return `${wrapper} { ${selector} { ${body} } }`;
@@ -1923,12 +1923,12 @@ function findImportDeclaration(ast, source) {
1923
1923
  function replaceCssImportWithNamedImports(ast, cssBinding, source, imports) {
1924
1924
  for (const node of ast.program.body) {
1925
1925
  if (!t2.isImportDeclaration(node)) continue;
1926
- const cssSpecIndex = node.specifiers.findIndex(function(spec) {
1926
+ const cssSpecIndex = node.specifiers.findIndex((spec) => {
1927
1927
  return t2.isImportSpecifier(spec) && spec.local.name === cssBinding;
1928
1928
  });
1929
1929
  if (cssSpecIndex === -1 || node.specifiers.length !== 1) continue;
1930
1930
  node.source = t2.stringLiteral(source);
1931
- node.specifiers = imports.map(function(entry) {
1931
+ node.specifiers = imports.map((entry) => {
1932
1932
  return t2.importSpecifier(t2.identifier(entry.localName), t2.identifier(entry.importedName));
1933
1933
  });
1934
1934
  return true;
@@ -1940,7 +1940,7 @@ function upsertNamedImports(ast, source, imports) {
1940
1940
  for (const node of ast.program.body) {
1941
1941
  if (!t2.isImportDeclaration(node) || node.source.value !== source) continue;
1942
1942
  for (const entry of imports) {
1943
- const exists = node.specifiers.some(function(spec) {
1943
+ const exists = node.specifiers.some((spec) => {
1944
1944
  return t2.isImportSpecifier(spec) && t2.isIdentifier(spec.imported, { name: entry.importedName });
1945
1945
  });
1946
1946
  if (exists) continue;
@@ -1949,7 +1949,7 @@ function upsertNamedImports(ast, source, imports) {
1949
1949
  return;
1950
1950
  }
1951
1951
  const importDecl = t2.importDeclaration(
1952
- imports.map(function(entry) {
1952
+ imports.map((entry) => {
1953
1953
  return t2.importSpecifier(t2.identifier(entry.localName), t2.identifier(entry.importedName));
1954
1954
  }),
1955
1955
  t2.stringLiteral(source)
@@ -2031,7 +2031,7 @@ function buildStyleHashFromChain(chain, options) {
2031
2031
  const members = [];
2032
2032
  const previousProperties = /* @__PURE__ */ new Map();
2033
2033
  if (chain.markers.length > 0) {
2034
- const markerClasses = chain.markers.map(function(marker) {
2034
+ const markerClasses = chain.markers.map((marker) => {
2035
2035
  return markerClassName(marker.markerNode);
2036
2036
  });
2037
2037
  members.push(t3.objectProperty(t3.identifier("__marker"), t3.stringLiteral(markerClasses.join(" "))));
@@ -2149,7 +2149,7 @@ function collectConditionalOnlyProps(segments) {
2149
2149
  return result;
2150
2150
  }
2151
2151
  function mergeConditionalBranchMembers(members, previousProperties, conditionalOnlyProps) {
2152
- return members.map(function(member) {
2152
+ return members.map((member) => {
2153
2153
  if (!t3.isObjectProperty(member)) {
2154
2154
  return member;
2155
2155
  }
@@ -2200,10 +2200,10 @@ function arrayElementExpression(element) {
2200
2200
  function mergeVarsObject(previousVars, currentVars) {
2201
2201
  if (t3.isObjectExpression(previousVars) && t3.isObjectExpression(currentVars)) {
2202
2202
  return t3.objectExpression([
2203
- ...previousVars.properties.map(function(property) {
2203
+ ...previousVars.properties.map((property) => {
2204
2204
  return t3.cloneNode(property, true);
2205
2205
  }),
2206
- ...currentVars.properties.map(function(property) {
2206
+ ...currentVars.properties.map((property) => {
2207
2207
  return t3.cloneNode(property, true);
2208
2208
  })
2209
2209
  ]);
@@ -2227,7 +2227,7 @@ function clonePropertyKey(key) {
2227
2227
  }
2228
2228
  function injectDebugInfo(expr, line, options) {
2229
2229
  if (!options.debug) return;
2230
- const firstProp = expr.properties.find(function(p) {
2230
+ const firstProp = expr.properties.find((p) => {
2231
2231
  return t3.isObjectProperty(p) && !(t3.isIdentifier(p.key) && p.key.name === "__marker" || t3.isStringLiteral(p.key) && p.key.value === "__marker");
2232
2232
  });
2233
2233
  if (!firstProp) return;
@@ -2481,7 +2481,7 @@ function transformTruss(code, filename, mapping, options = {}) {
2481
2481
  );
2482
2482
  }
2483
2483
  if (declarationsToInsert.length > 0) {
2484
- const insertIndex = ast.program.body.findIndex(function(node) {
2484
+ const insertIndex = ast.program.body.findIndex((node) => {
2485
2485
  return !t4.isImportDeclaration(node);
2486
2486
  });
2487
2487
  ast.program.body.splice(insertIndex === -1 ? ast.program.body.length : insertIndex, 0, ...declarationsToInsert);
@@ -2860,7 +2860,7 @@ function mergeTrussCss(sources) {
2860
2860
  }
2861
2861
  }
2862
2862
  }
2863
- allRules.sort(function(a, b) {
2863
+ allRules.sort((a, b) => {
2864
2864
  const diff = a.priority - b.priority;
2865
2865
  if (diff !== 0) return diff;
2866
2866
  return a.className < b.className ? -1 : a.className > b.className ? 1 : 0;
@@ -2877,6 +2877,52 @@ function mergeTrussCss(sources) {
2877
2877
  return lines.join("\n");
2878
2878
  }
2879
2879
 
2880
+ // src/plugin/esbuild-plugin.ts
2881
+ import { readFileSync as readFileSync2, writeFileSync, mkdirSync } from "fs";
2882
+ import { resolve, join } from "path";
2883
+ function trussEsbuildPlugin(opts) {
2884
+ const cssRegistry = /* @__PURE__ */ new Map();
2885
+ let mapping = null;
2886
+ let outDir;
2887
+ return {
2888
+ name: "truss",
2889
+ setup(build) {
2890
+ outDir = build.initialOptions.outdir ?? build.initialOptions.outdir;
2891
+ build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, (args) => {
2892
+ const code = readFileSync2(args.path, "utf8");
2893
+ if (!code.includes("Css")) return void 0;
2894
+ if (!mapping) {
2895
+ mapping = loadMapping(resolve(process.cwd(), opts.mapping));
2896
+ }
2897
+ const result = transformTruss(code, args.path, mapping);
2898
+ if (!result) return void 0;
2899
+ if (result.rules) {
2900
+ for (const [className, rule] of result.rules) {
2901
+ if (!cssRegistry.has(className)) {
2902
+ cssRegistry.set(className, rule);
2903
+ }
2904
+ }
2905
+ }
2906
+ return { contents: result.code, loader: loaderForPath(args.path) };
2907
+ });
2908
+ build.onEnd(() => {
2909
+ if (cssRegistry.size === 0) return;
2910
+ const css = generateCssText(cssRegistry);
2911
+ const cssFileName = opts.outputCss ?? "truss.css";
2912
+ const cssPath = resolve(outDir ?? join(process.cwd(), "dist"), cssFileName);
2913
+ mkdirSync(resolve(cssPath, ".."), { recursive: true });
2914
+ writeFileSync(cssPath, css, "utf8");
2915
+ });
2916
+ }
2917
+ };
2918
+ }
2919
+ function loaderForPath(filePath) {
2920
+ if (filePath.endsWith(".tsx")) return "tsx";
2921
+ if (filePath.endsWith(".ts")) return "ts";
2922
+ if (filePath.endsWith(".jsx")) return "jsx";
2923
+ return "js";
2924
+ }
2925
+
2880
2926
  // src/plugin/index.ts
2881
2927
  var VIRTUAL_CSS_PREFIX = "\0truss-css:";
2882
2928
  var CSS_TS_QUERY = "?truss-css";
@@ -2894,7 +2940,7 @@ function trussPlugin(opts) {
2894
2940
  let cssVersion = 0;
2895
2941
  let lastSentVersion = 0;
2896
2942
  function mappingPath() {
2897
- return resolve(projectRoot || process.cwd(), opts.mapping);
2943
+ return resolve2(projectRoot || process.cwd(), opts.mapping);
2898
2944
  }
2899
2945
  function ensureMapping() {
2900
2946
  if (!mapping) {
@@ -2905,8 +2951,8 @@ function trussPlugin(opts) {
2905
2951
  let libraryCache = null;
2906
2952
  function loadLibraries() {
2907
2953
  if (!libraryCache) {
2908
- libraryCache = libraryPaths.map(function(libPath) {
2909
- const resolved = resolve(projectRoot || process.cwd(), libPath);
2954
+ libraryCache = libraryPaths.map((libPath) => {
2955
+ const resolved = resolve2(projectRoot || process.cwd(), libPath);
2910
2956
  return readTrussCss(resolved);
2911
2957
  });
2912
2958
  }
@@ -2938,20 +2984,20 @@ function trussPlugin(opts) {
2938
2984
  // -- Dev mode HMR --
2939
2985
  configureServer(server) {
2940
2986
  if (isTest) return;
2941
- server.middlewares.use(function(req, res, next) {
2987
+ server.middlewares.use((req, res, next) => {
2942
2988
  if (req.url !== VIRTUAL_CSS_ENDPOINT) return next();
2943
2989
  const css = collectCss();
2944
2990
  res.setHeader("Content-Type", "text/css");
2945
2991
  res.setHeader("Cache-Control", "no-store");
2946
2992
  res.end(css);
2947
2993
  });
2948
- const interval = setInterval(function() {
2994
+ const interval = setInterval(() => {
2949
2995
  if (cssVersion !== lastSentVersion && server.ws) {
2950
2996
  lastSentVersion = cssVersion;
2951
2997
  server.ws.send({ type: "custom", event: "truss:css-update" });
2952
2998
  }
2953
2999
  }, 150);
2954
- server.httpServer?.on("close", function() {
3000
+ server.httpServer?.on("close", () => {
2955
3001
  clearInterval(interval);
2956
3002
  });
2957
3003
  },
@@ -2980,7 +3026,7 @@ function trussPlugin(opts) {
2980
3026
  if (id === RESOLVED_VIRTUAL_RUNTIME_ID) {
2981
3027
  return `
2982
3028
  // Truss dev HMR runtime \u2014 keeps styles up to date without page reload
2983
- (function() {
3029
+ (() => {
2984
3030
  let style = document.getElementById("__truss_virtual__");
2985
3031
  if (!style) {
2986
3032
  style = document.createElement("style");
@@ -2990,16 +3036,16 @@ function trussPlugin(opts) {
2990
3036
 
2991
3037
  function fetchCss() {
2992
3038
  fetch("${VIRTUAL_CSS_ENDPOINT}")
2993
- .then(function(r) { return r.text(); })
2994
- .then(function(css) { style.textContent = css; })
2995
- .catch(function() {});
3039
+ .then((r) => r.text())
3040
+ .then((css) => { style.textContent = css; })
3041
+ .catch(() => {});
2996
3042
  }
2997
3043
 
2998
3044
  fetchCss();
2999
3045
 
3000
3046
  if (import.meta.hot) {
3001
3047
  import.meta.hot.on("truss:css-update", fetchCss);
3002
- import.meta.hot.on("vite:afterUpdate", function() {
3048
+ import.meta.hot.on("vite:afterUpdate", () => {
3003
3049
  setTimeout(fetchCss, 50);
3004
3050
  });
3005
3051
  }
@@ -3008,7 +3054,7 @@ function trussPlugin(opts) {
3008
3054
  }
3009
3055
  if (!id.startsWith(VIRTUAL_CSS_PREFIX)) return null;
3010
3056
  const sourcePath = id.slice(VIRTUAL_CSS_PREFIX.length) + ".ts";
3011
- const sourceCode = readFileSync2(sourcePath, "utf8");
3057
+ const sourceCode = readFileSync3(sourcePath, "utf8");
3012
3058
  return transformCssTs(sourceCode, sourcePath, ensureMapping());
3013
3059
  },
3014
3060
  transform(code, id) {
@@ -3072,15 +3118,15 @@ function trussPlugin(opts) {
3072
3118
  if (!isBuild) return;
3073
3119
  const css = collectCss();
3074
3120
  if (!css) return;
3075
- const outDir = options.dir || join(projectRoot, "dist");
3076
- const trussPath = join(outDir, "truss.css");
3121
+ const outDir = options.dir || join2(projectRoot, "dist");
3122
+ const trussPath = join2(outDir, "truss.css");
3077
3123
  if (!existsSync(trussPath)) {
3078
- const alreadyEmitted = Object.keys(bundle).some(function(key) {
3124
+ const alreadyEmitted = Object.keys(bundle).some((key) => {
3079
3125
  const asset = bundle[key];
3080
3126
  return asset.type === "asset" && key.endsWith(".css") && typeof asset.source === "string" && asset.source.includes(css);
3081
3127
  });
3082
3128
  if (!alreadyEmitted) {
3083
- writeFileSync(trussPath, css, "utf8");
3129
+ writeFileSync2(trussPath, css, "utf8");
3084
3130
  }
3085
3131
  }
3086
3132
  }
@@ -3091,9 +3137,9 @@ function resolveImportPath(source, importer, projectRoot) {
3091
3137
  return source;
3092
3138
  }
3093
3139
  if (importer) {
3094
- return resolve(dirname(importer), source);
3140
+ return resolve2(dirname(importer), source);
3095
3141
  }
3096
- return resolve(projectRoot || process.cwd(), source);
3142
+ return resolve2(projectRoot || process.cwd(), source);
3097
3143
  }
3098
3144
  function stripQueryAndHash(id) {
3099
3145
  const queryIndex = id.indexOf("?");
@@ -3111,11 +3157,12 @@ function isNodeModulesFile(filePath) {
3111
3157
  return filePath.replace(/\\/g, "/").includes("/node_modules/");
3112
3158
  }
3113
3159
  function loadMapping(path) {
3114
- const raw = readFileSync2(path, "utf8");
3160
+ const raw = readFileSync3(path, "utf8");
3115
3161
  return JSON.parse(raw);
3116
3162
  }
3117
3163
  export {
3118
3164
  loadMapping,
3165
+ trussEsbuildPlugin,
3119
3166
  trussPlugin
3120
3167
  };
3121
3168
  //# sourceMappingURL=index.js.map