/
index.ts
284 lines (246 loc) · 7.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { WatchOptions } from 'chokidar';
import { EventEmitter } from 'events';
import path from 'path';
import createFilter from 'rollup-pluginutils/src/createFilter';
import rollup, { setWatcher } from '../rollup/index';
import {
InputOptions,
ModuleJSON,
OutputOptions,
RollupBuild,
RollupCache,
RollupWatcher,
WatcherOptions
} from '../rollup/types';
import mergeOptions, { GenericConfigObject } from '../utils/mergeOptions';
import chokidar from './chokidar';
import { addTask, deleteTask } from './fileWatchers';
const DELAY = 200;
export class Watcher {
emitter: RollupWatcher;
private buildTimeout: NodeJS.Timer | null = null;
private invalidatedIds: Set<string> = new Set();
private rerun = false;
private running: boolean;
private succeeded = false;
private tasks: Task[];
constructor(configs: GenericConfigObject[] | GenericConfigObject) {
this.emitter = new (class extends EventEmitter implements RollupWatcher {
close: () => void;
constructor(close: () => void) {
super();
this.close = close;
// Allows more than 10 bundles to be watched without
// showing the `MaxListenersExceededWarning` to the user.
this.setMaxListeners(Infinity);
}
})(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());
}
close() {
if (this.buildTimeout) clearTimeout(this.buildTimeout);
this.tasks.forEach(task => {
task.close();
});
this.emitter.removeAllListeners();
}
emit(event: string, value?: any) {
this.emitter.emit(event, value);
}
invalidate(id?: string) {
if (id) {
this.invalidatedIds.add(id);
}
if (this.running) {
this.rerun = true;
return;
}
if (this.buildTimeout) clearTimeout(this.buildTimeout);
this.buildTimeout = setTimeout(() => {
this.buildTimeout = null;
this.invalidatedIds.forEach(id => this.emit('change', id));
this.invalidatedIds.clear();
this.emit('restart');
this.run();
}, DELAY);
}
private run() {
this.running = true;
this.emit('event', {
code: 'START'
});
let taskPromise = Promise.resolve();
for (const task of this.tasks) taskPromise = taskPromise.then(() => task.run());
return taskPromise
.then(() => {
this.succeeded = true;
this.running = false;
this.emit('event', {
code: 'END'
});
})
.catch(error => {
this.running = false;
this.emit('event', {
code: this.succeeded ? 'ERROR' : 'FATAL',
error
});
})
.then(() => {
if (this.rerun) {
this.rerun = false;
this.invalidate();
}
});
}
}
export class Task {
cache: RollupCache;
watchFiles: string[] = [];
private chokidarOptions: WatchOptions;
private chokidarOptionsHash: string;
private closed: boolean;
private filter: (id: string) => boolean;
private inputOptions: InputOptions;
private invalidated = true;
private outputFiles: string[];
private outputs: OutputOptions[];
private watched: Set<string>;
private watcher: Watcher;
constructor(watcher: Watcher, config: GenericConfigObject) {
this.cache = null as any;
this.watcher = watcher;
this.closed = false;
this.watched = new Set();
const { inputOptions, outputOptions } = mergeOptions({
config
});
this.inputOptions = inputOptions;
this.outputs = outputOptions;
this.outputFiles = this.outputs.map(output => {
if (output.file || output.dir) return path.resolve(output.file || (output.dir as string));
return undefined as any;
});
const watchOptions: WatcherOptions = inputOptions.watch || {};
if ('useChokidar' in watchOptions)
(watchOptions as any).chokidar = (watchOptions as any).useChokidar;
let chokidarOptions = 'chokidar' in watchOptions ? watchOptions.chokidar : !!chokidar;
if (chokidarOptions) {
chokidarOptions = {
...(chokidarOptions === true ? {} : chokidarOptions),
disableGlobbing: true,
ignoreInitial: true
};
}
if (chokidarOptions && !chokidar) {
throw new Error(
`watch.chokidar was provided, but chokidar could not be found. Have you installed it?`
);
}
this.chokidarOptions = chokidarOptions as WatchOptions;
this.chokidarOptionsHash = JSON.stringify(chokidarOptions);
this.filter = createFilter(watchOptions.include, watchOptions.exclude);
}
close() {
this.closed = true;
this.watched.forEach(id => {
deleteTask(id, this, this.chokidarOptionsHash);
});
}
invalidate(id: string, isTransformDependency: boolean) {
this.invalidated = true;
if (isTransformDependency) {
(this.cache.modules as ModuleJSON[]).forEach(module => {
if (!module.transformDependencies || module.transformDependencies.indexOf(id) === -1)
return;
// effective invalidation
module.originalCode = null as any;
});
}
this.watcher.invalidate(id);
}
run() {
if (!this.invalidated) return;
this.invalidated = false;
const options = {
...this.inputOptions,
cache: this.cache
};
const start = Date.now();
this.watcher.emit('event', {
code: 'BUNDLE_START',
input: this.inputOptions.input,
output: this.outputFiles
});
setWatcher(this.watcher.emitter);
return rollup(options)
.then(result => {
if (this.closed) return undefined as any;
const previouslyWatched = this.watched;
const watched = (this.watched = new Set());
this.cache = result.cache;
this.watchFiles = result.watchFiles;
for (const module of this.cache.modules as ModuleJSON[]) {
if (module.transformDependencies) {
module.transformDependencies.forEach(depId => {
watched.add(depId);
this.watchFile(depId, true);
});
}
}
for (const id of this.watchFiles) {
watched.add(id);
this.watchFile(id);
}
for (const id of previouslyWatched) {
if (!watched.has(id)) deleteTask(id, this, this.chokidarOptionsHash);
}
return Promise.all(this.outputs.map(output => result.write(output))).then(() => result);
})
.then((result: RollupBuild) => {
this.watcher.emit('event', {
code: 'BUNDLE_END',
duration: Date.now() - start,
input: this.inputOptions.input,
output: this.outputFiles,
result
});
})
.catch((error: Error) => {
if (this.closed) return;
if (this.cache) {
// this is necessary to ensure that any 'renamed' files
// continue to be watched following an error
if (this.cache.modules) {
this.cache.modules.forEach(module => {
if (module.transformDependencies) {
module.transformDependencies.forEach(depId => {
this.watchFile(depId, true);
});
}
});
}
this.watchFiles.forEach(id => {
this.watchFile(id);
});
}
throw error;
});
}
watchFile(id: string, isTransformDependency = false) {
if (!this.filter(id)) return;
if (this.outputFiles.some(file => file === id)) {
throw new Error('Cannot import the generated bundle');
}
// this is necessary to ensure that any 'renamed' files
// continue to be watched following an error
addTask(id, this, this.chokidarOptions, this.chokidarOptionsHash, isTransformDependency);
}
}
export default function watch(configs: GenericConfigObject[] | GenericConfigObject): RollupWatcher {
return new Watcher(configs).emitter;
}