Skip to content

Commit

Permalink
Gracefully handle build errors in esbuild script
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lpsinger committed Dec 16, 2024
1 parent def8c76 commit 8117a76
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,35 @@ 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))
)
)
)
)
)
}
})
},
},
{
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)
)
}
})
},
},
Expand Down

0 comments on commit 8117a76

Please sign in to comment.