trilium/apps/server/e2e/sync
Elian Doran 4892428574
perf(server): bundle to ESM with code splitting
The desktop main process moved to ESM with code splitting a while ago; the
server stayed on a single 13.6 MB CJS bundle, which V8 parsed in full at every
boot. That also made the backend lazy-loading pattern a no-op here: a dynamic
import() only moved code around inside one file that was loaded anyway.

Build src/main.ts with { format: "esm" }, and rename the entry everywhere that
launches it: the two Docker entrypoint scripts (all five Dockerfiles go through
them), the trilium.sh written into the Linux tarball, start-prod-no-dir (also
the Playwright webServer), the sync e2e harness, and the Nix wrapper. The image
worker stays CJS -- the pool spawns it by its .cjs path.

Measured on a demo-sized document, old and new bundles built from the same
source and benchmarked interleaved with a pristine data dir per boot (n=6):

  loaded at startup   13.56 MB (1 file)  ->  6.64 MB (98 of 313 files)
  spawn -> first response      439 ms    ->  328 ms
  heap used after GC          56.9 MB    ->  32.4 MB

RSS improved too but was too noisy on the test machine to quote. The largest
chunks that no longer load at boot are pdfjs (1.61 MB), the Anthropic agent SDK
(1.35 MB), express-openid-connect, undici and the share theme.

flake.nix also pointed the desktop wrapper at main.cjs, left behind by the
desktop ESM migration; it would have failed to launch. Fixed alongside, with
the stale main.cjs mentions in nearby comments.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-25 11:00:55 +03:00
..
sync-instance.ts perf(server): bundle to ESM with code splitting 2026-08-25 11:00:55 +03:00
sync.spec.ts perf(server): bundle to ESM with code splitting 2026-08-25 11:00:55 +03:00