From 25691c6712804fc7fcca4df7040c24025a6b66a0 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Thu, 22 Aug 2019 19:06:43 +0800 Subject: [PATCH 1/4] Adds type definitions for RollupWatcher events. The type information is extracted from https://github.com/rollup/rollup/blob/master/src/watch/index.ts, someone should double check that I got it all right. I'm new to rollup and have never actually used any of this stuff, but when I tried to I ran into the problem of there being no useful type information which caused me to have to read source code. Rather than having everyone suffer like I did, I decided to create a PR to resolve the issue. --- src/rollup/types.d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index f24e305b4d7..af6a4654bb2 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -584,7 +584,16 @@ export interface RollupWatchOptions extends InputOptions { watch?: WatcherOptions; } +type RollupWatcherEvent = + {code:'START'} + | {code: 'BUNDLE_START', input: InputOption, output: string | undefined} + | {code: 'BUNDLE_END', duration: number, input: InputOption, output: string | undefined, result: RollupBuild} + | {code:'END'} + | {code:'ERROR', error:Error} + | {code:'FATAL', error:Error} + export interface RollupWatcher extends EventEmitter { + on(event: 'event', listener: (event: RollupWatcherEvent) => void): void; close(): void; } From ce05443aa043b055fa14aff443dfa8634da54e36 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Fri, 23 Aug 2019 01:40:25 +0800 Subject: [PATCH 2/4] Fixes RollupWatcher.on type signature. I didn't realize that `on` was chainable, this should resolve the issue. --- src/rollup/types.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index af6a4654bb2..2fcbc2ec5e1 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -593,7 +593,7 @@ type RollupWatcherEvent = | {code:'FATAL', error:Error} export interface RollupWatcher extends EventEmitter { - on(event: 'event', listener: (event: RollupWatcherEvent) => void): void; + on(event: 'event', listener: (event: RollupWatcherEvent) => void): this; close(): void; } From 81ebca537d4ebb7af30ed4058e596c10e8717580 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Tue, 24 Dec 2019 13:13:50 -0800 Subject: [PATCH 3/4] Utilize new watcher type --- cli/run/watch.ts | 31 ++++++++----------------- src/rollup/types.d.ts | 48 +++++++++++++++++++++++++++++++-------- src/utils/pluginDriver.ts | 2 +- src/watch/index.ts | 6 ++--- 4 files changed, 51 insertions(+), 36 deletions(-) diff --git a/cli/run/watch.ts b/cli/run/watch.ts index 74297a9f8c6..2be59656b19 100644 --- a/cli/run/watch.ts +++ b/cli/run/watch.ts @@ -5,13 +5,9 @@ import onExit from 'signal-exit'; import tc from 'turbocolor'; import * as rollup from '../../src/node-entry'; import { - InputOption, - RollupBuild, - RollupError, RollupWatcher, RollupWatchOptions, - WarningHandler, - WatcherOptions + WarningHandler } from '../../src/rollup/types'; import mergeOptions, { GenericConfigObject } from '../../src/utils/mergeOptions'; import relativeId from '../../src/utils/relativeId'; @@ -21,15 +17,6 @@ import loadConfigFile from './loadConfigFile'; import { getResetScreen } from './resetScreen'; import { printTimings } from './timings'; -interface WatchEvent { - code?: string; - duration?: number; - error?: RollupError | Error; - input?: InputOption; - output?: string[]; - result?: RollupBuild; -} - export default function watch( configFile: string, configs: GenericConfigObject[], @@ -40,7 +27,7 @@ export default function watch( const warnings = batchWarnings(); const initialConfigs = processConfigs(configs); const clearScreen = initialConfigs.every( - config => (config.watch as WatcherOptions).clearScreen !== false + config => config.watch!.clearScreen !== false ); const resetScreen = getResetScreen(isTTY && clearScreen); @@ -75,16 +62,16 @@ export default function watch( function start(configs: RollupWatchOptions[]) { watcher = rollup.watch(configs as any); - watcher.on('event', (event: WatchEvent) => { + watcher.on('event', event => { switch (event.code) { case 'FATAL': - handleError(event.error as RollupError, true); + handleError(event.error, true); process.exit(1); break; case 'ERROR': warnings.flush(); - handleError(event.error as RollupError, true); + handleError(event.error, true); break; case 'START': @@ -106,7 +93,7 @@ export default function watch( stderr( tc.cyan( `bundles ${tc.bold(input)} → ${tc.bold( - (event.output as string[]).map(relativeId).join(', ') + event.output.map(relativeId).join(', ') )}...` ) ); @@ -119,8 +106,8 @@ export default function watch( stderr( tc.green( `created ${tc.bold( - (event.output as string[]).map(relativeId).join(', ') - )} in ${tc.bold(ms(event.duration as number))}` + event.output.map(relativeId).join(', ') + )} in ${tc.bold(ms(event.duration))}` ) ); if (event.result && event.result.getTimings) { @@ -204,6 +191,6 @@ export default function watch( configWatcher = fs.watch(configFile, (event: string) => { if (event === 'change') restart(); - }); + }) as RollupWatcher; } } diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 2fcbc2ec5e1..0b5fb6b8687 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -584,16 +584,44 @@ export interface RollupWatchOptions extends InputOptions { watch?: WatcherOptions; } -type RollupWatcherEvent = - {code:'START'} - | {code: 'BUNDLE_START', input: InputOption, output: string | undefined} - | {code: 'BUNDLE_END', duration: number, input: InputOption, output: string | undefined, result: RollupBuild} - | {code:'END'} - | {code:'ERROR', error:Error} - | {code:'FATAL', error:Error} - -export interface RollupWatcher extends EventEmitter { - on(event: 'event', listener: (event: RollupWatcherEvent) => void): this; +interface TypedEventEmitter { + addListener(event: K, listener: T[K]): this; + emit(event: K, ...args: Parameters): boolean; + eventNames(): Array; + getMaxListeners(): number; + listenerCount(type: keyof T): number; + listeners(event: K): Array; + off(event: K, listener: T[K]): this; + on(event: K, listener: T[K]): this; + once(event: K, listener: T[K]): this; + prependListener(event: K, listener: T[K]): this; + prependOnceListener(event: K, listener: T[K]): this; + rawListeners(event: K): Array; + removeAllListeners(event?: K): this; + removeListener(event: K, listener: T[K]): this; + setMaxListeners(n: number): this; +} + +export type RollupWatcherEvent = + | { code: 'START' } + | { code: 'BUNDLE_START'; input: InputOption; output: readonly string[] } + | { + code: 'BUNDLE_END'; + duration: number; + input: InputOption; + output: readonly string[]; + result: RollupBuild; + } + | { code: 'END' } + | { code: 'ERROR'; error: RollupError } + | { code: 'FATAL'; error: RollupError }; + +export interface RollupWatcher + extends TypedEventEmitter<{ + change: (id: string) => void; + event: (event: RollupWatcherEvent) => void; + restart: () => void; + }> { close(): void; } diff --git a/src/utils/pluginDriver.ts b/src/utils/pluginDriver.ts index 3446b8622b8..0af6d7a0ed9 100644 --- a/src/utils/pluginDriver.ts +++ b/src/utils/pluginDriver.ts @@ -302,7 +302,7 @@ export function createPluginDriver( }); deprecationWarningShown = true; } - return (watcher as RollupWatcher).on(event, handler); + return watcher!.on(event as any, handler); } return { diff --git a/src/watch/index.ts b/src/watch/index.ts index 5bd0284ee8e..bed7b345fe1 100644 --- a/src/watch/index.ts +++ b/src/watch/index.ts @@ -29,7 +29,7 @@ export class Watcher { private tasks: Task[]; constructor(configs: GenericConfigObject[] | GenericConfigObject) { - this.emitter = new (class extends EventEmitter implements RollupWatcher { + this.emitter = new (class extends EventEmitter { close: () => void; constructor(close: () => void) { super(); @@ -38,7 +38,7 @@ export class Watcher { // showing the `MaxListenersExceededWarning` to the user. this.setMaxListeners(Infinity); } - })(this.close.bind(this)); + })(this.close.bind(this)) as RollupWatcher; this.tasks = (Array.isArray(configs) ? configs : configs ? [configs] : []).map( config => new Task(this, config) ); @@ -56,7 +56,7 @@ export class Watcher { } emit(event: string, value?: any) { - this.emitter.emit(event, value); + this.emitter.emit(event as any, value); } invalidate(id?: string) { From 04b56a88f3b80df27e9c9a40a4ba8aef8fba943c Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Sun, 5 Jan 2020 09:20:23 -0800 Subject: [PATCH 4/4] Remove FATAL references --- cli/run/watch.ts | 5 ----- docs/02-javascript-api.md | 5 ++--- src/rollup/types.d.ts | 3 +-- test/hooks/index.js | 7 +++++-- test/watch/index.js | 3 --- 5 files changed, 8 insertions(+), 15 deletions(-) diff --git a/cli/run/watch.ts b/cli/run/watch.ts index 2be59656b19..e437c71abc9 100644 --- a/cli/run/watch.ts +++ b/cli/run/watch.ts @@ -64,11 +64,6 @@ export default function watch( watcher.on('event', event => { switch (event.code) { - case 'FATAL': - handleError(event.error, true); - process.exit(1); - break; - case 'ERROR': warnings.flush(); handleError(event.error, true); diff --git a/docs/02-javascript-api.md b/docs/02-javascript-api.md index 992802dbe16..ffa16222774 100755 --- a/docs/02-javascript-api.md +++ b/docs/02-javascript-api.md @@ -117,7 +117,7 @@ const outputOptions = { globals, name, plugins, - + // advanced output options assetFileNames, banner, @@ -135,7 +135,7 @@ const outputOptions = { sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, - + // danger zone amd, dynamicImportFunction, @@ -167,7 +167,6 @@ watcher.on('event', event => { // BUNDLE_END — finished building a bundle // END — finished building all bundles // ERROR — encountered an error while bundling - // FATAL — encountered an unrecoverable error }); // stop watching diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index a5638025d9f..cc64901301e 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -643,8 +643,7 @@ export type RollupWatcherEvent = result: RollupBuild; } | { code: 'END' } - | { code: 'ERROR'; error: RollupError } - | { code: 'FATAL'; error: RollupError }; + | { code: 'ERROR'; error: RollupError }; export interface RollupWatcher extends TypedEventEmitter<{ diff --git a/test/hooks/index.js b/test/hooks/index.js index c8e083a3b2a..719b6a128ed 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -914,7 +914,7 @@ describe('hooks', () => { return new Promise((resolve, reject) => { watcher.on('event', event => { if (event.code === 'BUNDLE_END') resolve(); - else if (event.code === 'ERROR' || event.code === 'FATAL') reject(event.error); + else if (event.code === 'ERROR') reject(event.error); }); }).catch(err => { assert.strictEqual( @@ -1192,7 +1192,10 @@ describe('hooks', () => { }) ) .then(() => { - assert.deepEqual(result, [{ a: file, format: 'cjs' }, { b: file, format: 'cjs' }]); + assert.deepEqual(result, [ + { a: file, format: 'cjs' }, + { b: file, format: 'cjs' } + ]); return sander.rimraf(TEMP_DIR); }); }); diff --git a/test/watch/index.js b/test/watch/index.js index a1042679488..f454536bdd9 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -34,9 +34,6 @@ describe('rollup.watch', () => { } else if (typeof next === 'string') { watcher.once('event', event => { if (event.code !== next) { - if (event.code === 'FATAL') { - console.error(event.error); - } watcher.close(); if (event.code === 'ERROR') console.log(event.error); reject(new Error(`Expected ${next} event, got ${event.code}`));