trilium/apps/server/scripts/build-server.sh
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

61 lines
1.5 KiB
Bash

#!/usr/bin/env bash
set -e # Fail on any command error
# Debug output
echo "Matrix Arch: $MATRIX_ARCH"
# Detect architecture from matrix input, fallback to system architecture
if [ -n "$MATRIX_ARCH" ]; then
ARCH=$MATRIX_ARCH
else
ARCH=$(uname -m)
# Convert system architecture to our naming convention
case $ARCH in
x86_64) ARCH="x64" ;;
aarch64) ARCH="arm64" ;;
esac
fi
# Debug output
echo "Selected Arch: $ARCH"
script_dir=$(realpath $(dirname $0))
# Set Node.js version and architecture-specific filename
NODE_VERSION=$(cat "../../.nvmrc")
BUILD_DIR="$script_dir/../dist"
DIST_DIR="$script_dir/../out"
NODE_FILENAME=node-v${NODE_VERSION}-linux-${ARCH}
echo "Downloading Node.js runtime $NODE_FILENAME..."
cd $BUILD_DIR
wget -qO- https://nodejs.org/dist/v${NODE_VERSION}/${NODE_FILENAME}.tar.xz | tar xfJ -
mv $NODE_FILENAME node
cd ..
rm -rf $BUILD_DIR/node/lib/node_modules/{npm,corepack} \
$BUILD_DIR/node/bin/{npm,npx,corepack} \
$BUILD_DIR/node/CHANGELOG.md \
$BUILD_DIR/node/include/node \
$BUILD_DIR/node_modules/electron* \
$BUILD_DIR/electron*.{js,map}
printf "#!/bin/sh\nexec ./node/bin/node main.mjs\n" > $BUILD_DIR/trilium.sh
chmod 755 $BUILD_DIR/trilium.sh
VERSION=`jq -r ".version" package.json`
ARCHIVE_NAME="TriliumNotes-Server-${VERSION}-linux-${ARCH}"
echo "Creating Archive $ARCHIVE_NAME..."
rm -rf $DIST_DIR
mkdir -p $DIST_DIR
cp -r "$BUILD_DIR" "$DIST_DIR/$ARCHIVE_NAME"
cd $DIST_DIR
tar cJf "$ARCHIVE_NAME.tar.xz" "$ARCHIVE_NAME"
rm -rf "$ARCHIVE_NAME"
echo "Server Build Completed!"