-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathesbuild.mjs
More file actions
75 lines (68 loc) · 2.12 KB
/
Copy pathesbuild.mjs
File metadata and controls
75 lines (68 loc) · 2.12 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
// Builds a single-file zero-dependency version of the API server
// with an embedded static version of the frontend.
import * as esbuild from "esbuild";
import { readFile, readdir } from "fs/promises";
import { resolve, relative } from "path";
async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
let files = await Promise.all(
dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
})
);
return Array.prototype.concat(...files).filter((f) => !f.includes("cache"));
}
const generateFrontendManifest = async (rootPath) => {
const output = [`module.exports = {`];
let files = await getFiles(rootPath);
for (const file of files) {
const key = relative(rootPath, file);
output.push(` "${key}": require("${file}"),`);
}
output.push(`};`);
return output.join("\n");
};
let frontendBundlePlugin = {
name: "frontendBundle",
setup(build) {
build.onResolve({ filter: /.*frontend-stub$/ }, async (args) => {
return {
path: resolve(args.resolveDir, "..", "..", "www", "static-build"),
namespace: "frontendBundle",
};
});
// Any files from the static-build directory should be bundled as binary
build.onLoad(
{ namespace: "frontendBundle", filter: /.*static\-build/ },
async (args) => {
return {
contents: await generateFrontendManifest(args.path),
loader: "js",
resolveDir: resolve(args.resolveDir, args.path),
};
}
);
// Any files from the static-build directory should be bundled as binary
build.onLoad({ filter: /.*static\-build.+/ }, async (args) => {
const contents = await readFile(args.path);
return {
contents: contents,
loader: "binary",
};
});
},
};
(async () => {
await esbuild.build({
entryPoints: ["./src/cli.ts"],
bundle: true,
platform: "node",
outfile: "./dist-esbuild/livepeer-api",
target: "node18",
external: ["pg-native"],
sourcemap: "inline",
plugins: [frontendBundlePlugin],
minify: true,
});
})();