@stacksjs/stx 0.2.140 → 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,3 +1,4 @@
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
  *
@@ -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
  }
@@ -27,7 +35,7 @@ export function hasUserImports(code) {
27
35
  if (source === "@stacksjs/browser")
28
36
  continue;
29
37
  if (!EXTERNAL_PATTERNS.some((p) => p.test(source))) {
30
- console.log("[stx:bundler] detected side-effect import:", source);
38
+ logBundlerDiagnostic("detected side-effect import:", source);
31
39
  return !0;
32
40
  }
33
41
  }
@@ -93,7 +101,7 @@ function createBundlePlugin(projectRoot, templateDir, tmpEntry, inputFiles) {
93
101
  ];
94
102
  for (const candidate of candidates)
95
103
  if (fs.existsSync(candidate) && fs.statSync(candidate, { throwIfNoEntry: !1 })?.isFile()) {
96
- console.log(`[stx:bundler] resolved ${args.path[0]}/ import:`, args.path, "\u2192", candidate);
104
+ logBundlerDiagnostic(`resolved ${args.path[0]}/ import:`, args.path, "\u2192", candidate);
97
105
  inputFiles.add(candidate);
98
106
  return { path: candidate };
99
107
  }
@@ -148,10 +156,10 @@ export async function bundleClientScript(code, filePath, options = {}) {
148
156
  depsValid = !1;
149
157
  }
150
158
  if (depsValid) {
151
- console.log("[stx:bundler] cache hit:", hash);
159
+ logBundlerDiagnostic("cache hit:", hash);
152
160
  return await cacheFile.text();
153
161
  }
154
- console.log("[stx:bundler] cache invalidated by dep change:", hash);
162
+ logBundlerDiagnostic("cache invalidated by dep change:", hash);
155
163
  }
156
164
  const running = inFlightBundles.get(hash);
157
165
  if (running)
@@ -190,7 +198,7 @@ async function buildBundle(code, filePath, options) {
190
198
  const exportLine = declNames.length > 0 ? `
191
199
  export { ${declNames.join(", ")} }` : "";
192
200
  await Bun.write(tmpEntry, code + exportLine);
193
- console.log("[stx:bundler] bundling:", hash, "from:", path.basename(filePath));
201
+ logBundlerDiagnostic("bundling:", hash, "from:", path.basename(filePath));
194
202
  const inputFiles = new Set;
195
203
  try {
196
204
  const result = await Bun.build({
@@ -241,7 +249,7 @@ ${bundled}
241
249
  return { ${returnedBindings} };
242
250
  })();
243
251
  ${publicAssignments}`.trim();
244
- console.log("[stx:bundler] bundled:", hash, "output:", bundled.length, "bytes");
252
+ logBundlerDiagnostic("bundled:", hash, "output:", bundled.length, "bytes");
245
253
  if (declNames.length > 0 && exposedBindings.length === 0) {
246
254
  console.warn(`[stx:bundler] dropped every binding for ${path.basename(filePath)} (${hash}); serving the unbundled source and not caching this result`);
247
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