This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sea.build.js
62 lines (53 loc) · 1.86 KB
/
sea.build.js
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
import { execSync } from "child_process";
import { build } from "esbuild";
import fs from "fs";
import path from "path";
const ESBUILD_NAME = "Node SEA Bundle";
const BUILD_DIR = path.join(import.meta.dirname, "build", "sea");
const BUNDLE_FILE = path.join(BUILD_DIR, "index.cjs");
const injectSeaApi = function () {
const bundleContent = fs.readFileSync(BUNDLE_FILE, "utf8");
const bundleContentInjected = bundleContent
.replace(
`"use strict";`,
`"use strict";\nvar cfSeaApi = require("node:sea"); // injected by Cinnabar Forge\n`,
)
.replace(
`var binary = getBinarySync(file)`,
`var binary = cfSeaApi.getAsset("node-sqlite3-wasm.wasm")`,
);
fs.writeFileSync(BUNDLE_FILE, bundleContentInjected);
};
const buildCrossPlatform = function () {
const isWindows = process.platform === "win32";
const configCommand = "node --experimental-sea-config sea.config.json";
execSync(configCommand, { stdio: "inherit" });
const nodeBinaryPath = process.execPath;
const baseName = "app";
const destinationPath = path.join(
BUILD_DIR,
isWindows ? `${baseName}.exe` : baseName,
);
fs.copyFileSync(nodeBinaryPath, destinationPath);
const postjectCommandBase = `npx postject ${destinationPath} NODE_SEA_BLOB ${path.join(BUILD_DIR, "prep.blob")} --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2`;
const postjectCommand = isWindows
? postjectCommandBase.replace(/\//g, "\\")
: postjectCommandBase;
execSync(postjectCommand, { stdio: "inherit" });
};
build({
bundle: true,
entryPoints: ["src/index.ts"],
external: [],
outfile: BUNDLE_FILE,
platform: "node",
})
.then((r) => {
console.log(`${ESBUILD_NAME} has been built to ${BUNDLE_FILE}`);
injectSeaApi();
buildCrossPlatform();
})
.catch((e) => {
console.log(`Error building ${ESBUILD_NAME}: ${e.message}`);
process.exit(1);
});