diff --git a/types/rollup/index.d.ts b/types/rollup/index.d.ts index 946b4f6786..921dca7d21 100644 --- a/types/rollup/index.d.ts +++ b/types/rollup/index.d.ts @@ -164,3 +164,54 @@ export interface TransformContext { /** Returns a Promise that resolves with a bundle */ export function rollup(options: Options): Promise + +export interface WatchOptions extends ConfigFileOptions { + watch?: { + /** + * If set to true, will use chokidar for file watching (requires installation). + * If set to object, the settings are passed on to chokidar + */ + chokidar?: boolean | { [key: string]: any }; + /** Limit the file-watching to certain files, e.g. 'src/**' */ + include?: string; + /** Prevent files from being watched, e.g. 'node_modules/**' */ + exclude?: string; + } +} + +export interface StartEvent { + code: 'START' +} +export interface EndEvent { + code: 'END' +} +export interface ErrorEvent { + code: 'ERROR' + error: Error +} +export interface FatalEvent { + code: 'FATAL' + error: Error +} +export interface BundleStartEvent { + code: 'BUNDLE_START' + input: string + output: string[] +} +export interface BundleEndEvent { + code: 'BUNDLE_END' + input: string + output: string[] + duration: number +} +export type WatchEvent = StartEvent | EndEvent | ErrorEvent | FatalEvent | BundleStartEvent | BundleEndEvent + +export class Watcher { + /** Listen to the events that are emitted by rollup during watching */ + on(type: 'event', callback: (e: WatchEvent) => void): void + /** Stop watching for file changes */ + close(): void +} + +/** Starts rollup in watch mode. Returns a watcher instance for closing or listening to events */ +export function watch(options: WatchOptions): Watcher diff --git a/types/rollup/rollup-tests.ts b/types/rollup/rollup-tests.ts index 924e675373..7fa0c32330 100644 --- a/types/rollup/rollup-tests.ts +++ b/types/rollup/rollup-tests.ts @@ -1,4 +1,4 @@ -import { rollup, Bundle, Plugin, ConfigFileOptions } from 'rollup' +import { rollup, watch, Bundle, Plugin, ConfigFileOptions } from 'rollup' declare const console: { log(...messages: any[]): void, warn(message: string): void } @@ -69,6 +69,42 @@ async function main() { sourcemapFile: 'bundle.js.map', strict: true, }) + + const watcher = watch({ + input: 'main.js', + output: { + file: 'bundle.js', + format: 'es' + }, + watch: { + chokidar: true + } + }) + + watcher.on('event', e => { + switch (e.code) { + case 'START': + console.log(`We're rolling!`) + return + case 'END': + console.log(`We're all rolled up!`) + return + case 'ERROR': + case 'FATAL': + console.log(e.error.message) + return + case 'BUNDLE_START': + console.log(`Bundling: ${e.input}`) + return + case 'BUNDLE_END': + console.log(`Bundling: ${e.input}`) + return + } + + assertNever(e) + }) + + watcher.close() } main() @@ -94,3 +130,7 @@ export const multiConfig: ConfigFileOptions = { } ] } + +function assertNever(nope: never) { + throw new Error('Oh no!') +}