Skip to content

Commit bdcf6ee

Browse files
committed
refactor(build): bundle backend for pkg-compatible esm source
1 parent 1c8a48b commit bdcf6ee

11 files changed

Lines changed: 1052 additions & 387 deletions

package-lock.json

Lines changed: 592 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
"start:dev": "nodemon --watch src --ext ts --exec \"node dist/index.js\"",
1212
"build": "npm run build:backend && npm run build:webui",
1313
"build:watch": "concurrently \"npm run build:backend:watch\" \"npm run build:webui:watch\"",
14-
"build:backend": "tsc",
15-
"build:backend:watch": "tsc --watch",
14+
"build:backend": "tsx scripts/build-backend.ts",
15+
"build:backend:watch": "tsx scripts/build-backend.ts --watch",
1616
"build:webui": "tsc --project src/webui/static/tsconfig.json && npm run build:webui:copy",
1717
"build:webui:watch": "tsc --project src/webui/static/tsconfig.json --watch",
1818
"build:webui:copy": "node scripts/copy-webui-assets.js",
1919
"build:linux": "npm run build && pkg . --targets node20-linux-x64 --output dist/flashforge-webui-linux-x64",
2020
"build:linux-arm": "npm run build && pkg . --targets node20-linux-arm64 --output dist/flashforge-webui-linux-arm64",
21-
"build:linux-armv7": "npm run build && pkg . --targets node20.19.5-linux-armv7 --output dist/flashforge-webui-linux-armv7",
21+
"build:linux-armv7": "npm run build && pkg . --targets node20-linuxstatic-armv7 --output dist/flashforge-webui-linux-armv7",
2222
"build:win": "npm run build && pkg . --targets node20-win-x64 --output dist/flashforge-webui-win-x64.exe",
2323
"build:mac": "npm run build && pkg . --targets node20-macos-x64 --output dist/flashforge-webui-macos-x64",
2424
"build:mac-arm": "npm run build && pkg . --targets node20-macos-arm64 --output dist/flashforge-webui-macos-arm64",
@@ -76,6 +76,7 @@
7676
"@types/ws": "^8.5.13",
7777
"@yao-pkg/pkg": "^5.15.0",
7878
"concurrently": "^9.1.2",
79+
"esbuild": "^0.27.3",
7980
"jest": "^30.2.0",
8081
"nodemon": "^3.1.11",
8182
"rimraf": "^6.0.1",

scripts/build-backend.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { type BuildOptions, build, context } from 'esbuild';
2+
import * as fs from 'fs/promises';
3+
import * as path from 'path';
4+
5+
const DIST_DIR = path.join(process.cwd(), 'dist');
6+
const OUTFILE = path.join(DIST_DIR, 'index.js');
7+
const WATCH_FLAG = '--watch';
8+
9+
const buildOptions: BuildOptions = {
10+
entryPoints: ['src/index.ts'],
11+
outfile: OUTFILE,
12+
bundle: true,
13+
charset: 'utf8',
14+
format: 'cjs',
15+
keepNames: true,
16+
legalComments: 'none',
17+
logLevel: 'info',
18+
packages: 'external',
19+
platform: 'node',
20+
sourcemap: true,
21+
target: ['node20'],
22+
treeShaking: false,
23+
tsconfig: 'tsconfig.json',
24+
};
25+
26+
async function ensureDistDirectory(): Promise<void> {
27+
await fs.mkdir(DIST_DIR, { recursive: true });
28+
}
29+
30+
async function runBuild(): Promise<void> {
31+
await ensureDistDirectory();
32+
await build(buildOptions);
33+
}
34+
35+
async function runWatch(): Promise<void> {
36+
await ensureDistDirectory();
37+
38+
const buildContext = await context(buildOptions);
39+
40+
const shutdown = async (): Promise<void> => {
41+
await buildContext.dispose();
42+
process.exit(0);
43+
};
44+
45+
process.on('SIGINT', () => {
46+
void shutdown();
47+
});
48+
process.on('SIGTERM', () => {
49+
void shutdown();
50+
});
51+
52+
await buildContext.watch();
53+
process.stdout.write('[build-backend] Watching for backend changes...\n');
54+
55+
await new Promise(() => {
56+
// Keep the process alive while esbuild runs in watch mode.
57+
});
58+
}
59+
60+
async function main(): Promise<void> {
61+
if (process.argv.includes(WATCH_FLAG)) {
62+
await runWatch();
63+
return;
64+
}
65+
66+
await runBuild();
67+
}
68+
69+
void main().catch((error) => {
70+
const message = error instanceof Error ? (error.stack ?? error.message) : String(error);
71+
process.stderr.write(`${message}\n`);
72+
process.exit(1);
73+
});

0 commit comments

Comments
 (0)