build: bundle server with esbuild and copy native SQLite/Argon2 modules for .deb package

This commit is contained in:
Schwitzz
2026-10-30 13:02:38 -05:00
parent 02b824b6ab
commit 77576e2982
+45 -15
View File
@@ -26,11 +26,7 @@ mkdir -p \
"$STAGE_DIR/opt/pubscript/apps/web" \
"$STAGE_DIR/opt/pubscript/server/dist"
echo "==> [3/6] Deploying production server dependencies and bundling server entry..."
pnpm --filter @pubscript/server --prod deploy "$STAGE_DIR/opt/pubscript/server-runtime"
cp -a "$STAGE_DIR/opt/pubscript/server-runtime/node_modules" "$STAGE_DIR/opt/pubscript/node_modules"
rm -rf "$STAGE_DIR/opt/pubscript/server-runtime"
echo "==> [3/6] Bundling server entry and native SQLite/Argon2 runtime modules..."
cat > "$STAGE_DIR/opt/pubscript/package.json" <<'EOF'
{
"name": "pubscript-desktop-runtime",
@@ -40,23 +36,57 @@ cat > "$STAGE_DIR/opt/pubscript/package.json" <<'EOF'
}
EOF
# Bundle server TypeScript + @pubscript/core into ESM while keeping native/CJS deps external
# Bundle server TypeScript + Fastify + @pubscript/core into a single ESM file,
# keeping only native addons (better-sqlite3, argon2) external.
./apps/web/node_modules/.bin/esbuild server/src/index.ts \
--bundle \
--platform=node \
--target=node22 \
--format=esm \
--banner:js="import { createRequire as __psCreateRequire } from 'node:module'; const require = __psCreateRequire(import.meta.url);" \
--outfile="$STAGE_DIR/opt/pubscript/server/dist/server.mjs" \
--external:better-sqlite3 \
--external:argon2 \
--external:fastify \
--external:@fastify/cors \
--external:@fastify/cookie \
--external:@fastify/static \
--external:@fastify/websocket \
--external:ws \
--external:ulid \
--external:zod
--external:argon2
# Copy better-sqlite3 and argon2 plus their transitive runtime dependencies
node - "$ROOT_DIR/server" "$STAGE_DIR/opt/pubscript/node_modules" <<'NODE_EOF'
import { cpSync, existsSync, mkdirSync, readFileSync, realpathSync } from "node:fs";
import { dirname, join } from "node:path";
import { createRequire } from "node:module";
const [serverDir, outModules] = process.argv.slice(2);
mkdirSync(outModules, { recursive: true });
const seen = new Set();
function copyPkg(pkgName, fromDir) {
if (seen.has(pkgName)) return;
seen.add(pkgName);
const req = createRequire(join(fromDir, "package.json"));
let pkgJsonPath;
try {
pkgJsonPath = req.resolve(`${pkgName}/package.json`);
} catch {
const entry = req.resolve(pkgName);
let cur = dirname(realpathSync(entry));
while (cur !== "/" && !existsSync(join(cur, "package.json"))) {
cur = dirname(cur);
}
pkgJsonPath = join(cur, "package.json");
}
const realPkgDir = dirname(realpathSync(pkgJsonPath));
const destDir = join(outModules, pkgName);
mkdirSync(dirname(destDir), { recursive: true });
cpSync(realPkgDir, destDir, { recursive: true, dereference: true });
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, "utf8"));
const deps = Object.keys(pkgJson.dependencies ?? {});
for (const dep of deps) {
copyPkg(dep, realPkgDir);
}
}
copyPkg("better-sqlite3", serverDir);
copyPkg("argon2", serverDir);
NODE_EOF
cp -a apps/web/dist "$STAGE_DIR/opt/pubscript/apps/web/dist"