watch.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. @license
  3. Rollup.js v4.24.2
  4. Sun, 27 Oct 2024 15:39:37 GMT - commit 32d0e7dae85121ac0850ec28576a10a6302f84a9
  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('tty');
  18. require('path');
  19. require('node:perf_hooks');
  20. require('node:fs/promises');
  21. require('fs');
  22. require('util');
  23. require('stream');
  24. require('os');
  25. require('./fsevents-importer.js');
  26. require('events');
  27. class FileWatcher {
  28. constructor(task, chokidarOptions) {
  29. this.transformWatchers = new Map();
  30. this.chokidarOptions = chokidarOptions;
  31. this.task = task;
  32. this.watcher = this.createWatcher(null);
  33. }
  34. close() {
  35. this.watcher.close();
  36. for (const watcher of this.transformWatchers.values()) {
  37. watcher.close();
  38. }
  39. }
  40. unwatch(id) {
  41. this.watcher.unwatch(id);
  42. const transformWatcher = this.transformWatchers.get(id);
  43. if (transformWatcher) {
  44. this.transformWatchers.delete(id);
  45. transformWatcher.close();
  46. }
  47. }
  48. watch(id, isTransformDependency) {
  49. if (isTransformDependency) {
  50. const watcher = this.transformWatchers.get(id) ?? this.createWatcher(id);
  51. watcher.add(id);
  52. this.transformWatchers.set(id, watcher);
  53. }
  54. else {
  55. this.watcher.add(id);
  56. }
  57. }
  58. createWatcher(transformWatcherId) {
  59. const task = this.task;
  60. const isLinux = node_os.platform() === 'linux';
  61. const isFreeBSD = node_os.platform() === 'freebsd';
  62. const isTransformDependency = transformWatcherId !== null;
  63. const handleChange = (id, event) => {
  64. const changedId = transformWatcherId || id;
  65. if (isLinux || isFreeBSD) {
  66. // unwatching and watching fixes an issue with chokidar where on certain systems,
  67. // a file that was unlinked and immediately recreated would create a change event
  68. // but then no longer any further events
  69. watcher.unwatch(changedId);
  70. watcher.add(changedId);
  71. }
  72. task.invalidate(changedId, { event, isTransformDependency });
  73. };
  74. const watcher = index.chokidar
  75. .watch([], this.chokidarOptions)
  76. .on('add', id => handleChange(id, 'create'))
  77. .on('change', id => handleChange(id, 'update'))
  78. .on('unlink', id => handleChange(id, 'delete'));
  79. return watcher;
  80. }
  81. }
  82. const eventsRewrites = {
  83. create: {
  84. create: 'buggy',
  85. delete: null, //delete file from map
  86. update: 'create'
  87. },
  88. delete: {
  89. create: 'update',
  90. delete: 'buggy',
  91. update: 'buggy'
  92. },
  93. update: {
  94. create: 'buggy',
  95. delete: 'delete',
  96. update: 'update'
  97. }
  98. };
  99. class Watcher {
  100. constructor(optionsList, emitter) {
  101. this.buildDelay = 0;
  102. this.buildTimeout = null;
  103. this.closed = false;
  104. this.invalidatedIds = new Map();
  105. this.rerun = false;
  106. this.running = true;
  107. this.emitter = emitter;
  108. emitter.close = this.close.bind(this);
  109. this.tasks = optionsList.map(options => new Task(this, options));
  110. for (const { watch } of optionsList) {
  111. if (watch && typeof watch.buildDelay === 'number') {
  112. this.buildDelay = Math.max(this.buildDelay, watch.buildDelay);
  113. }
  114. }
  115. process.nextTick(() => this.run());
  116. }
  117. async close() {
  118. if (this.closed)
  119. return;
  120. this.closed = true;
  121. if (this.buildTimeout)
  122. clearTimeout(this.buildTimeout);
  123. for (const task of this.tasks) {
  124. task.close();
  125. }
  126. await this.emitter.emit('close');
  127. this.emitter.removeAllListeners();
  128. }
  129. invalidate(file) {
  130. if (file) {
  131. const previousEvent = this.invalidatedIds.get(file.id);
  132. const event = previousEvent ? eventsRewrites[previousEvent][file.event] : file.event;
  133. if (event === 'buggy') {
  134. //TODO: throws or warn? Currently just ignore, uses new event
  135. this.invalidatedIds.set(file.id, file.event);
  136. }
  137. else if (event === null) {
  138. this.invalidatedIds.delete(file.id);
  139. }
  140. else {
  141. this.invalidatedIds.set(file.id, event);
  142. }
  143. }
  144. if (this.running) {
  145. this.rerun = true;
  146. return;
  147. }
  148. if (this.buildTimeout)
  149. clearTimeout(this.buildTimeout);
  150. this.buildTimeout = setTimeout(async () => {
  151. this.buildTimeout = null;
  152. try {
  153. await Promise.all([...this.invalidatedIds].map(([id, event]) => this.emitter.emit('change', id, { event })));
  154. this.invalidatedIds.clear();
  155. await this.emitter.emit('restart');
  156. this.emitter.removeListenersForCurrentRun();
  157. this.run();
  158. }
  159. catch (error) {
  160. this.invalidatedIds.clear();
  161. await this.emitter.emit('event', {
  162. code: 'ERROR',
  163. error,
  164. result: null
  165. });
  166. await this.emitter.emit('event', {
  167. code: 'END'
  168. });
  169. }
  170. }, this.buildDelay);
  171. }
  172. async run() {
  173. this.running = true;
  174. await this.emitter.emit('event', {
  175. code: 'START'
  176. });
  177. for (const task of this.tasks) {
  178. await task.run();
  179. }
  180. this.running = false;
  181. await this.emitter.emit('event', {
  182. code: 'END'
  183. });
  184. if (this.rerun) {
  185. this.rerun = false;
  186. this.invalidate();
  187. }
  188. }
  189. }
  190. class Task {
  191. constructor(watcher, options) {
  192. this.cache = { modules: [] };
  193. this.watchFiles = [];
  194. this.closed = false;
  195. this.invalidated = true;
  196. this.watched = new Set();
  197. this.watcher = watcher;
  198. this.options = options;
  199. this.skipWrite = Boolean(options.watch && options.watch.skipWrite);
  200. this.outputs = this.options.output;
  201. this.outputFiles = this.outputs.map(output => {
  202. if (output.file || output.dir)
  203. return path.resolve(output.file || output.dir);
  204. return undefined;
  205. });
  206. const watchOptions = this.options.watch || {};
  207. this.filter = rollup.createFilter(watchOptions.include, watchOptions.exclude);
  208. this.fileWatcher = new FileWatcher(this, {
  209. ...watchOptions.chokidar,
  210. disableGlobbing: true,
  211. ignoreInitial: true
  212. });
  213. }
  214. close() {
  215. this.closed = true;
  216. this.fileWatcher.close();
  217. }
  218. invalidate(id, details) {
  219. this.invalidated = true;
  220. if (details.isTransformDependency) {
  221. for (const module of this.cache.modules) {
  222. if (!module.transformDependencies.includes(id))
  223. continue;
  224. // effective invalidation
  225. module.originalCode = null;
  226. }
  227. }
  228. this.watcher.invalidate({ event: details.event, 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