Skip to content

Instantly share code, notes, and snippets.

@banagale
Last active May 21, 2024 16:06
Show Gist options
  • Save banagale/dbad61a74b654b8c77dd69f1d8a0482d to your computer and use it in GitHub Desktop.
Save banagale/dbad61a74b654b8c77dd69f1d8a0482d to your computer and use it in GitHub Desktop.
An example javascript api-based `esbuild` build script with a watch and automatic post-build bash script execution.
// As discussed in https://github.com/evanw/esbuild/issues/1688
require('esbuild').build({
entryPoints: ['./briefings/frontend/src/briefings.ts'],
outfile: './briefings/frontend/build/briefings-fe.js',
bundle: true,
minify: true,
target: 'es2017',
color: true,
watch: {
onRebuild(error, result) {
const verboseOutput = false;
console.log(`File change at ${new Date()}`);
console.log('Rebuilding...');
if (error) console.error('Watch build failed:', error);
else if (verboseOutput) console.log('Build complete', result);
else console.log('Build complete.');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function runPostBuildScript() {
try {
console.log('Running post-build script...');
const { stdout, stderr } = await exec('./postbuild.sh');
if (stdout !== '') console.log(stdout);
if (stderr !== '') console.log(`Errors :, ${stderr}`);
} catch (e) {
console.error(e); // should contain code (exit code) and signal (that caused the termination).
}
}
runPostBuildScript().then(() => {
console.log('Post-build script complete.\n');
});
},
},
}).then(() => {
console.log('esbuild is watching for changes. (ctrl-c to quit)\n');
// Call "stop" on the result when you're done
// result.stop();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment