@rollup/wasm-node 4.0.0-12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,317 @@
1
+ /*
2
+ @license
3
+ Rollup.js v4.0.0-12
4
+ Wed, 23 Aug 2023 14:39:48 GMT - commit b6eec18d711348e3b177ef58dc2836cdf75e0432
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 node_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('tty');
20
+ require('path');
21
+ require('node:perf_hooks');
22
+ require('node:crypto');
23
+ require('../native.js');
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,
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 node_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
+ this.skipWrite || (await Promise.all(this.outputs.map(output => result.write(output))));
257
+ await this.watcher.emitter.emit('event', {
258
+ code: 'BUNDLE_END',
259
+ duration: Date.now() - start,
260
+ input: this.options.input,
261
+ output: this.outputFiles,
262
+ result
263
+ });
264
+ }
265
+ catch (error) {
266
+ if (!this.closed) {
267
+ if (Array.isArray(error.watchFiles)) {
268
+ for (const id of error.watchFiles) {
269
+ this.watchFile(id);
270
+ }
271
+ }
272
+ if (error.id) {
273
+ this.cache.modules = this.cache.modules.filter(module => module.id !== error.id);
274
+ }
275
+ }
276
+ await this.watcher.emitter.emit('event', {
277
+ code: 'ERROR',
278
+ error,
279
+ result
280
+ });
281
+ }
282
+ }
283
+ updateWatchedFiles(result) {
284
+ const previouslyWatched = this.watched;
285
+ this.watched = new Set();
286
+ this.watchFiles = result.watchFiles;
287
+ this.cache = result.cache;
288
+ for (const id of this.watchFiles) {
289
+ this.watchFile(id);
290
+ }
291
+ for (const module of this.cache.modules) {
292
+ for (const depId of module.transformDependencies) {
293
+ this.watchFile(depId, true);
294
+ }
295
+ }
296
+ for (const id of previouslyWatched) {
297
+ if (!this.watched.has(id)) {
298
+ this.fileWatcher.unwatch(id);
299
+ }
300
+ }
301
+ }
302
+ watchFile(id, isTransformDependency = false) {
303
+ if (!this.filter(id))
304
+ return;
305
+ this.watched.add(id);
306
+ if (this.outputFiles.includes(id)) {
307
+ throw new Error('Cannot import the generated bundle');
308
+ }
309
+ // this is necessary to ensure that any 'renamed' files
310
+ // continue to be watched following an error
311
+ this.fileWatcher.watch(id, isTransformDependency);
312
+ }
313
+ }
314
+
315
+ exports.Task = Task;
316
+ exports.Watcher = Watcher;
317
+ //# sourceMappingURL=watch.js.map