UNPKG

1.26 kBPlain TextView Raw
1import { EventEmitter } from "events";
2import { watch as rollupWatch } from "rollup";
3import { BUILDING, BUILT, ERROR, WRITING, WRITTEN } from "./events";
4import { BuildEventEmitter, BuldFunction } from "./types";
5
6interface WatchEvent {
7 output: any;
8 error: any;
9}
10
11type EmitMethod = (result: EventEmitter, event: WatchEvent) => void;
12
13const ERR: EmitMethod = (result, { error }) => {
14 result.emit(
15 ERROR,
16 error,
17 );
18};
19
20const map: Record<string, EmitMethod> = {
21
22 START(result) {
23 result.emit(BUILDING);
24 },
25
26 END(result) {
27 result.emit(BUILT);
28 },
29
30 BUNDLE_START(result, { output }) {
31 for (const filename of output) {
32 result.emit(
33 WRITING,
34 filename,
35 );
36 }
37 },
38
39 BUNDLE_END(result, { output }) {
40 for (const filename of output) {
41 result.emit(
42 WRITTEN,
43 filename,
44 );
45 }
46 },
47
48 ERROR: ERR,
49 FATAL: ERR,
50
51};
52
53const watch: BuldFunction = (configs) => {
54
55 const watcher = rollupWatch(configs);
56 const result: BuildEventEmitter = new EventEmitter();
57
58 watcher.on("event", (event) => {
59
60 const { code } = event;
61
62 const emit: EmitMethod | undefined = map[code];
63 if (emit) {
64 emit(result, event);
65 }
66
67 });
68
69 return result;
70
71};
72
73export default watch;