Skip to content

Commit

Permalink
refactor: remove iife
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 14, 2022
1 parent 4eb3e36 commit 8e729c9
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions 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';
Expand All @@ -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<RollupOptions>, 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);
}

0 comments on commit 8e729c9

Please sign in to comment.