Hook.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const deprecateContext = util.deprecate(
  8. () => {},
  9. "Hook.context is deprecated and will be removed"
  10. );
  11. function CALL_DELEGATE(...args) {
  12. this.call = this._createCall("sync");
  13. return this.call(...args);
  14. }
  15. function CALL_ASYNC_DELEGATE(...args) {
  16. this.callAsync = this._createCall("async");
  17. return this.callAsync(...args);
  18. }
  19. function PROMISE_DELEGATE(...args) {
  20. this.promise = this._createCall("promise");
  21. return this.promise(...args);
  22. }
  23. class Hook {
  24. constructor(args = [], name = undefined) {
  25. this._args = args;
  26. this.name = name;
  27. this.taps = [];
  28. this.interceptors = [];
  29. this._call = CALL_DELEGATE;
  30. this.call = CALL_DELEGATE;
  31. this._callAsync = CALL_ASYNC_DELEGATE;
  32. this.callAsync = CALL_ASYNC_DELEGATE;
  33. this._promise = PROMISE_DELEGATE;
  34. this.promise = PROMISE_DELEGATE;
  35. this._x = undefined;
  36. // eslint-disable-next-line no-self-assign
  37. this.compile = this.compile;
  38. // eslint-disable-next-line no-self-assign
  39. this.tap = this.tap;
  40. // eslint-disable-next-line no-self-assign
  41. this.tapAsync = this.tapAsync;
  42. // eslint-disable-next-line no-self-assign
  43. this.tapPromise = this.tapPromise;
  44. }
  45. compile(_options) {
  46. throw new Error("Abstract: should be overridden");
  47. }
  48. _createCall(type) {
  49. return this.compile({
  50. taps: this.taps,
  51. interceptors: this.interceptors,
  52. args: this._args,
  53. type
  54. });
  55. }
  56. _tap(type, options, fn) {
  57. if (typeof options === "string") {
  58. // Fast path: a string options ("name") is by far the most common
  59. // case. Build the final descriptor in a single allocation instead
  60. // of creating `{ name }` and then `Object.assign`ing it.
  61. const name = options.trim();
  62. if (name === "") {
  63. throw new Error("Missing name for tap");
  64. }
  65. options = { type, fn, name };
  66. } else {
  67. if (typeof options !== "object" || options === null) {
  68. throw new Error("Invalid tap options");
  69. }
  70. let { name } = options;
  71. if (typeof name === "string") {
  72. name = name.trim();
  73. }
  74. if (typeof name !== "string" || name === "") {
  75. throw new Error("Missing name for tap");
  76. }
  77. if (typeof options.context !== "undefined") {
  78. deprecateContext();
  79. }
  80. // Fast path: only `name` is set. Build the descriptor as a literal
  81. // so `_insert` and downstream consumers see the same hidden class
  82. // as the string-options path, avoiding a polymorphic call site.
  83. // Scan with `for...in` (cheaper than allocating `Object.keys`)
  84. // to verify no other user-provided properties exist - e.g.
  85. // webpack's `additionalAssets` - otherwise they'd be dropped.
  86. let onlyName = true;
  87. for (const key in options) {
  88. if (key !== "name") {
  89. onlyName = false;
  90. break;
  91. }
  92. }
  93. if (onlyName) {
  94. options = { type, fn, name };
  95. } else {
  96. options.name = name;
  97. // Preserve previous precedence: user-provided keys win over the internal `type`/`fn`.
  98. options = Object.assign({ type, fn }, options);
  99. }
  100. }
  101. options = this._runRegisterInterceptors(options);
  102. this._insert(options);
  103. }
  104. tap(options, fn) {
  105. this._tap("sync", options, fn);
  106. }
  107. tapAsync(options, fn) {
  108. this._tap("async", options, fn);
  109. }
  110. tapPromise(options, fn) {
  111. this._tap("promise", options, fn);
  112. }
  113. _runRegisterInterceptors(options) {
  114. const { interceptors } = this;
  115. const { length } = interceptors;
  116. // Common case: no interceptors.
  117. if (length === 0) return options;
  118. for (let i = 0; i < length; i++) {
  119. const interceptor = interceptors[i];
  120. if (interceptor.register) {
  121. const newOptions = interceptor.register(options);
  122. if (newOptions !== undefined) {
  123. options = newOptions;
  124. }
  125. }
  126. }
  127. return options;
  128. }
  129. withOptions(options) {
  130. const mergeOptions = (opt) =>
  131. Object.assign({}, options, typeof opt === "string" ? { name: opt } : opt);
  132. return {
  133. name: this.name,
  134. tap: (opt, fn) => this.tap(mergeOptions(opt), fn),
  135. tapAsync: (opt, fn) => this.tapAsync(mergeOptions(opt), fn),
  136. tapPromise: (opt, fn) => this.tapPromise(mergeOptions(opt), fn),
  137. intercept: (interceptor) => this.intercept(interceptor),
  138. isUsed: () => this.isUsed(),
  139. withOptions: (opt) => this.withOptions(mergeOptions(opt))
  140. };
  141. }
  142. isUsed() {
  143. return this.taps.length > 0 || this.interceptors.length > 0;
  144. }
  145. intercept(interceptor) {
  146. this._resetCompilation();
  147. this.interceptors.push(Object.assign({}, interceptor));
  148. if (interceptor.register) {
  149. for (let i = 0; i < this.taps.length; i++) {
  150. this.taps[i] = interceptor.register(this.taps[i]);
  151. }
  152. }
  153. }
  154. _resetCompilation() {
  155. this.call = this._call;
  156. this.callAsync = this._callAsync;
  157. this.promise = this._promise;
  158. }
  159. _insert(item) {
  160. this._resetCompilation();
  161. const { taps } = this;
  162. const stage = typeof item.stage === "number" ? item.stage : 0;
  163. // Fast path: the overwhelmingly common `hook.tap("name", fn)` case
  164. // has no `before` and default stage 0. If the list is empty or the
  165. // last tap's stage is <= the new item's stage the item belongs at
  166. // the end - append in O(1), skipping the Set allocation and the
  167. // shift loop.
  168. if (!(typeof item.before === "string" || Array.isArray(item.before))) {
  169. const n = taps.length;
  170. if (n === 0 || (taps[n - 1].stage || 0) <= stage) {
  171. taps[n] = item;
  172. return;
  173. }
  174. }
  175. let before;
  176. if (typeof item.before === "string") {
  177. before = new Set([item.before]);
  178. } else if (Array.isArray(item.before)) {
  179. before = new Set(item.before);
  180. }
  181. let i = taps.length;
  182. while (i > 0) {
  183. i--;
  184. const tap = taps[i];
  185. taps[i + 1] = tap;
  186. const xStage = tap.stage || 0;
  187. if (before) {
  188. if (before.has(tap.name)) {
  189. before.delete(tap.name);
  190. continue;
  191. }
  192. if (before.size > 0) {
  193. continue;
  194. }
  195. }
  196. if (xStage > stage) {
  197. continue;
  198. }
  199. i++;
  200. break;
  201. }
  202. taps[i] = item;
  203. }
  204. }
  205. Object.setPrototypeOf(Hook.prototype, null);
  206. module.exports = Hook;