2024-08-08 15:12:05 +03:00
|
|
|
import esbuild from 'esbuild'
|
|
|
|
import { wasmLoader } from 'esbuild-plugin-wasm'
|
2024-08-14 16:17:53 +03:00
|
|
|
import { fork } from 'node:child_process'
|
2024-08-08 15:12:05 +03:00
|
|
|
|
|
|
|
const watchMode = process.argv[2] === 'watch'
|
|
|
|
|
|
|
|
const ctx = await esbuild.context({
|
|
|
|
outfile: 'dist/main.mjs',
|
|
|
|
sourcemap: 'linked',
|
|
|
|
entryPoints: ['src/main.ts'],
|
|
|
|
bundle: true,
|
|
|
|
platform: 'node',
|
|
|
|
target: ['node20'],
|
|
|
|
conditions: watchMode ? ['source'] : [],
|
|
|
|
format: 'esm',
|
2024-08-14 16:17:53 +03:00
|
|
|
plugins: [wasmLoader(), ...(watchMode ? [runServerProcess()] : [])],
|
2024-08-08 15:12:05 +03:00
|
|
|
})
|
|
|
|
if (watchMode) await ctx.watch()
|
|
|
|
else {
|
|
|
|
await ctx.rebuild()
|
|
|
|
await ctx.dispose()
|
|
|
|
}
|
2024-08-14 16:17:53 +03:00
|
|
|
|
|
|
|
function runServerProcess() {
|
|
|
|
return {
|
|
|
|
name: 'run-server-process',
|
|
|
|
setup: async function (build) {
|
|
|
|
let abortCtl
|
|
|
|
build.onEnd(async function (result) {
|
|
|
|
if (result.errors.length > 0) return
|
|
|
|
if (abortCtl) abortCtl.abort()
|
|
|
|
let scriptPath = build.initialOptions.outfile
|
|
|
|
console.info(`Restarting "${scriptPath}".`)
|
|
|
|
abortCtl = forkChildProcess(scriptPath)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function forkChildProcess(scriptPath) {
|
|
|
|
const controller = new AbortController()
|
|
|
|
fork(scriptPath, { stdio: 'inherit', signal: controller.signal })
|
|
|
|
.on('error', error => {
|
|
|
|
if (error.constructor.name != 'AbortError') console.error(error)
|
|
|
|
})
|
|
|
|
.on('close', function (code) {
|
|
|
|
if (code) console.info(`Completed '${scriptPath}' with exit code ${code}.`)
|
|
|
|
})
|
|
|
|
return controller
|
|
|
|
}
|