@stacksjs/stx 0.2.189 → 0.2.190

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.
@@ -67,6 +67,32 @@ export declare function resetCrosswindCache(): void;
67
67
  * without surfacing a "not found" warning to the user.
68
68
  */
69
69
  export declare function loadCrosswindConfig(cwd: string): Promise<CrosswindConfig | null>;
70
+ /**
71
+ * Resolve the project's crosswind config the way CSS generation does.
72
+ *
73
+ * Priority: the `css` field of `stx.config.ts` (a path, or an inline object
74
+ * that may itself name a path), then auto-discovery of `crosswind.config.ts`.
75
+ *
76
+ * Extracted so the SSG's cache key can be built from the same resolution the
77
+ * stylesheet is. Two copies of this precedence would drift, and a cache key
78
+ * computed from a *different* config than the CSS is worse than no key at all —
79
+ * it would report freshness it has not checked.
80
+ */
81
+ export declare function resolveUserCrosswindConfig(resolveRoot: string): Promise<CrosswindConfig | null>;
82
+ /**
83
+ * A short, stable digest of a resolved crosswind config.
84
+ *
85
+ * Hashes the resolved OBJECT rather than a file, so it covers a config
86
+ * assembled from several files, an inline `css` block in `stx.config.ts`, and a
87
+ * preflight imported from somewhere else — all cases a path mtime would miss.
88
+ *
89
+ * Functions are hashed by source text. `JSON.stringify` drops them entirely,
90
+ * which would make an edit to a function-valued plugin or preflight invisible —
91
+ * the exact silent-staleness this digest exists to prevent. Cycles are replaced
92
+ * rather than thrown on, because throwing here degrades to 'unhashable', and a
93
+ * constant digest is a cache key that never changes.
94
+ */
95
+ export declare function fingerprintConfig(config: unknown): string;
70
96
  /**
71
97
  * Build Crosswind CSS using the build() API
72
98
  * This scans content files and generates CSS for all used classes
@@ -193,6 +193,54 @@ export async function loadCrosswindConfig(cwd) {
193
193
  return null;
194
194
  }
195
195
  }
196
+ export async function resolveUserCrosswindConfig(resolveRoot) {
197
+ let stxCssConfig = null;
198
+ try {
199
+ const { loadStxConfig } = await import("../config.js"), stxConfig = await loadStxConfig(resolveRoot);
200
+ if (stxConfig.css)
201
+ if (typeof stxConfig.css === "string") {
202
+ const configPath = path.isAbsolute(stxConfig.css) ? stxConfig.css : path.resolve(resolveRoot, stxConfig.css);
203
+ if (await Bun.file(configPath).exists()) {
204
+ const mod = await import(configPath);
205
+ stxCssConfig = mod.default || mod;
206
+ console.log(`${colors.green}[Crosswind]${colors.reset} Loaded config from stx.config.ts \u2192 ${stxConfig.css}`);
207
+ }
208
+ } else {
209
+ stxCssConfig = {
210
+ ...stxConfig.css,
211
+ content: stxConfig.css.content || [],
212
+ preflight: stxConfig.css.preflight ?? !0,
213
+ minify: stxConfig.css.minify ?? !1
214
+ };
215
+ if (stxConfig.css.config) {
216
+ const configPath = path.isAbsolute(stxConfig.css.config) ? stxConfig.css.config : path.resolve(resolveRoot, stxConfig.css.config);
217
+ if (await Bun.file(configPath).exists()) {
218
+ const mod = await import(configPath);
219
+ stxCssConfig = { ...mod.default || mod, ...stxCssConfig };
220
+ }
221
+ }
222
+ console.log(`${colors.green}[Crosswind]${colors.reset} Using inline CSS config from stx.config.ts`);
223
+ }
224
+ } catch {}
225
+ return stxCssConfig || await loadCrosswindConfig(resolveRoot);
226
+ }
227
+ export function fingerprintConfig(config) {
228
+ try {
229
+ const seen = new WeakSet;
230
+ return shortHash(JSON.stringify(config ?? {}, (_key, value) => {
231
+ if (typeof value === "function")
232
+ return `[fn]${String(value)}`;
233
+ if (typeof value === "object" && value !== null) {
234
+ if (seen.has(value))
235
+ return "[circular]";
236
+ seen.add(value);
237
+ }
238
+ return value;
239
+ }) ?? "");
240
+ } catch {
241
+ return "unhashable";
242
+ }
243
+ }
196
244
  export async function buildCrosswindCSS(cwd) {
197
245
  if (isBuilding)
198
246
  return cachedCSS;
@@ -289,40 +337,8 @@ export async function generateCrosswindCSS(htmlContent, appDir) {
289
337
  return onDisk;
290
338
  }
291
339
  if (!cachedConfig) {
292
- let stxCssConfig = null;
293
- try {
294
- const { loadStxConfig } = await import("../config.js"), stxConfig = await loadStxConfig(resolveRoot);
295
- if (stxConfig.css)
296
- if (typeof stxConfig.css === "string") {
297
- const configPath = path.isAbsolute(stxConfig.css) ? stxConfig.css : path.resolve(resolveRoot, stxConfig.css);
298
- if (await Bun.file(configPath).exists()) {
299
- const mod = await import(configPath);
300
- stxCssConfig = mod.default || mod;
301
- console.log(`${colors.green}[Crosswind]${colors.reset} Loaded config from stx.config.ts \u2192 ${stxConfig.css}`);
302
- }
303
- } else {
304
- stxCssConfig = {
305
- ...stxConfig.css,
306
- content: stxConfig.css.content || [],
307
- preflight: stxConfig.css.preflight ?? !0,
308
- minify: stxConfig.css.minify ?? !1
309
- };
310
- if (stxConfig.css.config) {
311
- const configPath = path.isAbsolute(stxConfig.css.config) ? stxConfig.css.config : path.resolve(resolveRoot, stxConfig.css.config);
312
- if (await Bun.file(configPath).exists()) {
313
- const mod = await import(configPath);
314
- stxCssConfig = { ...mod.default || mod, ...stxCssConfig };
315
- }
316
- }
317
- console.log(`${colors.green}[Crosswind]${colors.reset} Using inline CSS config from stx.config.ts`);
318
- }
319
- } catch {}
320
- cachedConfig = stxCssConfig || await loadCrosswindConfig(resolveRoot);
321
- try {
322
- configFingerprint = shortHash(JSON.stringify(cachedConfig ?? {}));
323
- } catch {
324
- configFingerprint = "unhashable";
325
- }
340
+ cachedConfig = await resolveUserCrosswindConfig(resolveRoot);
341
+ configFingerprint = fingerprintConfig(cachedConfig);
326
342
  }
327
343
  const baseConfig = hw.defaultConfig || hw.config, userConfig = cachedConfig || {}, merged = mergeCrosswindConfig(baseConfig, userConfig), { safelist, includePreflight, minify, tokenCSS } = merged, crosswindConfig = merged.config, generator = new hw.CSSGenerator(crosswindConfig);
328
344
  for (const cls of safelist)
package/dist/process.js CHANGED
@@ -341,7 +341,7 @@ export async function processDirectives(template, context, filePath, options, de
341
341
  } catch {}
342
342
  if (isTopLevel)
343
343
  try {
344
- const { getComposableScript } = await importOnce("stx/composable-loader", () => import("./composable-loader.js")), resolvedComposablesDir = options.composablesDir, composableCode = await getComposableScript(resolvedComposablesDir);
344
+ const { getComposableScript } = await importOnce("stx/composable-loader", () => import("./composable-loader.js")), resolvedComposablesDir = options.composablesDir, composableCode = await getComposableScript(resolvedComposablesDir, result);
345
345
  if (composableCode) {
346
346
  const composableTag = `<script data-stx-composables>${composableCode}</script>`, anchor = findScriptBlockByAttribute(result, "data-stx-stores") || findScriptBlockByAttribute(result, "data-stx-runtime");
347
347
  if (anchor)