From c9ad4a6b787e2e5c488661ca5714318b07591875 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 20 Mar 2023 18:36:18 -0500 Subject: [PATCH] fix: refactor esbuild setup to support watch (#6608) --- scripts/cmd/build.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/cmd/build.js b/scripts/cmd/build.js index 4412693c3..030bcc0c5 100644 --- a/scripts/cmd/build.js +++ b/scripts/cmd/build.js @@ -81,15 +81,15 @@ export default async function build(...args) { return; } - const builder = await esbuild.build({ - ...config, - watch: { - onRebuild(error, result) { + const rebuildPlugin = { + name: 'astro:rebuild', + setup(build) { + build.onEnd(async result => { if (prebuilds.length) { - prebuild(...prebuilds); + await prebuild(...prebuilds); } const date = dt.format(new Date()); - if (error || (result && result.errors.length)) { + if (result && result.errors.length) { console.error(dim(`[${date}] `) + red(error || result.errors.join('\n'))); } else { if (result.warnings.length) { @@ -99,12 +99,17 @@ export default async function build(...args) { } console.log(dim(`[${date}] `) + green('✔ updated')); } - }, + }); }, + }; + + const builder = await esbuild.context({ + ...config, entryPoints, outdir, format, plugins: [ + rebuildPlugin, svelte({ isDev }), ...(copyWASM ? [ @@ -120,6 +125,8 @@ export default async function build(...args) { ], }); + await builder.watch(); + process.on('beforeExit', () => { builder.stop && builder.stop(); });