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/apps/web" \
"$STAGE_DIR/opt/pubscript/server/dist" "$STAGE_DIR/opt/pubscript/server/dist"
echo "==> [3/6] Deploying production server dependencies and bundling server entry..." echo "==> [3/6] Bundling server entry and native SQLite/Argon2 runtime modules..."
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"
cat > "$STAGE_DIR/opt/pubscript/package.json" <<'EOF' cat > "$STAGE_DIR/opt/pubscript/package.json" <<'EOF'
{ {
"name": "pubscript-desktop-runtime", "name": "pubscript-desktop-runtime",
@@ -40,23 +36,57 @@ cat > "$STAGE_DIR/opt/pubscript/package.json" <<'EOF'
} }
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 \ ./apps/web/node_modules/.bin/esbuild server/src/index.ts \
--bundle \ --bundle \
--platform=node \ --platform=node \
--target=node22 \ --target=node22 \
--format=esm \ --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" \ --outfile="$STAGE_DIR/opt/pubscript/server/dist/server.mjs" \
--external:better-sqlite3 \ --external:better-sqlite3 \
--external:argon2 \ --external:argon2
--external:fastify \
--external:@fastify/cors \ # Copy better-sqlite3 and argon2 plus their transitive runtime dependencies
--external:@fastify/cookie \ node - "$ROOT_DIR/server" "$STAGE_DIR/opt/pubscript/node_modules" <<'NODE_EOF'
--external:@fastify/static \ import { cpSync, existsSync, mkdirSync, readFileSync, realpathSync } from "node:fs";
--external:@fastify/websocket \ import { dirname, join } from "node:path";
--external:ws \ import { createRequire } from "node:module";
--external:ulid \
--external:zod 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" cp -a apps/web/dist "$STAGE_DIR/opt/pubscript/apps/web/dist"