How can I catch esbuild's build error and run a script?

303 views Asked by At

Is there a way to catch esbuild's build error and run a script? I'm using it with npm, VSCode, and vanilla JS.

I can't find any documentation about it.

1

There are 1 answers

5
Hatem On

esbuild uses promises, so you can catch errors as usual

import * as esbuild from 'esbuild'

try {
  await esbuild.build({
    entryPoints: ['app.ts'],
    bundle: true,
    outdir: 'dist',
  });
} catch {
  // build failed
}

// or
esbuild.build({
  entryPoints: ['app.ts'],
  bundle: true,
  outdir: 'dist',
}).catch(() => {
  // build failed
});