diff --git a/src/utils/hookActions.ts b/src/utils/hookActions.ts index 69269bbb27f..01608209f55 100644 --- a/src/utils/hookActions.ts +++ b/src/utils/hookActions.ts @@ -1,3 +1,4 @@ +import { EventEmitter } from 'events'; import process from 'process'; import { HookAction, PluginDriver } from './PluginDriver'; @@ -19,6 +20,13 @@ function formatAction([pluginName, hookName, args]: HookAction): string { return action; } +// We do not directly listen on process to avoid max listeners warnings for +// complicated build processes +const beforeExitEvent = 'beforeExit'; +const beforeExitEmitter = new EventEmitter(); +beforeExitEmitter.setMaxListeners(0); +process.on(beforeExitEvent, () => beforeExitEmitter.emit(beforeExitEvent)); + export async function catchUnfinishedHookActions( pluginDriver: PluginDriver, callback: () => Promise @@ -34,10 +42,10 @@ export async function catchUnfinishedHookActions( ) ); }; - process.once('beforeExit', handleEmptyEventLoop); + beforeExitEmitter.once(beforeExitEvent, handleEmptyEventLoop); }); const result = await Promise.race([callback(), emptyEventLoopPromise]); - process.off('beforeExit', handleEmptyEventLoop!); + beforeExitEmitter.off(beforeExitEvent, handleEmptyEventLoop!); return result; }