You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
10 KiB

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