jsgotty 1.1.7 → 1.1.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsgotty",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Share your terminal as a web application: A JavaScript rewrite of GoTTY",
5
5
  "bin": {
6
6
  "jsgotty": "./gotty.js"
@@ -55,14 +55,21 @@ project around it.
55
55
  export const ASSETS_ROOT = "..";
56
56
  ```
57
57
 
58
- 2. **`entry.mjs`, the last import.** It names the program to start. Build
59
- also reads this line to find the entry point for `ASSETS_BUNFS=1`, so keep
60
- it as the final import in the file:
58
+ 2. **`entry.mjs`, the import of the program.** It names what to start, and
59
+ entry.mjs is the compiled entry point in both back ends:
61
60
 
62
61
  ```js
63
62
  await import("../src/index.js");
64
63
  ```
65
64
 
65
+ **Keep the `await`.** It reads like a formality — nothing here needs the
66
+ module object — but together with the `await globalThis.assetsLoaderPromise`
67
+ line above it, it is the only top-level `await` the compiled program writes
68
+ anywhere, and `--bytecode` reads exactly that to decide the program starts
69
+ asynchronously. Turn both into `.then(…)` and the build produces an
70
+ executable that exits 0 without running a line.
71
+ [Why](#the-await-in-entrymjs-is-load-bearing) explains the trap.
72
+
66
73
  3. **`package.json`.** Add the `assets` array shown above.
67
74
 
68
75
  Everything else — `compiled.js`, `assetsHelper.js`, `assetsLoader.mjs` — is
@@ -178,8 +185,35 @@ so Node 20.11+ runs the same file directly against on-disk assets while Bun
178
185
  runs it through the bootstrap. The Promise is that boundary, not a workaround
179
186
  for missing top-level await.
180
187
 
181
- `ASSETS_BUNFS=1` sidesteps the question: with no tar to load, the build skips
182
- `entry.mjs` and compiles the main program as its own entry point.
188
+ `ASSETS_BUNFS=1` sidesteps the question: there is no tar to load, so the build
189
+ writes `.bunfs-entry.mjs` next to `entry.mjs` — the same file with the loader
190
+ import removed — compiles that, and deletes it afterwards. A copy rather than a
191
+ stub, because entry.mjs may do more than import the program: a CommonJS main
192
+ that starts itself from `require.main === module` needs the exported starter
193
+ called instead, and that call has to survive. jsgotty is exactly that case; see
194
+ [below](#the-entry-import-calls-an-exported-starter).
195
+
196
+ ## The `await` in entry.mjs is load-bearing
197
+
198
+ `--bytecode` decides whether a program starts asynchronously by looking for a
199
+ top-level `await` **written in some file's own source**. The bundler moves a
200
+ module into a lazy init function whenever something dynamically imports it, and
201
+ a module moved that way takes its top-level `await` with it — what stays behind
202
+ is an `await init_x()` the bundler wrote itself, which does not count. If no
203
+ file wrote one, the executable is evaluated on the synchronous path, its
204
+ suspended startup is dropped, and it exits 0 having run nothing. No error, at
205
+ any `--minify` setting, however small the program.
206
+
207
+ `entry.mjs` is immune because it writes its own, and one is enough. Every other
208
+ `await` in the graph — all of `assetsLoader.mjs`'s, for one — sits inside a
209
+ function and cannot help. It is also why bunfs builds compile a copy of that
210
+ file rather than handing the main program to `bun build` directly: a main
211
+ program that reaches top-level await only through its imports writes none of
212
+ its own.
213
+
214
+ Upstream: `src/bundler/linker_context/postProcessJSChunk.rs` skips exactly the
215
+ modules the bundler moved, still true on oven-sh/bun `main` at 8eb5b6e2b5
216
+ (2026-08-22). Until that changes, treat entry.mjs's `await`s as API.
183
217
 
184
218
  ## Environment variables
185
219
 
@@ -326,10 +360,12 @@ const { bootstrap } = await import("../gotty.js");
326
360
  await bootstrap();
327
361
  ```
328
362
 
329
- It is still the last import in the file, which is all `ASSETS_BUNFS=1` needs to
330
- find the main program. In that mode `gotty.js` is compiled as the entry point
331
- itself, Bun rewrites the guard to `import.meta.main` — true — and it starts
332
- itself; the exported `bootstrap` then simply goes unused.
363
+ Both back ends go through it: `ASSETS_BUNFS=1` compiles a copy of `entry.mjs`,
364
+ so `gotty.js` is never the entry point and the guard stays `false` in either
365
+ mode. Handing `gotty.js` to `bun build` directly instead would compile it as
366
+ the entry, Bun would rewrite the guard to `import.meta.main` — true — and it
367
+ would start itself; that path is not taken, and `bootstrap` is what starts the
368
+ program in both builds.
333
369
 
334
370
  ### `packAssets.sh`
335
371
 
@@ -1,5 +1,5 @@
1
1
  import { basename, dirname, resolve } from "node:path";
2
- import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs";
2
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import child_process from "node:child_process";
5
5
  import { ASSETS_ROOT } from "./assetsPacker.js";
@@ -247,18 +247,29 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
247
247
  const bunfs = Boolean(process.env.ASSETS_BUNFS);
248
248
  const staging = resolve(REPO_ROOT, "build", "assets");
249
249
 
250
- // Nothing loads a tar in bunfs mode, so entry.mjs and its asset loader
251
- // are dead weight and the program becomes its own entry point. Which
252
- // program that is stays declared in one place, the last import of
253
- // entry.mjs, so adopting projects still edit only that file.
254
- const mainSpec = bunfs ? lastEntryImport(resolve(SINGLE_EXE_DIR, "entry.mjs")) : "";
255
-
256
- if (bunfs && !mainSpec) {
257
- console.error("Could not read the main program from entry.mjs's last import");
250
+ // Nothing loads a tar in bunfs mode, so the asset loader — and the
251
+ // `assets.tar` it imports — cannot be in the graph. Everything else in
252
+ // entry.mjs still has to run, so the compile entry is a generated copy
253
+ // of it with that one import removed.
254
+ //
255
+ // Handing the main program to `bun build` directly would be simpler and
256
+ // breaks two ways. Whatever entry.mjs does after the import is lost, and
257
+ // a CommonJS program relying on a `require.main` guard the bundler
258
+ // inlines to false would never start. Worse, `--bytecode` only marks the
259
+ // chunk as an async module when a top-level await appears in an
260
+ // unwrapped part's own source; a main program that reaches top-level
261
+ // await only through `__esm`-wrapped dependencies is then evaluated on
262
+ // the sync path, its suspended generator is dropped, and the binary
263
+ // exits 0 having run nothing. entry.mjs awaits in its own source, so the
264
+ // copy carries that flag the way the tar build always has.
265
+ const bunfsEntry = resolve(SINGLE_EXE_DIR, ".bunfs-entry.mjs");
266
+ const dropBunfsEntry = () => bunfs && rmSync(bunfsEntry, { force: true });
267
+
268
+ if (bunfs && !writeBunfsEntry(resolve(SINGLE_EXE_DIR, "entry.mjs"), bunfsEntry)) {
258
269
  return 1;
259
270
  }
260
271
 
261
- const compileEntry = bunfs ? resolve(SINGLE_EXE_DIR, mainSpec) : "./entry.mjs";
272
+ const compileEntry = bunfs ? bunfsEntry : "./entry.mjs";
262
273
 
263
274
  const step = (label, args, cwd) => {
264
275
  console.log("");
@@ -296,6 +307,7 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
296
307
  // and --bytecode; the module graph is the same and it is much faster.
297
308
  if (step("Scan module graph", ["build", compileEntry, "--target=bun", `--metafile=${metafile}`, `--outdir=${scanDir}`], SINGLE_EXE_DIR) !== 0) {
298
309
  console.error("Scan build failed");
310
+ dropBunfsEntry();
299
311
  return 1;
300
312
  }
301
313
 
@@ -314,6 +326,7 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
314
326
 
315
327
  if (step(`Pack ${name}`, args, dirname(packer)) !== 0) {
316
328
  console.error(`Failed to pack assets from ${packer}`);
329
+ dropBunfsEntry();
317
330
  return 1;
318
331
  }
319
332
  }
@@ -361,6 +374,8 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
361
374
  console.error("Compile failed");
362
375
  }
363
376
 
377
+ dropBunfsEntry();
378
+
364
379
  const isWindows = normalizedTarget ? normalizedTarget.toLowerCase().includes("windows") : process.platform === "win32";
365
380
  if (isWindows && !outfile.toLowerCase().endsWith(".exe")) {
366
381
  outfile += ".exe";
@@ -376,9 +391,63 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
376
391
  }
377
392
 
378
393
 
394
+ // entry.mjs, minus the import of the asset loader, written to `outFile`
395
+ // as the compile entry for bunfs builds. Returns whether it was written.
396
+ //
397
+ // A textual copy, not a generated stub: entry.mjs may do more than import
398
+ // the main program, and all of it has to survive.
399
+ export function writeBunfsEntry(entryFile, outFile) {
400
+ let src;
401
+
402
+ try {
403
+ src = readFileSync(entryFile, "utf8");
404
+ } catch (e) {
405
+ console.error(`Could not read ${entryFile}`);
406
+ console.error(e);
407
+ return false;
408
+ }
409
+
410
+ // Whole-line side-effect import of the loader, in any of the shapes a
411
+ // project might have written it. Nothing else may reference it: the
412
+ // loader is what pulls in `assets.tar`, which no bunfs build produces.
413
+ const loaderImport = /^[ \t]*import\s+(?:[^"'\n]*\bfrom\s+)?["'][^"'\n]*assetsLoader\.mjs["'][ \t]*;?[ \t]*$/gm; // "
414
+ const stripped = src.replace(loaderImport, "");
415
+
416
+ if (stripped === src) {
417
+ console.log("");
418
+ console.log(`No assetsLoader import found in ${entryFile}; compiling it as is`);
419
+ }
420
+
421
+ const header =
422
+ "// Generated by single-exe/compiled.js for ASSETS_BUNFS=1 builds.\n"
423
+ + "// A copy of entry.mjs without the asset loader; edit entry.mjs.\n";
424
+
425
+ // A hashbang is only a hashbang on the first line; anywhere else it is
426
+ // a syntax error. Keep it there and put the header underneath, unless
427
+ // the file is nothing but a hashbang and there is no underneath.
428
+ const firstBreak = stripped.indexOf("\n");
429
+ const hasHashbang = stripped.startsWith("#!");
430
+ const hashbang = hasHashbang && firstBreak !== -1 ? stripped.slice(0, firstBreak + 1) : "";
431
+
432
+ try {
433
+ writeFileSync(
434
+ outFile,
435
+ hasHashbang && !hashbang
436
+ ? stripped
437
+ : hashbang + header + stripped.slice(hashbang.length),
438
+ );
439
+ } catch (e) {
440
+ console.error(`Could not write ${outFile}`);
441
+ console.error(e);
442
+ return false;
443
+ }
444
+
445
+ return true;
446
+ }
447
+
448
+
379
449
  // The main program, as declared by the last import of entry.mjs. That
380
- // file is already the one place an adopting project edits, so bunfs
381
- // builds read it rather than adding a second declaration.
450
+ // file is already the one place an adopting project edits.
382
451
  export function lastEntryImport(entryFile) {
383
452
  let src;
384
453
 
@@ -6,6 +6,13 @@
6
6
  // 3. Starts the main program only after the assets are ready
7
7
  import "./assetsLoader.mjs";
8
8
 
9
+ // Keep these as `await`, not `.then()`. Besides the sequencing, they are
10
+ // the only top-level await this program writes anywhere, and `--bytecode`
11
+ // reads exactly that to decide the program starts asynchronously. With
12
+ // none written, the executable is evaluated synchronously, its suspended
13
+ // startup is dropped, and it exits 0 having run nothing — no error, at any
14
+ // size. Awaits the bundler generates for us do not count; see the README's
15
+ // "The await in entry.mjs is load-bearing".
9
16
  await globalThis.assetsLoaderPromise;
10
17
 
11
18
  // gotty.js starts itself from `require.main === module`, which the