watch.js 10.0 KB

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