watch.js 9.8 KB

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