Skip to content

Instantly share code, notes, and snippets.

@kevinfiol
Created January 22, 2023 21:54
Show Gist options
  • Save kevinfiol/e692502aec6e0c2402607bcc68f8babe to your computer and use it in GitHub Desktop.
Save kevinfiol/e692502aec6e0c2402607bcc68f8babe to your computer and use it in GitHub Desktop.
esbuild w context
/** @type {esbuild.BuildOptions} **/
const config = {
format: 'iife',
entryPoints: [ENTRY],
outfile: OUTFILE,
bundle: false,
plugins: [{
name: 'on-end',
setup(build) {
build.onEnd(({ errors }) => {
if (errors[0]) logError(errors[0]);
else logSuccess();
})
}
}]
};
const ctx = await esbuild.context(config);
if (DEV) {
// enable watch mode
// await ctx.watch();
// enable server
await ctx.serve({
port: SERVE_PORT,
servedir: SERVE_DIR
}).then(() => {
console.log(`Serving [${SERVE_DIR}] at http://localhost:${SERVE_PORT}`)
});
// free up resources on close
process.on('exit', ctx.dispose);
} else {
// run build once && dispose context
await ctx.rebuild().finally(ctx.dispose);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment