diff --git a/src/utils/PluginDriver.ts b/src/utils/PluginDriver.ts index 512120459e2..d4e8de71dfa 100644 --- a/src/utils/PluginDriver.ts +++ b/src/utils/PluginDriver.ts @@ -270,17 +270,6 @@ export class PluginDriver { return promise; } - // chains synchronously, ignores returns - hookSeqSync( - hookName: H, - args: Parameters, - replaceContext?: ReplaceContext - ): void { - for (const plugin of this.plugins) { - this.runHookSync(hookName, args, plugin, replaceContext); - } - } - /** * Run an async plugin hook and return the result. * @param hookName Name of the plugin hook. Must be either in `PluginHooks` or `OutputPluginValueHooks`. diff --git a/src/watch/WatchEmitter.ts b/src/watch/WatchEmitter.ts new file mode 100644 index 00000000000..edc68f85471 --- /dev/null +++ b/src/watch/WatchEmitter.ts @@ -0,0 +1,48 @@ +import { EventEmitter } from 'events'; + +type PromiseReturn any> = ( + ...args: Parameters +) => Promise>; + +export class WatchEmitter< + T extends { [event: string]: (...args: any) => any } +> extends EventEmitter { + private awaitedHandlers: { + [K in keyof T]?: PromiseReturn[]; + } = Object.create(null); + + constructor() { + super(); + // Allows more than 10 bundles to be watched without + // showing the `MaxListenersExceededWarning` to the user. + this.setMaxListeners(Infinity); + } + + // Will be overwritten by Rollup + async close(): Promise {} + + emitAndAwait( + event: K, + ...args: Parameters + ): Promise[]> { + this.emit(event as string, ...(args as any[])); + return Promise.all(this.getHandlers(event).map(handler => handler(...args))); + } + + onCurrentAwaited( + event: K, + listener: (...args: Parameters) => Promise> + ): this { + this.getHandlers(event).push(listener); + return this; + } + + removeAwaited(): this { + this.awaitedHandlers = {}; + return this; + } + + private getHandlers(event: K): PromiseReturn[] { + return this.awaitedHandlers[event] || (this.awaitedHandlers[event] = []); + } +} diff --git a/src/watch/watch-proxy.ts b/src/watch/watch-proxy.ts index 487a3365708..b7371b0398a 100644 --- a/src/watch/watch-proxy.ts +++ b/src/watch/watch-proxy.ts @@ -1,53 +1,10 @@ -import { EventEmitter } from 'events'; import type { RollupWatcher } from '../rollup/types'; import { ensureArray } from '../utils/ensureArray'; import { errInvalidOption, error } from '../utils/error'; import type { GenericConfigObject } from '../utils/options/options'; +import { WatchEmitter } from './WatchEmitter'; import { loadFsEvents } from './fsevents-importer'; -class WatchEmitter any }> extends EventEmitter { - private awaitedHandlers: { - [K in keyof T]?: ((...args: Parameters) => Promise>)[]; - } = {}; - - constructor() { - super(); - // Allows more than 10 bundles to be watched without - // showing the `MaxListenersExceededWarning` to the user. - this.setMaxListeners(Infinity); - } - - // Will be overwritten by Rollup - async close(): Promise {} - - emitAndAwait( - event: K, - ...args: Parameters - ): Promise[]> { - this.emit(event as string, ...(args as any[])); - const handlers = this.awaitedHandlers[event]; - if (!handlers) return Promise.resolve([]); - return Promise.all(handlers.map(handler => handler(...args))); - } - - onCurrentAwaited( - event: K, - listener: (...args: Parameters) => Promise> - ): this { - let handlers = this.awaitedHandlers[event]; - if (!handlers) { - handlers = this.awaitedHandlers[event] = []; - } - handlers.push(listener); - return this; - } - - removeAwaited(): this { - this.awaitedHandlers = {}; - return this; - } -} - export default function watch(configs: GenericConfigObject[] | GenericConfigObject): RollupWatcher { const emitter = new WatchEmitter() as RollupWatcher; const configArray = ensureArray(configs);