From b703d1bd4baa93d747b255a06bf6b76475105d45 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sat, 6 Aug 2022 07:36:11 +0200 Subject: [PATCH] Improve coverage --- src/rollup/rollup.ts | 18 +++++++----------- src/utils/PluginDriver.ts | 40 ++++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/rollup/rollup.ts b/src/rollup/rollup.ts index f34d06ad11b..8f9ec428c28 100644 --- a/src/rollup/rollup.ts +++ b/src/rollup/rollup.ts @@ -129,17 +129,13 @@ function applyOptionHook(watchMode: boolean) { inputOptions: Promise, plugin: Plugin ): Promise => { - if (plugin.options) { - const handler = 'handler' in plugin.options ? plugin.options.handler : plugin.options; - return ( - ((await handler.call( - { meta: { rollupVersion, watchMode } }, - await inputOptions - )) as GenericConfigObject) || inputOptions - ); - } - - return inputOptions; + const handler = 'handler' in plugin.options! ? plugin.options.handler : plugin.options!; + return ( + ((await handler.call( + { meta: { rollupVersion, watchMode } }, + await inputOptions + )) as GenericConfigObject) || inputOptions + ); }; } diff --git a/src/utils/PluginDriver.ts b/src/utils/PluginDriver.ts index d2070899883..7cd92ad75b9 100644 --- a/src/utils/PluginDriver.ts +++ b/src/utils/PluginDriver.ts @@ -170,13 +170,11 @@ export class PluginDriver { args: Parameters, replaceContext?: ReplaceContext ): Promise { - const promises: Promise[] = []; - for (const plugin of this.getSortedPlugins(hookName)) { - const hookPromise = this.runHook(hookName, args, plugin, replaceContext); - if (!hookPromise) continue; - promises.push(hookPromise); - } - return Promise.all(promises).then(() => {}); + return Promise.all( + this.getSortedPlugins(hookName).map(plugin => + this.runHook(hookName, args, plugin, replaceContext) + ) + ).then(() => {}); } // chains, reduces returned value, handling the reduced value as the first hook argument @@ -192,14 +190,14 @@ export class PluginDriver { ): Promise> { let promise = Promise.resolve(arg0); for (const plugin of this.getSortedPlugins(hookName)) { - promise = promise.then(arg0 => { - const args = [arg0, ...rest] as Parameters; - const hookPromise = this.runHook(hookName, args, plugin, replaceContext); - if (!hookPromise) return arg0; - return hookPromise.then(result => - reduce.call(this.pluginContexts.get(plugin), arg0, result, plugin) - ); - }); + promise = promise.then(arg0 => + this.runHook( + hookName, + [arg0, ...rest] as Parameters, + plugin, + replaceContext + ).then(result => reduce.call(this.pluginContexts.get(plugin), arg0, result, plugin)) + ); } return promise; } @@ -239,13 +237,11 @@ export class PluginDriver { } } )) { - promise = promise.then(value => { - const hookPromise = this.runHook(hookName, args, plugin); - if (!hookPromise) return value; - return hookPromise.then(result => + promise = promise.then(value => + this.runHook(hookName, args, plugin).then(result => reduce.call(this.pluginContexts.get(plugin), value, result, plugin) - ); - }); + ) + ); } return promise; } @@ -314,7 +310,7 @@ export class PluginDriver { args: unknown[], plugin: Plugin, replaceContext?: ReplaceContext | null - ): Promise | undefined { + ): Promise { // We always filter for plugins that support the hook before running it const hook = plugin[hookName]!; const handler = typeof hook === 'object' ? hook.handler : hook;