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