From 91e8058c52d77faa7cdae223f1db22241acc3040 Mon Sep 17 00:00:00 2001 From: nikek Date: Thu, 2 Apr 2020 10:35:04 +0200 Subject: [PATCH] refactor plugin build function --- packages/cli/src/commands/plugin/build.ts | 33 ++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/commands/plugin/build.ts b/packages/cli/src/commands/plugin/build.ts index ac8193c0fb..5bb9e171b0 100644 --- a/packages/cli/src/commands/plugin/build.ts +++ b/packages/cli/src/commands/plugin/build.ts @@ -20,15 +20,30 @@ import { Command } from 'commander'; export default async (cmd: Command) => { if (cmd.watch) { - const watcher = watch(conf); - watcher.on('event', console.log); - await new Promise(() => {}); - } else { - const bundle = await rollup({ - input: conf.input, - plugins: conf.plugins, + // We're not resolving this promise because watch() doesn't have any exit event. + // Instead we just wait until the user sends an interrupt signal. + return new Promise(() => { + const watcher = watch(conf); + watcher.on('event', event => { + // START — the watcher is (re)starting + // BUNDLE_START — building an individual bundle + // BUNDLE_END — finished building a bundle + // END — finished building all bundles + // ERROR — encountered an error while bundling + + if (event.code === 'ERROR') { + console.log(event.error); + } else { + console.log(event.code); + } + }); }); - await bundle.generate(conf.output as OutputOptions); - await bundle.write(conf.output as OutputOptions); } + + const bundle = await rollup({ + input: conf.input, + plugins: conf.plugins, + }); + await bundle.generate(conf.output as OutputOptions); + await bundle.write(conf.output as OutputOptions); };