@homebound/truss 2.7.0 → 2.8.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.
@@ -2933,6 +2933,8 @@ var CSS_TS_QUERY = "?truss-css";
2933
2933
  var VIRTUAL_CSS_ENDPOINT = "/virtual:truss.css";
2934
2934
  var VIRTUAL_RUNTIME_ID = "virtual:truss:runtime";
2935
2935
  var RESOLVED_VIRTUAL_RUNTIME_ID = "\0" + VIRTUAL_RUNTIME_ID;
2936
+ var VIRTUAL_TEST_CSS_ID = "virtual:truss:test-css";
2937
+ var RESOLVED_VIRTUAL_TEST_CSS_ID = "\0" + VIRTUAL_TEST_CSS_ID;
2936
2938
  function trussPlugin(opts) {
2937
2939
  let mapping = null;
2938
2940
  let projectRoot;
@@ -3021,6 +3023,9 @@ function trussPlugin(opts) {
3021
3023
  if (source === VIRTUAL_RUNTIME_ID || source === "/" + VIRTUAL_RUNTIME_ID) {
3022
3024
  return RESOLVED_VIRTUAL_RUNTIME_ID;
3023
3025
  }
3026
+ if (source === VIRTUAL_TEST_CSS_ID || source === "/" + VIRTUAL_TEST_CSS_ID) {
3027
+ return RESOLVED_VIRTUAL_TEST_CSS_ID;
3028
+ }
3024
3029
  if (!source.endsWith(CSS_TS_QUERY)) return null;
3025
3030
  const absolutePath = resolveImportPath(source.slice(0, -CSS_TS_QUERY.length), importer, projectRoot);
3026
3031
  if (!existsSync(absolutePath)) return null;
@@ -3054,6 +3059,14 @@ function trussPlugin(opts) {
3054
3059
  });
3055
3060
  }
3056
3061
  })();
3062
+ `;
3063
+ }
3064
+ if (id === RESOLVED_VIRTUAL_TEST_CSS_ID) {
3065
+ const css = collectCss();
3066
+ return `
3067
+ import { __injectTrussCSS } from "@homebound/truss/runtime";
3068
+
3069
+ __injectTrussCSS(${JSON.stringify(css)});
3057
3070
  `;
3058
3071
  }
3059
3072
  if (!id.startsWith(VIRTUAL_CSS_PREFIX)) return null;
@@ -3065,26 +3078,31 @@ function trussPlugin(opts) {
3065
3078
  if (!/\.[cm]?[jt]sx?(\?|$)/.test(id)) return null;
3066
3079
  const rewrittenImports = rewriteCssTsImports(code, id);
3067
3080
  const rewrittenCode = rewrittenImports.code;
3068
- const hasCssDsl = rewrittenCode.includes("Css");
3069
- if (!hasCssDsl && !rewrittenImports.changed) return null;
3070
3081
  const fileId = stripQueryAndHash(id);
3082
+ const shouldBootstrapTestCss = isTest && libraryPaths.length > 0 && !isNodeModulesFile(fileId);
3083
+ const testCssBootstrap = injectTestCssBootstrapImport(rewrittenCode, shouldBootstrapTestCss);
3084
+ const transformedCode = testCssBootstrap.code;
3085
+ const hasCssDsl = rewrittenCode.includes("Css");
3071
3086
  if (isNodeModulesFile(fileId)) {
3072
3087
  return null;
3073
3088
  }
3089
+ if (!hasCssDsl && !rewrittenImports.changed && !testCssBootstrap.changed) return null;
3074
3090
  if (fileId.endsWith(".css.ts")) {
3075
- return rewrittenImports.changed ? { code: rewrittenCode, map: null } : null;
3091
+ return rewrittenImports.changed || testCssBootstrap.changed ? { code: transformedCode, map: null } : null;
3076
3092
  }
3077
3093
  if (!hasCssDsl) {
3078
- return { code: rewrittenCode, map: null };
3094
+ return { code: transformedCode, map: null };
3079
3095
  }
3080
- const result = transformTruss(rewrittenCode, fileId, ensureMapping(), {
3081
- debug,
3096
+ const result = transformTruss(
3097
+ transformedCode,
3098
+ fileId,
3099
+ ensureMapping(),
3082
3100
  // In test mode (jsdom), inject CSS directly so document.styleSheets has rules
3083
- injectCss: isTest
3084
- });
3101
+ { debug, injectCss: isTest }
3102
+ );
3085
3103
  if (!result) {
3086
- if (!rewrittenImports.changed) return null;
3087
- return { code: rewrittenCode, map: null };
3104
+ if (!rewrittenImports.changed && !testCssBootstrap.changed) return null;
3105
+ return { code: transformedCode, map: null };
3088
3106
  }
3089
3107
  if (result.rules) {
3090
3108
  let hasNewRules = false;
@@ -3160,6 +3178,16 @@ function stripQueryAndHash(id) {
3160
3178
  function isNodeModulesFile(filePath) {
3161
3179
  return filePath.replace(/\\/g, "/").includes("/node_modules/");
3162
3180
  }
3181
+ function injectTestCssBootstrapImport(code, shouldInject) {
3182
+ if (!shouldInject) {
3183
+ return { code, changed: false };
3184
+ }
3185
+ return {
3186
+ code: `${code}
3187
+ import "${VIRTUAL_TEST_CSS_ID}";`,
3188
+ changed: true
3189
+ };
3190
+ }
3163
3191
  function loadMapping(path) {
3164
3192
  const raw = readFileSync3(path, "utf8");
3165
3193
  return JSON.parse(raw);