File size: 1,326 Bytes
dcedb25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Bundles desktop/src/server-host.ts (and through it the whole server) into
// build/server.mjs. esbuild resolves the server's `.js`-suffixed TS imports
// and flattens the @freellmapi/shared workspace symlink — only better-sqlite3
// stays external (native module, electron-rebuilt in desktop/node_modules).
import { build } from 'esbuild';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const githubSha = process.env.GITHUB_SHA;
const hasBuildIdentity = /^[0-9a-f]{40}$/i.test(githubSha ?? '');

await build({
  entryPoints: [path.resolve(__dirname, '../src/server-host.ts')],
  bundle: true,
  platform: 'node',
  format: 'esm',
  target: 'node20',
  outfile: path.resolve(__dirname, '../build/server.mjs'),
  external: ['better-sqlite3'],
  define: {
    'process.env.FREELLMAPI_COMMIT_SHA': JSON.stringify(hasBuildIdentity ? githubSha : ''),
    'process.env.FREELLMAPI_INSTALL_METHOD': JSON.stringify(hasBuildIdentity ? 'desktop' : ''),
  },
  // Some inlined CJS deps (express internals) reference `require` at runtime;
  // give the ESM bundle a working one.
  banner: {
    js: "import { createRequire as __createRequire } from 'node:module'; const require = __createRequire(import.meta.url);",
  },
  logLevel: 'info',
});