shellphone.app/scripts/build-server.js

87 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-05-14 10:22:06 +00:00
const path = require("path");
const esbuild = require("esbuild");
const { nodeExternalsPlugin } = require("esbuild-node-externals");
const ps = require("ps-node");
2022-05-14 10:22:06 +00:00
const basePath = process.cwd();
const args = process.argv.slice(2);
const watch = args.includes("--watch");
esbuild
.build({
write: true,
2022-06-26 15:01:55 +00:00
outfile: path.join(basePath, "server/index.js"),
entryPoints: [path.join(basePath, "server/index.ts")],
2022-05-14 10:22:06 +00:00
platform: "node",
format: "cjs",
bundle: true,
sourcemap: "inline",
2022-05-14 10:22:06 +00:00
plugins: [
nodeExternalsPlugin({ packagePath: path.join(basePath, "package.json") }),
{
name: "remix-bundle-external",
setup(build) {
2022-06-26 15:01:55 +00:00
build.onResolve({ filter: /^\.\.\/build$/ }, () => ({ external: true }));
2022-05-14 10:22:06 +00:00
},
},
],
watch: watch
? {
onRebuild(error, buildResult) {
const warnings = error?.warnings || buildResult?.warnings;
const errors = error?.errors || buildResult?.errors;
if (warnings.length) {
console.log(esbuild.formatMessages(warnings, { kind: "warning" }));
}
if (errors.length) {
console.log(esbuild.formatMessages(errors, { kind: "error" }));
process.exit(1);
}
ps.lookup(
{
command: "node",
arguments: "./server/index.js",
},
(error, processes) => {
if (error) {
throw new Error(error);
}
if (processes.length === 0) {
return;
}
const devServerProcess = processes.reduce((prev, current) => {
if (prev.pid > current.pid) {
return prev;
}
return current;
}, processes[0]);
process.kill(devServerProcess.pid, "SIGUSR2");
},
);
console.log("Server rebuilt successfully");
2022-05-14 10:22:06 +00:00
},
}
: false,
})
.then(({ errors, warnings }) => {
if (warnings.length) {
console.log(esbuild.formatMessages(warnings, { kind: "warning" }));
}
if (errors.length) {
console.log(esbuild.formatMessages(errors, { kind: "error" }));
process.exit(1);
}
console.log("Server build succeeded");
})
.catch((err) => {
console.error(err);
process.exit(1);
});