trilium/scripts/electron-start.mts
Elian Doran 039b85ddd4
Some checks failed
Checks / main (push) Has been cancelled
build: adapt the native build pipeline to better-sqlite3 v13
v13 moves the addon onto N-API and ships prebuilt binaries for every
supported platform/arch inside the package itself. Three parts of the
build assumed the old prebuild-install/node-gyp layout.

Dropped `bindings` and `file-uri-to-path` from copyNodeModules: v13 no
longer depends on either, so the copy aborted the build outright with
"Unable to find any of the paths: .../node_modules/bindings".

Removed the Electron native rebuild. v13's binding.gyp resolves both of
its targets to `type: none` whenever a prebuild for the host exists, so
electron-rebuild.mts produced a stamp file and no addon on every install,
leaving BETTERSQLITE3_NATIVE_PATH pointing at a build/Release binary that
is never created -- `pnpm desktop:start` died on MODULE_NOT_FOUND. The
prebuilds are ABI-stable and load unchanged under Electron 42 (Node-API
10), so the rebuild step, its @electron/rebuild and prebuild-install
devDependencies, and the ELECTRON_NODEDIR plumbing in flake.nix all go.

Rebuilt the Docker images around the same short-circuit: `pnpm rebuild`
in the builder stages was also a no-op, so the images would have shipped
the upstream prebuild, which needs glibc >= 2.34 (x64) / >= 2.38 (arm64)
against bullseye's 2.31. The glibc images move to trixie and lose their
builder stage; the Alpine pair lose theirs too, since the linuxmusl-*
prebuilds only need libstdc++, which node:*-alpine already provides.
Alpine stays pinned to 3.23 for the musl 1.2.5 reason in #10627.

Dockerfile.legacy must keep compiling from source -- 32-bit ARM has no
prebuild at all, and linux/arm/v8 normalizes to arm64, whose prebuild
outruns that image's bullseye glibc. npm_config_force_build=1 restores
the real compile and link rules.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-25 17:09:31 +03:00

39 lines
1.6 KiB
TypeScript

import { execSync } from "child_process";
import { buildSync } from "esbuild";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { getElectronPath, getNixLdLibraryPath, isNixOS } from "./utils.mjs";
// Compile the preload script to CJS so Electron's sandboxed renderer can load it.
// Always build from apps/desktop/ regardless of CWD — window.ts resolves the
// compiled preload there for every Electron-based app (desktop, edit-docs, ...).
const DESKTOP_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "..", "apps", "desktop");
buildSync({
entryPoints: [join(DESKTOP_DIR, "src", "preload.ts")],
outfile: join(DESKTOP_DIR, "src", "preload.compiled.cjs"),
platform: "node",
format: "cjs",
bundle: true,
external: ["electron"]
});
const LD_LIBRARY_PATH = isNixOS() && getNixLdLibraryPath();
const args = process.argv.slice(2);
execSync(`${getElectronPath()} ${args.join(" ")} --no-sandbox`, {
stdio: "inherit",
env: {
...process.env,
NODE_OPTIONS: "--import tsx",
NODE_ENV: "development",
TRILIUM_ENV: "dev",
TRILIUM_RESOURCE_DIR: "../server/src",
// No BETTERSQLITE3_NATIVE_PATH: since better-sqlite3 v13 the addon is
// N-API based and ships prebuilt binaries for every supported
// platform/arch, which load unchanged under Electron. Its own resolver
// picks prebuilds/<platform>-<arch>.node, so there is no Electron-
// specific build in build/Release/ to point at any more.
LD_LIBRARY_PATH
}
});