From 8117a7647303d210867b6efcfebbdce755dcc4a5 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Mon, 16 Dec 2024 12:30:28 -0500 Subject: [PATCH] Gracefully handle build errors in esbuild script We have two plugins which each take the metafile as an argument. However, if the build fails, then the metafile arguemnt will not be set. In this case esbuild will print the original and hopefully helpful error message, but also unhelpful error messages about the metafile being undefined. Suppress the latter error messages by checking first that the metafile is defined. --- esbuild.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/esbuild.js b/esbuild.js index 6ee2bad67..77532714f 100644 --- a/esbuild.js +++ b/esbuild.js @@ -31,16 +31,22 @@ const options = { { name: 'copy Node API modules to output directories', setup(build) { - build.onEnd(async ({ metafile: { outputs } }) => { - await Promise.all( - Object.entries(outputs).flatMap(([entryPoint, { inputs }]) => - Object.keys(inputs) - .filter((input) => extname(input) === '.node') - .map((input) => - copyFile(input, join(dirname(entryPoint), basename(input))) - ) + build.onEnd(async ({ metafile }) => { + if (metafile) { + await Promise.all( + Object.entries(metafile.outputs).flatMap( + ([entryPoint, { inputs }]) => + Object.keys(inputs) + .filter((input) => extname(input) === '.node') + .map((input) => + copyFile( + input, + join(dirname(entryPoint), basename(input)) + ) + ) + ) ) - ) + } }) }, }, @@ -48,10 +54,12 @@ const options = { name: 'write metafile to output directory', setup(build) { build.onEnd(async ({ metafile }) => { - await writeFile( - join(build.initialOptions.outdir, 'metafile.lambda.json'), - JSON.stringify(metafile) - ) + if (metafile) { + await writeFile( + join(build.initialOptions.outdir, 'metafile.lambda.json'), + JSON.stringify(metafile) + ) + } }) }, },