diff --git a/src/watch/watch-proxy.ts b/src/watch/watch-proxy.ts index e44e1966893..98d65616ac3 100644 --- a/src/watch/watch-proxy.ts +++ b/src/watch/watch-proxy.ts @@ -1,5 +1,5 @@ import { handleError } from '../../cli/logging'; -import type { RollupOptions, RollupWatcher } from '../rollup/types'; +import type { MaybeArray, RollupOptions, RollupWatcher } from '../rollup/types'; import { ensureArray } from '../utils/ensureArray'; import { error, errorInvalidOption } from '../utils/error'; import { mergeOptions } from '../utils/options/mergeOptions'; @@ -9,24 +9,26 @@ import { loadFsEvents } from './fsevents-importer'; export default function watch(configs: RollupOptions[] | RollupOptions): RollupWatcher { const emitter = new WatchEmitter() as RollupWatcher; - (async () => { - const optionsList = await Promise.all(ensureArray(configs).map(config => mergeOptions(config))); - const watchOptionsList = optionsList.filter(config => config.watch !== false); - if (watchOptionsList.length === 0) { - return error( - errorInvalidOption( - 'watch', - 'watch', - 'there must be at least one config where "watch" is not set to "false"' - ) - ); - } - await loadFsEvents(); - const { Watcher } = await import('./watch'); - new Watcher(watchOptionsList, emitter); - })().catch(error => { + watchInternal(configs, emitter).catch(error => { handleError(error); }); return emitter; } + +async function watchInternal(configs: MaybeArray, emitter: RollupWatcher) { + const optionsList = await Promise.all(ensureArray(configs).map(config => mergeOptions(config))); + const watchOptionsList = optionsList.filter(config => config.watch !== false); + if (watchOptionsList.length === 0) { + return error( + errorInvalidOption( + 'watch', + 'watch', + 'there must be at least one config where "watch" is not set to "false"' + ) + ); + } + await loadFsEvents(); + const { Watcher } = await import('./watch'); + new Watcher(watchOptionsList, emitter); +}