source: imaps-frontend/node_modules/rollup/dist/shared/watch.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

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