Skip to content

Build your personal knowledge base with Trilium Notes

build: ship only the better-sqlite3 files an artifact actually uses

Elian Doranfd7ee399630f
diff --git a/apps/build-docs/scripts/build.ts b/apps/build-docs/scripts/build.ts
index 7a50373142..7e0c93712f 100644
--- a/apps/build-docs/scripts/build.ts
+++ b/apps/build-docs/scripts/build.ts
@@ -18,6 +18,7 @@ async function main() {
// Copy node modules dependencies if needed
build.copyNodeModules([ "better-sqlite3" ]);
+ build.trimBetterSqlite3();
}
main();
diff --git a/apps/desktop/scripts/build.ts b/apps/desktop/scripts/build.ts
index 46de8ba4bf..e760d5a5fe 100644
--- a/apps/desktop/scripts/build.ts
+++ b/apps/desktop/scripts/build.ts
@@ -24,6 +24,8 @@ async function main() {
// Copy node modules dependencies
build.copyNodeModules([ "better-sqlite3" ]);
+ // No musl build: Electron itself ships glibc-only Linux binaries.
+ build.trimBetterSqlite3({ includeMusl: false });
build.copy("/node_modules/ckeditor5/dist/ckeditor5-content.css", "ckeditor5-content.css");
diff --git a/apps/edit-docs/scripts/build.ts b/apps/edit-docs/scripts/build.ts
index 28a6a7998b..b7b329fd44 100644
--- a/apps/edit-docs/scripts/build.ts
+++ b/apps/edit-docs/scripts/build.ts
@@ -18,6 +18,8 @@ async function main() {
// Copy node modules dependencies
build.copyNodeModules(["better-sqlite3"]);
+ // No musl build: Electron itself ships glibc-only Linux binaries.
+ build.trimBetterSqlite3({ includeMusl: false });
generatePackageJson();
}
diff --git a/apps/server/scripts/build.ts b/apps/server/scripts/build.ts
index 1b893823a0..52d0219fd9 100644
--- a/apps/server/scripts/build.ts
+++ b/apps/server/scripts/build.ts
@@ -18,6 +18,7 @@ async function main() {
// Copy node modules dependencies
build.copyNodeModules([ "better-sqlite3" ]);
+ build.trimBetterSqlite3();
build.copy("/node_modules/ckeditor5/dist/ckeditor5-content.css", "ckeditor5-content.css");
build.buildFrontend();
diff --git a/flake.nix b/flake.nix
index 822875cef6..78e12c3eee 100644
--- a/flake.nix
+++ b/flake.nix
@@ -316,17 +316,9 @@ nodejs.python
buildTask = "server:build";
mainProgram = "trilium-server";
installCommands = ''
- #remove-references-to -t ${nodejs.python} apps/server/dist/node_modules/better-sqlite3/build/config.gypi
- #remove-references-to -t ${pnpm} apps/server/dist/node_modules/better-sqlite3/build/config.gypi
-
- pushd apps/server/dist
- rm -rf node_modules/better-sqlite3/build/Release/obj \
- node_modules/better-sqlite3/build/Release/obj.target \
- node_modules/better-sqlite3/build/Release/sqlite3.a \
- node_modules/better-sqlite3/build/{Makefile,better_sqlite3.target.mk,test_extension.target.mk,binding.Makefile} \
- node_modules/better-sqlite3/deps/sqlite3
- popd
-
+ # No better-sqlite3 cleanup needed here any more: the server build
+ # trims deps/, src/ and build/ (which held the store paths that used
+ # to need remove-references-to) out of dist itself.
mkdir -p $out/{bin,opt/trilium-server}
cp --archive apps/server/dist/* $out/opt/trilium-server
makeWrapper ${lib.getExe nodejs} $out/bin/trilium-server \
diff --git a/scripts/build-utils.ts b/scripts/build-utils.ts
index b266f62605..3435508e44 100644
--- a/scripts/build-utils.ts
+++ b/scripts/build-utils.ts
@@ -1,6 +1,6 @@
import { execSync } from "child_process";
import { build as esbuild } from "esbuild";
-import { cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from "fs";
+import { cpSync, existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "fs";
import { delimiter, join } from "path";
export default class BuildHelper {
@@ -143,6 +143,60 @@ export default class BuildHelper {
}
}
+ /**
+ * Strips everything from the copied better-sqlite3 that a packaged artifact
+ * never uses. Since v13 the package bundles a prebuilt binary for all eight
+ * platforms it supports (~17 MB) plus the SQLite sources needed to compile
+ * from scratch (~10 MB), but an artifact only ever loads one binary and never
+ * compiles: 27 MB -> ~2.2 MB.
+ *
+ * @param opts.platform the platform being built *for*. Defaults to the host's,
+ * but pass it explicitly for cross-packaging.
+ * @param opts.arch the architecture being built *for* -- not always the host's,
+ * as the macOS runners are arm64 and also package darwin-x64.
+ * @param opts.includeMusl whether to keep the musl build alongside the glibc one
+ * on Linux (defaults to `true`). Only the server needs it; see below.
+ */
+ trimBetterSqlite3(opts: { platform?: string; arch?: string; includeMusl?: boolean } = {}) {
+ const { platform = process.platform, arch = targetArch(), includeMusl = true } = opts;
+ const moduleDir = join(this.outDir, "node_modules", "better-sqlite3");
+ const prebuildDir = join(moduleDir, "prebuilds");
+ if (!existsSync(prebuildDir)) {
+ return;
+ }
+
+ // Keep both libc variants on Linux by default. The server's dist is built
+ // once on a glibc runner and then consumed by both the Debian and the Alpine
+ // images, and better-sqlite3 resolves linuxmusl-* at runtime on the latter --
+ // dropping it there would break the amd64 image at startup. Electron-based
+ // artifacts have no such consumer (Electron ships no musl builds), so they
+ // opt out and save the extra ~2.3 MB.
+ const keep = new Set([ `${platform}-${arch}.node` ]);
+ if (platform === "linux" && includeMusl) {
+ keep.add(`linuxmusl-${arch}.node`);
+ }
+
+ const available = readdirSync(prebuildDir);
+ if (!available.includes(`${platform}-${arch}.node`)) {
+ throw new Error(
+ `better-sqlite3 ships no prebuild for ${platform}-${arch} (found: ${available.join(", ")}). `
+ + `Refusing to trim, since that would leave the artifact with no native addon.`
+ );
+ }
+
+ for (const file of available) {
+ if (!keep.has(file)) {
+ rmSync(join(prebuildDir, file));
+ }
+ }
+
+ // Compile-only: the SQLite amalgamation, the addon's own C++ sources, the
+ // gyp manifest and node-gyp's scratch output. Nothing in lib/ reads them.
+ for (const path of [ "deps", "src", "build", "binding.gyp" ]) {
+ rmSync(join(moduleDir, path), { recursive: true, force: true });
+ }
+ }
+
writeJson(relativePath: string, data: any) {
const fullPath = join(this.outDir, relativePath);
const dirPath = fullPath.substring(0, fullPath.lastIndexOf("/"));
@@ -154,6 +208,14 @@ export default class BuildHelper {
}
+/**
+ * The architecture currently being built for. The release workflows set these when
+ * a job's target differs from the runner it happens to be executing on.
+ */
+function targetArch() {
+ return process.env.TARGET_ARCH || process.env.MATRIX_ARCH || process.arch;
+}
+
function tryPath(paths: string[]) {
for (const path of paths) {
if (existsSync(path)) {