Skip to content

Commit ae13e77

Browse files
committed
watcher interface simplifications
1 parent 6ca5336 commit ae13e77

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

src/Graph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as acorn from 'acorn';
22
import injectDynamicImportPlugin from 'acorn-dynamic-import/lib/inject';
33
import injectImportMeta from 'acorn-import-meta/inject';
44
import { Program } from 'estree';
5+
import { EventEmitter } from 'events';
56
import GlobalScope from './ast/scopes/GlobalScope';
67
import { EntityPathTracker } from './ast/utils/EntityPathTracker';
78
import GlobalVariable from './ast/variables/GlobalVariable';
@@ -22,8 +23,7 @@ import {
2223
RollupWarning,
2324
SourceDescription,
2425
TreeshakingOptions,
25-
WarningHandler,
26-
Watcher
26+
WarningHandler
2727
} from './rollup/types';
2828
import { Asset, createAssetPluginHooks, finaliseAsset } from './utils/assetHooks';
2929
import { load, makeOnwarn, resolveId } from './utils/defaults';
@@ -68,7 +68,7 @@ export default class Graph {
6868
// deprecated
6969
treeshake: boolean;
7070

71-
constructor(options: InputOptions, watcher?: Watcher) {
71+
constructor(options: InputOptions, watcher?: EventEmitter) {
7272
this.curChunkIndex = 0;
7373
this.reassignmentTracker = new EntityPathTracker();
7474
this.cachedModules = new Map();

src/rollup/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventEmitter } from 'events';
12
import { optimizeChunks } from '../chunk-optimization';
23
import Graph from '../Graph';
34
import { createAddons } from '../utils/addons';
@@ -12,7 +13,6 @@ import mergeOptions, { GenericConfigObject } from '../utils/mergeOptions';
1213
import { basename, dirname, resolve } from '../utils/path';
1314
import { SOURCEMAPPING_URL } from '../utils/sourceMappingURL';
1415
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
15-
import { Watcher } from '../watch';
1616
import {
1717
InputOptions,
1818
OutputAsset,
@@ -144,8 +144,8 @@ function getInputOptions(rawInputOptions: GenericConfigObject): any {
144144
return inputOptions;
145145
}
146146

147-
let curWatcher: Watcher;
148-
export function setWatcher(watcher: Watcher) {
147+
let curWatcher: EventEmitter;
148+
export function setWatcher(watcher: EventEmitter) {
149149
curWatcher = watcher;
150150
}
151151

src/rollup/types.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface ModuleJSON {
6666
}
6767

6868
export interface PluginContext {
69-
watcher: Watcher;
69+
watcher: EventEmitter;
7070
resolveId: ResolveIdHook;
7171
isExternal: IsExternal;
7272
parse: (input: string, options: any) => ESTree.Program;
@@ -351,8 +351,6 @@ export interface RollupOptions extends InputOptions {
351351

352352
export function rollup(options: RollupOptions): Promise<RollupBuild>;
353353

354-
export interface Watcher extends EventEmitter {}
355-
356354
// chokidar watch options
357355
export interface WatchOptions {
358356
persistent?: boolean;
@@ -389,4 +387,8 @@ export interface RollupWatchOptions extends InputOptions {
389387
watch?: WatcherOptions;
390388
}
391389

392-
export function watch(configs: RollupWatchOptions[]): Watcher;
390+
export interface RollupWatcher extends EventEmitter {
391+
close(): void;
392+
}
393+
394+
export function watch(configs: RollupWatchOptions[]): RollupWatcher;

src/watch/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import rollup, { setWatcher } from '../rollup/index';
66
import {
77
InputOptions,
88
ModuleJSON,
9-
OutputChunk,
109
OutputOptions,
1110
RollupBuild,
12-
RollupSingleFileBuild,
11+
RollupWatcher,
1312
RollupWatchOptions
1413
} from '../rollup/types';
1514
import ensureArray from '../utils/ensureArray';
@@ -20,27 +19,38 @@ import { addTask, deleteTask } from './fileWatchers';
2019

2120
const DELAY = 200;
2221

23-
export class Watcher extends EventEmitter {
22+
export class Watcher {
23+
emitter: RollupWatcher;
2424
private buildTimeout: NodeJS.Timer;
2525
private running: boolean;
2626
private rerun: boolean = false;
2727
private tasks: Task[];
2828
private succeeded: boolean = false;
2929

3030
constructor(configs: RollupWatchOptions[]) {
31-
super();
31+
this.emitter = new class extends EventEmitter implements RollupWatcher {
32+
close: () => void;
33+
constructor(close: () => void) {
34+
super();
35+
this.close = close;
36+
}
37+
}(this.close.bind(this));
3238
this.tasks = ensureArray(configs).map(config => new Task(this, config));
3339
this.running = true;
3440
process.nextTick(() => this.run());
3541
}
3642

43+
emit(event: string, value: any) {
44+
this.emitter.emit(event, value);
45+
}
46+
3747
close() {
3848
if (this.buildTimeout) clearTimeout(this.buildTimeout);
3949
this.tasks.forEach(task => {
4050
task.close();
4151
});
4252

43-
this.removeAllListeners();
53+
this.emitter.removeAllListeners();
4454
}
4555

4656
invalidate() {
@@ -194,7 +204,7 @@ export class Task {
194204
});
195205
}
196206

197-
setWatcher(this.watcher);
207+
setWatcher(this.watcher.emitter);
198208
return rollup(options)
199209
.then(result => {
200210
if (this.closed) return;
@@ -218,11 +228,11 @@ export class Task {
218228

219229
return Promise.all(
220230
this.outputs.map(output => {
221-
return <Promise<OutputChunk | Record<string, OutputChunk>>>result.write(output);
231+
return result.write(output);
222232
})
223233
).then(() => result);
224234
})
225-
.then((result: RollupSingleFileBuild | RollupBuild) => {
235+
.then((result: RollupBuild) => {
226236
this.watcher.emit('event', {
227237
code: 'BUNDLE_END',
228238
input: this.inputOptions.input,
@@ -266,5 +276,5 @@ export class Task {
266276
}
267277

268278
export default function watch(configs: RollupWatchOptions[]) {
269-
return new Watcher(configs);
279+
return new Watcher(configs).emitter;
270280
}

0 commit comments

Comments
 (0)