@stacksjs/stx 0.2.139 → 0.2.141

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,10 +1,11 @@
1
+ export declare function shouldLogBundlerDiagnostics(environment?: { STX_DEBUG?: string }): boolean;
1
2
  /**
2
3
  * Check if a script has user imports that need Bun.build bundling.
3
4
  *
4
5
  * Returns false for:
5
6
  * - `import type { ... }` (type-only — stripped by TS transpiler)
6
7
  * - `import { ... } from 'stx'` (auto-imported from window.stx)
7
- * - `import { ... } from '@stacksjs/browser'` (auto-imported)
8
+ * - `import '@stacksjs/browser'` (the framework-injected module bootstrap)
8
9
  * - `import { ... } from '@stores'` (handled by transformStoreImports)
9
10
  * - `import { ... } from '@composables'` (handled by transformStoreImports)
10
11
  */
@@ -2,7 +2,15 @@ import path from "node:path";
2
2
  import fs from "node:fs";
3
3
  import { getPublicEnvDefine } from "./public-env.js";
4
4
  import { stateDir } from "./state-dir.js";
5
- const BUNDLE_CACHE_VERSION = 5, BUNDLE_CACHE_METADATA_VERSION = 1, EXTERNAL_PATTERNS = [
5
+ const BUNDLE_CACHE_VERSION = 5, BUNDLE_CACHE_METADATA_VERSION = 1;
6
+ export function shouldLogBundlerDiagnostics(environment = process.env) {
7
+ return environment.STX_DEBUG === "true" || environment.STX_DEBUG === "1";
8
+ }
9
+ function logBundlerDiagnostic(...message) {
10
+ if (shouldLogBundlerDiagnostics())
11
+ console.log("[stx:bundler]", ...message);
12
+ }
13
+ const EXTERNAL_PATTERNS = [
6
14
  /^stx$/,
7
15
  /^@stacksjs\/stx$/,
8
16
  /^@stores$/,
@@ -15,7 +23,7 @@ export function hasUserImports(code) {
15
23
  while ((match = fullImportRegex.exec(code)) !== null) {
16
24
  const source = match[1];
17
25
  if (!EXTERNAL_PATTERNS.some((p) => p.test(source))) {
18
- console.log("[stx:bundler] detected user import:", source);
26
+ logBundlerDiagnostic("detected user import:", source);
19
27
  return !0;
20
28
  }
21
29
  }
@@ -24,8 +32,10 @@ export function hasUserImports(code) {
24
32
  const source = match[1];
25
33
  if (/\.css(?:\?.*)?$/.test(source))
26
34
  continue;
35
+ if (source === "@stacksjs/browser")
36
+ continue;
27
37
  if (!EXTERNAL_PATTERNS.some((p) => p.test(source))) {
28
- console.log("[stx:bundler] detected side-effect import:", source);
38
+ logBundlerDiagnostic("detected side-effect import:", source);
29
39
  return !0;
30
40
  }
31
41
  }
@@ -91,7 +101,7 @@ function createBundlePlugin(projectRoot, templateDir, tmpEntry, inputFiles) {
91
101
  ];
92
102
  for (const candidate of candidates)
93
103
  if (fs.existsSync(candidate) && fs.statSync(candidate, { throwIfNoEntry: !1 })?.isFile()) {
94
- console.log(`[stx:bundler] resolved ${args.path[0]}/ import:`, args.path, "\u2192", candidate);
104
+ logBundlerDiagnostic(`resolved ${args.path[0]}/ import:`, args.path, "\u2192", candidate);
95
105
  inputFiles.add(candidate);
96
106
  return { path: candidate };
97
107
  }
@@ -146,10 +156,10 @@ export async function bundleClientScript(code, filePath, options = {}) {
146
156
  depsValid = !1;
147
157
  }
148
158
  if (depsValid) {
149
- console.log("[stx:bundler] cache hit:", hash);
159
+ logBundlerDiagnostic("cache hit:", hash);
150
160
  return await cacheFile.text();
151
161
  }
152
- console.log("[stx:bundler] cache invalidated by dep change:", hash);
162
+ logBundlerDiagnostic("cache invalidated by dep change:", hash);
153
163
  }
154
164
  const running = inFlightBundles.get(hash);
155
165
  if (running)
@@ -188,7 +198,7 @@ async function buildBundle(code, filePath, options) {
188
198
  const exportLine = declNames.length > 0 ? `
189
199
  export { ${declNames.join(", ")} }` : "";
190
200
  await Bun.write(tmpEntry, code + exportLine);
191
- console.log("[stx:bundler] bundling:", hash, "from:", path.basename(filePath));
201
+ logBundlerDiagnostic("bundling:", hash, "from:", path.basename(filePath));
192
202
  const inputFiles = new Set;
193
203
  try {
194
204
  const result = await Bun.build({
@@ -239,7 +249,7 @@ ${bundled}
239
249
  return { ${returnedBindings} };
240
250
  })();
241
251
  ${publicAssignments}`.trim();
242
- console.log("[stx:bundler] bundled:", hash, "output:", bundled.length, "bytes");
252
+ logBundlerDiagnostic("bundled:", hash, "output:", bundled.length, "bytes");
243
253
  if (declNames.length > 0 && exposedBindings.length === 0) {
244
254
  console.warn(`[stx:bundler] dropped every binding for ${path.basename(filePath)} (${hash}); serving the unbundled source and not caching this result`);
245
255
  return code;
@@ -2,6 +2,7 @@ import path from "node:path";
2
2
  import fs from "node:fs";
3
3
  import { transformStoreImports } from "./store-imports.js";
4
4
  import { shouldTranspileTypeScript, transpileTypeScript } from "./utils.js";
5
+ import { importOnce } from "./lazy-module.js";
5
6
  const CSS_SIDE_EFFECT_IMPORT_REGEX = /^[ \t]*import\s+['"]([^'"]+?\.css)(\?[^'"]*)?['"]\s*;?\s*$/gm;
6
7
  function splitBareSpecifier(spec) {
7
8
  if (spec.startsWith(".") || spec.startsWith("/"))
@@ -813,9 +814,8 @@ export async function processClientScript(scriptContent, options = {}) {
813
814
  const vendorStyleTags = renderVendorStyleTags(cssExtraction.styles), browserCoreImports = injectBrowserCoreAutoImports(code);
814
815
  code = browserCoreImports.code;
815
816
  bundledBrowserImports = browserCoreImports.imports;
816
- const { hasUserImports, bundleClientScript } = await import("./client-script-bundler.js");
817
+ const { hasUserImports, bundleClientScript } = await importOnce("stx/client-script-bundler", () => import("./client-script-bundler.js"));
817
818
  if (hasUserImports(code)) {
818
- console.log("[stx:bundler] user imports detected, bundling...");
819
819
  code = await bundleClientScript(code, options.filePath || "", {
820
820
  projectRoot: options.projectRoot,
821
821
  minify: options.production