Merge pull request #21608 from csvn/rollup-watch

rollup: added typings for watch api
This commit is contained in:
Benjamin Lichtman
2017-11-21 10:20:50 -08:00
committed by GitHub
2 changed files with 92 additions and 1 deletions
+51
View File
@@ -164,3 +164,54 @@ export interface TransformContext {
/** Returns a Promise that resolves with a bundle */
export function rollup(options: Options): Promise<Bundle>
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
+41 -1
View File
@@ -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!')
}