-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
252 lines (204 loc) · 5.73 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
import path from 'path';
import EventEmitter from 'events';
import createFilter from 'rollup-pluginutils/src/createFilter.js';
import rollup, { InputOptions, OutputOptions, OutputBundle } from '../rollup/index';
import ensureArray from '../utils/ensureArray';
import { mapSequence } from '../utils/promise';
import { addTask, deleteTask } from './fileWatchers';
import chokidar from './chokidar';
import mergeOptions from '../utils/mergeOptions.js';
import { WatchOptions } from 'chokidar';
import { ModuleJSON } from '../Module';
const DELAY = 100;
export interface WatcherOptions {
chokidar?: boolean | WatchOptions;
include?: string[];
exclude?: string[];
clearScreen?: boolean;
}
export type InputAndOutputOptions = InputOptions & OutputOptions;
export interface RollupWatchOptions extends InputAndOutputOptions {
output?: OutputOptions;
watch?: WatcherOptions;
}
export class Watcher extends (<{ new(): any }>EventEmitter) {
dirty: boolean;
running: boolean;
tasks: Task[];
succeeded: boolean;
constructor (configs: (RollupWatchOptions)[]) {
super();
this.dirty = true;
this.running = false;
this.tasks = ensureArray(configs).map(config => new Task(this, config));
this.succeeded = false;
process.nextTick(() => {
this._run();
});
}
close () {
this.tasks.forEach(task => {
task.close();
});
this.removeAllListeners();
}
_makeDirty () {
if (this.dirty) return;
this.dirty = true;
if (!this.running) {
setTimeout(() => {
this._run();
}, DELAY);
}
}
_run () {
this.running = true;
this.dirty = false;
this.emit('event', {
code: 'START'
});
mapSequence(this.tasks, (task: Task) => task.run())
.then(() => {
this.succeeded = true;
this.emit('event', {
code: 'END'
});
})
.catch(error => {
this.emit('event', {
code: this.succeeded ? 'ERROR' : 'FATAL',
error
});
})
.then(() => {
this.running = false;
if (this.dirty) {
this._run();
}
});
}
}
export class Task {
watcher: Watcher;
dirty: boolean;
closed: boolean;
watched: Set<string>;
inputOptions: InputOptions;
cache: OutputBundle;
chokidarOptions: WatchOptions;
chokidarOptionsHash: string;
outputFiles: string[];
outputs: OutputOptions[];
deprecations: { old: string, new: string }[];
filter: (id: string) => boolean;
constructor (watcher: Watcher, config: RollupWatchOptions) {
this.cache = null;
this.watcher = watcher;
this.dirty = true;
this.closed = false;
this.watched = new Set();
const { inputOptions, outputOptions, deprecations } = mergeOptions({ config });
this.inputOptions = inputOptions;
this.outputs = outputOptions;
this.outputFiles = this.outputs.map(output => path.resolve(output.file));
const watchOptions = inputOptions.watch || {};
if ('useChokidar' in watchOptions) watchOptions.chokidar = watchOptions.useChokidar;
let chokidarOptions = 'chokidar' in watchOptions ? watchOptions.chokidar : !!chokidar;
if (chokidarOptions) {
chokidarOptions = Object.assign(
chokidarOptions === true ? {} : chokidarOptions,
{
ignoreInitial: true
}
);
}
if (chokidarOptions && !chokidar) {
throw new Error(
`options.watch.chokidar was provided, but chokidar could not be found. Have you installed it?`
);
}
this.chokidarOptions = chokidarOptions;
this.chokidarOptionsHash = JSON.stringify(chokidarOptions);
this.filter = createFilter(watchOptions.include, watchOptions.exclude);
this.deprecations = [...deprecations, ...(watchOptions._deprecations || [])];
}
close () {
this.closed = true;
this.watched.forEach(id => {
deleteTask(id, this, this.chokidarOptionsHash);
});
}
makeDirty () {
if (!this.dirty) {
this.dirty = true;
this.watcher._makeDirty();
}
}
run () {
if (!this.dirty) return;
this.dirty = false;
const options = Object.assign(this.inputOptions, {
cache: this.cache
});
const start = Date.now();
this.watcher.emit('event', {
code: 'BUNDLE_START',
input: this.inputOptions.input,
output: this.outputFiles
});
if (this.deprecations.length) {
this.inputOptions.onwarn({
code: 'DEPRECATED_OPTIONS',
deprecations: this.deprecations,
message: `The following options have been renamed — please update your config: ${this.deprecations.map(
option => `${option.old} -> ${option.new}`).join(', ')}`,
});
}
return rollup(options)
.then((bundle: OutputBundle) => {
if (this.closed) return;
this.cache = bundle;
const watched = new Set();
bundle.modules.forEach((module: ModuleJSON) => {
watched.add(module.id);
this.watchFile(module.id);
});
this.watched.forEach(id => {
if (!watched.has(id)) deleteTask(id, this, this.chokidarOptionsHash);
});
this.watched = watched;
return Promise.all(this.outputs.map(output => bundle.write(output)));
})
.then(() => {
this.watcher.emit('event', {
code: 'BUNDLE_END',
input: this.inputOptions.input,
output: this.outputFiles,
duration: Date.now() - start
});
})
.catch((error: Error) => {
if (this.closed) return;
if (this.cache) {
this.cache.modules.forEach(module => {
// this is necessary to ensure that any 'renamed' files
// continue to be watched following an error
this.watchFile(module.id);
});
}
throw error;
});
}
watchFile (id: string) {
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);
}
}
export default function watch (configs: RollupWatchOptions[]) {
return new Watcher(configs);
}