UNPKG

9.61 kBJavaScriptView Raw
1/*
2 @license
3 Rollup.js v2.69.1
4 Fri, 04 Mar 2022 13:38:38 GMT - commit 994c1eccf4d53e416a010f47e54a2086f1a0f4e9
5
6
7 https://github.com/rollup/rollup
8
9 Released under the MIT License.
10*/
11'use strict';
12
13Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
14
15const require$$0 = require('path');
16const process = require('process');
17const rollup = require('./rollup.js');
18const mergeOptions = require('./mergeOptions.js');
19const require$$2 = require('os');
20const index = require('./index.js');
21require('perf_hooks');
22require('crypto');
23require('fs');
24require('events');
25require('util');
26require('stream');
27
28class FileWatcher {
29 constructor(task, chokidarOptions) {
30 this.transformWatchers = new Map();
31 this.chokidarOptions = chokidarOptions;
32 this.task = task;
33 this.watcher = this.createWatcher(null);
34 }
35 close() {
36 this.watcher.close();
37 for (const watcher of this.transformWatchers.values()) {
38 watcher.close();
39 }
40 }
41 unwatch(id) {
42 this.watcher.unwatch(id);
43 const transformWatcher = this.transformWatchers.get(id);
44 if (transformWatcher) {
45 this.transformWatchers.delete(id);
46 transformWatcher.close();
47 }
48 }
49 watch(id, isTransformDependency) {
50 var _a;
51 if (isTransformDependency) {
52 const watcher = (_a = this.transformWatchers.get(id)) !== null && _a !== void 0 ? _a : this.createWatcher(id);
53 watcher.add(id);
54 this.transformWatchers.set(id, watcher);
55 }
56 else {
57 this.watcher.add(id);
58 }
59 }
60 createWatcher(transformWatcherId) {
61 const task = this.task;
62 const isLinux = require$$2.platform() === 'linux';
63 const isTransformDependency = transformWatcherId !== null;
64 const handleChange = (id, event) => {
65 const changedId = transformWatcherId || id;
66 if (isLinux) {
67 // unwatching and watching fixes an issue with chokidar where on certain systems,
68 // a file that was unlinked and immediately recreated would create a change event
69 // but then no longer any further events
70 watcher.unwatch(changedId);
71 watcher.add(changedId);
72 }
73 task.invalidate(changedId, { event, isTransformDependency });
74 };
75 const watcher = index.chokidar
76 .watch([], this.chokidarOptions)
77 .on('add', id => handleChange(id, 'create'))
78 .on('change', id => handleChange(id, 'update'))
79 .on('unlink', id => handleChange(id, 'delete'));
80 return watcher;
81 }
82}
83
84const eventsRewrites = {
85 create: {
86 create: 'buggy',
87 delete: null,
88 update: 'create'
89 },
90 delete: {
91 create: 'update',
92 delete: 'buggy',
93 update: 'buggy'
94 },
95 update: {
96 create: 'buggy',
97 delete: 'delete',
98 update: 'update'
99 }
100};
101class Watcher {
102 constructor(configs, emitter) {
103 this.buildDelay = 0;
104 this.buildTimeout = null;
105 this.invalidatedIds = new Map();
106 this.rerun = false;
107 this.running = true;
108 this.emitter = emitter;
109 emitter.close = this.close.bind(this);
110 this.tasks = configs.map(config => new Task(this, config));
111 this.buildDelay = configs.reduce((buildDelay, { watch }) => watch && typeof watch.buildDelay === 'number'
112 ? Math.max(buildDelay, watch.buildDelay)
113 : buildDelay, this.buildDelay);
114 process.nextTick(() => this.run());
115 }
116 close() {
117 if (this.buildTimeout)
118 clearTimeout(this.buildTimeout);
119 for (const task of this.tasks) {
120 task.close();
121 }
122 this.emitter.emit('close');
123 this.emitter.removeAllListeners();
124 }
125 invalidate(file) {
126 if (file) {
127 const prevEvent = this.invalidatedIds.get(file.id);
128 const event = prevEvent ? eventsRewrites[prevEvent][file.event] : file.event;
129 if (event === 'buggy') {
130 //TODO: throws or warn? Currently just ignore, uses new event
131 this.invalidatedIds.set(file.id, file.event);
132 }
133 else if (event === null) {
134 this.invalidatedIds.delete(file.id);
135 }
136 else {
137 this.invalidatedIds.set(file.id, event);
138 }
139 }
140 if (this.running) {
141 this.rerun = true;
142 return;
143 }
144 if (this.buildTimeout)
145 clearTimeout(this.buildTimeout);
146 this.buildTimeout = setTimeout(() => {
147 this.buildTimeout = null;
148 for (const [id, event] of this.invalidatedIds) {
149 this.emitter.emit('change', id, { event });
150 }
151 this.invalidatedIds.clear();
152 this.emitter.emit('restart');
153 this.run();
154 }, this.buildDelay);
155 }
156 async run() {
157 this.running = true;
158 this.emitter.emit('event', {
159 code: 'START'
160 });
161 for (const task of this.tasks) {
162 await task.run();
163 }
164 this.running = false;
165 this.emitter.emit('event', {
166 code: 'END'
167 });
168 if (this.rerun) {
169 this.rerun = false;
170 this.invalidate();
171 }
172 }
173}
174class Task {
175 constructor(watcher, config) {
176 this.cache = { modules: [] };
177 this.watchFiles = [];
178 this.closed = false;
179 this.invalidated = true;
180 this.watched = new Set();
181 this.watcher = watcher;
182 this.skipWrite = Boolean(config.watch && config.watch.skipWrite);
183 this.options = mergeOptions.mergeOptions(config);
184 this.outputs = this.options.output;
185 this.outputFiles = this.outputs.map(output => {
186 if (output.file || output.dir)
187 return require$$0.resolve(output.file || output.dir);
188 return undefined;
189 });
190 const watchOptions = this.options.watch || {};
191 this.filter = rollup.createFilter(watchOptions.include, watchOptions.exclude);
192 this.fileWatcher = new FileWatcher(this, {
193 ...watchOptions.chokidar,
194 disableGlobbing: true,
195 ignoreInitial: true
196 });
197 }
198 close() {
199 this.closed = true;
200 this.fileWatcher.close();
201 }
202 invalidate(id, details) {
203 this.invalidated = true;
204 if (details.isTransformDependency) {
205 for (const module of this.cache.modules) {
206 if (!module.transformDependencies.includes(id))
207 continue;
208 // effective invalidation
209 module.originalCode = null;
210 }
211 }
212 this.watcher.invalidate({ event: details.event, id });
213 }
214 async run() {
215 if (!this.invalidated)
216 return;
217 this.invalidated = false;
218 const options = {
219 ...this.options,
220 cache: this.cache
221 };
222 const start = Date.now();
223 this.watcher.emitter.emit('event', {
224 code: 'BUNDLE_START',
225 input: this.options.input,
226 output: this.outputFiles
227 });
228 let result = null;
229 try {
230 result = await rollup.rollupInternal(options, this.watcher.emitter);
231 if (this.closed) {
232 return;
233 }
234 this.updateWatchedFiles(result);
235 this.skipWrite || (await Promise.all(this.outputs.map(output => result.write(output))));
236 this.watcher.emitter.emit('event', {
237 code: 'BUNDLE_END',
238 duration: Date.now() - start,
239 input: this.options.input,
240 output: this.outputFiles,
241 result
242 });
243 }
244 catch (error) {
245 if (!this.closed) {
246 if (Array.isArray(error.watchFiles)) {
247 for (const id of error.watchFiles) {
248 this.watchFile(id);
249 }
250 }
251 if (error.id) {
252 this.cache.modules = this.cache.modules.filter(module => module.id !== error.id);
253 }
254 }
255 this.watcher.emitter.emit('event', {
256 code: 'ERROR',
257 error,
258 result
259 });
260 }
261 }
262 updateWatchedFiles(result) {
263 const previouslyWatched = this.watched;
264 this.watched = new Set();
265 this.watchFiles = result.watchFiles;
266 this.cache = result.cache;
267 for (const id of this.watchFiles) {
268 this.watchFile(id);
269 }
270 for (const module of this.cache.modules) {
271 for (const depId of module.transformDependencies) {
272 this.watchFile(depId, true);
273 }
274 }
275 for (const id of previouslyWatched) {
276 if (!this.watched.has(id)) {
277 this.fileWatcher.unwatch(id);
278 }
279 }
280 }
281 watchFile(id, isTransformDependency = false) {
282 if (!this.filter(id))
283 return;
284 this.watched.add(id);
285 if (this.outputFiles.some(file => file === id)) {
286 throw new Error('Cannot import the generated bundle');
287 }
288 // this is necessary to ensure that any 'renamed' files
289 // continue to be watched following an error
290 this.fileWatcher.watch(id, isTransformDependency);
291 }
292}
293
294exports.Task = Task;
295exports.Watcher = Watcher;
296//# sourceMappingURL=watch.js.map