rollup.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. @license
  3. Rollup.js v4.41.0
  4. Sun, 18 May 2025 05:33:01 GMT - commit 0928185cd544907dab472754634ddf988452aae6
  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('./shared/rollup.js');
  11. const parseAst_js = require('./shared/parseAst.js');
  12. const fseventsImporter = require('./shared/fsevents-importer.js');
  13. require('node:process');
  14. require('node:path');
  15. require('path');
  16. require('./native.js');
  17. require('node:perf_hooks');
  18. require('node:fs/promises');
  19. class WatchEmitter {
  20. constructor() {
  21. this.currentHandlers = Object.create(null);
  22. this.persistentHandlers = Object.create(null);
  23. }
  24. // Will be overwritten by Rollup
  25. async close() { }
  26. emit(event, ...parameters) {
  27. return Promise.all([...this.getCurrentHandlers(event), ...this.getPersistentHandlers(event)].map(handler => handler(...parameters)));
  28. }
  29. off(event, listener) {
  30. const listeners = this.persistentHandlers[event];
  31. if (listeners) {
  32. // A hack stolen from "mitt": ">>> 0" does not change numbers >= 0, but -1
  33. // (which would remove the last array element if used unchanged) is turned
  34. // into max_int, which is outside the array and does not change anything.
  35. listeners.splice(listeners.indexOf(listener) >>> 0, 1);
  36. }
  37. return this;
  38. }
  39. on(event, listener) {
  40. this.getPersistentHandlers(event).push(listener);
  41. return this;
  42. }
  43. onCurrentRun(event, listener) {
  44. this.getCurrentHandlers(event).push(listener);
  45. return this;
  46. }
  47. once(event, listener) {
  48. const selfRemovingListener = (...parameters) => {
  49. this.off(event, selfRemovingListener);
  50. return listener(...parameters);
  51. };
  52. this.on(event, selfRemovingListener);
  53. return this;
  54. }
  55. removeAllListeners() {
  56. this.removeListenersForCurrentRun();
  57. this.persistentHandlers = Object.create(null);
  58. return this;
  59. }
  60. removeListenersForCurrentRun() {
  61. this.currentHandlers = Object.create(null);
  62. return this;
  63. }
  64. getCurrentHandlers(event) {
  65. return this.currentHandlers[event] || (this.currentHandlers[event] = []);
  66. }
  67. getPersistentHandlers(event) {
  68. return this.persistentHandlers[event] || (this.persistentHandlers[event] = []);
  69. }
  70. }
  71. function watch(configs) {
  72. const emitter = new WatchEmitter();
  73. watchInternal(configs, emitter).catch(error => {
  74. rollup.handleError(error);
  75. });
  76. return emitter;
  77. }
  78. function withTrailingSlash(path) {
  79. if (path[path.length - 1] !== '/') {
  80. return `${path}/`;
  81. }
  82. return path;
  83. }
  84. function checkWatchConfig(config) {
  85. for (const item of config) {
  86. if (item.input && item.output) {
  87. const input = typeof item.input === 'string' ? rollup.ensureArray(item.input) : item.input;
  88. const output = rollup.ensureArray(item.output);
  89. for (const index in input) {
  90. const inputPath = input[index];
  91. const subPath = output.find(o => {
  92. if (!o.dir || typeof inputPath !== 'string') {
  93. return false;
  94. }
  95. const _outPath = withTrailingSlash(o.dir);
  96. const _inputPath = withTrailingSlash(inputPath);
  97. return _inputPath.startsWith(_outPath);
  98. });
  99. if (subPath) {
  100. parseAst_js.error(parseAst_js.logInvalidOption('watch', parseAst_js.URL_WATCH, `the input "${inputPath}" is a subpath of the output "${subPath.dir}"`));
  101. }
  102. }
  103. }
  104. }
  105. }
  106. async function watchInternal(configs, emitter) {
  107. const optionsList = await Promise.all(rollup.ensureArray(configs).map(config => rollup.mergeOptions(config, true)));
  108. const watchOptionsList = optionsList.filter(config => config.watch !== false);
  109. if (watchOptionsList.length === 0) {
  110. return parseAst_js.error(parseAst_js.logInvalidOption('watch', parseAst_js.URL_WATCH, 'there must be at least one config where "watch" is not set to "false"'));
  111. }
  112. checkWatchConfig(watchOptionsList);
  113. await fseventsImporter.loadFsEvents();
  114. const { Watcher } = await Promise.resolve().then(() => require('./shared/watch.js'));
  115. new Watcher(watchOptionsList, emitter);
  116. }
  117. exports.VERSION = rollup.version;
  118. exports.defineConfig = rollup.defineConfig;
  119. exports.rollup = rollup.rollup;
  120. exports.watch = watch;
  121. //# sourceMappingURL=rollup.js.map