Skip to content

Commit

Permalink
watcher interface simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 12, 2018
1 parent 566f8b2 commit 05ee58b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as acorn from 'acorn';
import injectDynamicImportPlugin from 'acorn-dynamic-import/lib/inject';
import injectImportMeta from 'acorn-import-meta/inject';
import { Program } from 'estree';
import { EventEmitter } from 'events';
import GlobalScope from './ast/scopes/GlobalScope';
import { EntityPathTracker } from './ast/utils/EntityPathTracker';
import GlobalVariable from './ast/variables/GlobalVariable';
Expand All @@ -19,8 +20,7 @@ import {
SerializablePluginCache,
SourceDescription,
TreeshakingOptions,
WarningHandler,
Watcher
WarningHandler
} from './rollup/types';
import { finaliseAsset } from './utils/assetHooks';
import {
Expand Down Expand Up @@ -80,7 +80,7 @@ export default class Graph {
// deprecated
treeshake: boolean;

constructor(options: InputOptions, watcher?: Watcher) {
constructor(options: InputOptions, watcher?: EventEmitter) {
this.curChunkIndex = 0;
this.deoptimizationTracker = new EntityPathTracker();
this.cachedModules = new Map();
Expand Down
6 changes: 3 additions & 3 deletions src/rollup/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from 'events';
import { optimizeChunks } from '../chunk-optimization';
import Graph from '../Graph';
import { createAddons } from '../utils/addons';
Expand All @@ -11,7 +12,6 @@ import mergeOptions, { GenericConfigObject } from '../utils/mergeOptions';
import { basename, dirname, resolve } from '../utils/path';
import { SOURCEMAPPING_URL } from '../utils/sourceMappingURL';
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
import { Watcher } from '../watch';
import {
InputOptions,
OutputAsset,
Expand Down Expand Up @@ -132,8 +132,8 @@ function getInputOptions(rawInputOptions: GenericConfigObject): any {
return inputOptions;
}

let curWatcher: Watcher;
export function setWatcher(watcher: Watcher) {
let curWatcher: EventEmitter;
export function setWatcher(watcher: EventEmitter) {
curWatcher = watcher;
}

Expand Down
10 changes: 6 additions & 4 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface PluginCache {
}

export interface PluginContext {
watcher: Watcher;
watcher: EventEmitter;
addWatchFile: (id: string) => void;
cache: PluginCache;
resolveId: ResolveIdHook;
Expand Down Expand Up @@ -402,8 +402,6 @@ export interface RollupOptions extends InputOptions {

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

export interface Watcher extends EventEmitter {}

// chokidar watch options
export interface WatchOptions {
persistent?: boolean;
Expand Down Expand Up @@ -440,4 +438,8 @@ export interface RollupWatchOptions extends InputOptions {
watch?: WatcherOptions;
}

export function watch(configs: RollupWatchOptions[]): Watcher;
export interface RollupWatcher extends EventEmitter {
close(): void;
}

export function watch(configs: RollupWatchOptions[]): RollupWatcher;
35 changes: 22 additions & 13 deletions src/watch/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
import { WatchOptions } from 'chokidar';
import { EventEmitter } from 'events';
import path from 'path';
import createFilter from 'rollup-pluginutils/src/createFilter.js';
import rollup, { setWatcher } from '../rollup/index';
import {
InputOptions,
OutputChunk,
OutputOptions,
RollupBuild,
RollupCache,
RollupSingleFileBuild,
RollupWatcher,
RollupWatchOptions
} from '../rollup/types';
import mergeOptions from '../utils/mergeOptions';
import chokidar from './chokidar';
import { addTask, deleteTask } from './fileWatchers';
import { EventEmitter } from 'events';

const DELAY = 200;

export class Watcher extends EventEmitter {
export class Watcher {
emitter: RollupWatcher;
private buildTimeout: NodeJS.Timer;
private running: boolean;
private rerun: boolean = false;
private tasks: Task[];
private succeeded: boolean = false;

constructor(configs: RollupWatchOptions[] = []) {
super();
if (!Array.isArray(configs)) configs = [configs];
this.tasks = configs.map(config => new Task(this, config));
constructor(configs: RollupWatchOptions[]) {
this.emitter = new class extends EventEmitter implements RollupWatcher {
close: () => void;
constructor(close: () => void) {
super();
this.close = close;
}
}(this.close.bind(this));
this.tasks = (Array.isArray(configs) ? configs : configs ? [configs] : []).map(config => new Task(this, config));
this.running = true;
process.nextTick(() => this.run());
}

emit(event: string, value: any) {
this.emitter.emit(event, value);
}

close() {
if (this.buildTimeout) clearTimeout(this.buildTimeout);
this.tasks.forEach(task => {
task.close();
});

this.removeAllListeners();
this.emitter.removeAllListeners();
}

invalidate() {
Expand Down Expand Up @@ -194,7 +203,7 @@ export class Task {
});
}

setWatcher(this.watcher);
setWatcher(this.watcher.emitter);
return rollup(options)
.then(result => {
if (this.closed) return;
Expand All @@ -221,11 +230,11 @@ export class Task {

return Promise.all(
this.outputs.map(output => {
return <Promise<OutputChunk | Record<string, OutputChunk>>>result.write(output);
return result.write(output);
})
).then(() => result);
})
.then((result: RollupSingleFileBuild | RollupBuild) => {
.then((result: RollupBuild) => {
this.watcher.emit('event', {
code: 'BUNDLE_END',
input: this.inputOptions.input,
Expand Down Expand Up @@ -271,5 +280,5 @@ export class Task {
}

export default function watch(configs: RollupWatchOptions[]) {
return new Watcher(configs);
return new Watcher(configs).emitter;
}
6 changes: 0 additions & 6 deletions test/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ describe('hooks', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: '' }),
{
Expand All @@ -280,7 +279,6 @@ describe('hooks', () => {
.rollup({
cache,
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: '' }),
{
Expand All @@ -306,7 +304,6 @@ describe('hooks', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: '' }),
{
Expand All @@ -332,7 +329,6 @@ describe('hooks', () => {
.rollup({
cache,
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: '' }),
{
Expand Down Expand Up @@ -464,7 +460,6 @@ module.exports = input;
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
Expand Down Expand Up @@ -555,7 +550,6 @@ module.exports = input;
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
Expand Down
3 changes: 0 additions & 3 deletions test/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ describe('rollup.watch', () => {
format: 'cjs'
},
watch: { chokidar },
experimentalCodeSplitting: true
});

return sequence(watcher, [
Expand Down Expand Up @@ -204,7 +203,6 @@ describe('rollup.watch', () => {
format: 'cjs'
},
watch: { chokidar },
experimentalCodeSplitting: true
});

return sequence(watcher, [
Expand Down Expand Up @@ -536,7 +534,6 @@ describe('rollup.watch', () => {
file: 'test/_tmp/output/bundle.js',
format: 'cjs'
},
experimentalCodeSplitting: true,
plugins: [{
buildStart () {
try {
Expand Down

0 comments on commit 05ee58b

Please sign in to comment.