-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild-server.mjs
More file actions
78 lines (70 loc) · 2.39 KB
/
Copy pathbuild-server.mjs
File metadata and controls
78 lines (70 loc) · 2.39 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* esbuild script for compiling the server and worker entry points to JS.
*
* Used by the Electron packaging pipeline. The dev workflow (tsx --watch)
* is unaffected — this script is only invoked via `pnpm bind`.
*
* Output structure mirrors the source layout so that relative URL
* resolution (e.g. `new URL('../workers/…', import.meta.url)`) still
* works in the compiled output after swapping .ts → .js extensions.
*
* dist-server/
* server/index.js
* workers/abject-worker-node.js
* workers/ui-worker-node.js
* workers/p2p-worker-node.js
*/
import { build } from 'esbuild';
import { builtinModules } from 'node:module';
// All node: built-ins plus their un-prefixed variants
const nodeExternals = [
...builtinModules,
...builtinModules.map((m) => `node:${m}`),
];
// Native / optional deps that must stay as require/import at runtime
const runtimeExternals = [
'node-datachannel',
'node-datachannel/polyfill',
'ws',
'playwright',
'linkedom',
'tsx/esm/api',
];
const external = [...nodeExternals, ...runtimeExternals];
const shared = {
bundle: true,
format: 'esm',
platform: 'node',
target: 'node20',
external,
sourcemap: true,
// Preserve import.meta.url so worker path resolution works
define: {},
};
await build({
...shared,
entryPoints: { 'server/index': 'server/index.ts' },
outdir: 'dist-server',
banner: {
// Shim require() for ESM bundles that import CJS packages at runtime
// Alias the imported binding so it can't collide with a `createRequire`
// import inside a bundled ESM dependency (e.g. fflate).
js: `import { createRequire as __abjectsCreateRequire } from 'node:module'; const require = __abjectsCreateRequire(import.meta.url);`,
},
});
// Workers are separate entry points (they run in worker_threads)
await build({
...shared,
entryPoints: {
'workers/abject-worker-node': 'workers/abject-worker-node.ts',
'workers/ui-worker-node': 'workers/ui-worker-node.ts',
'workers/p2p-worker-node': 'workers/p2p-worker-node.ts',
},
outdir: 'dist-server',
banner: {
// Alias the imported binding so it can't collide with a `createRequire`
// import inside a bundled ESM dependency (e.g. fflate).
js: `import { createRequire as __abjectsCreateRequire } from 'node:module'; const require = __abjectsCreateRequire(import.meta.url);`,
},
});
console.log('Server build complete → dist-server/');