index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. let idInc = 0;
  2. const genWrappedFunc = ({
  3. func,
  4. smp,
  5. context,
  6. timeEventName,
  7. pluginName,
  8. endType,
  9. }) => (...args) => {
  10. const id = idInc++;
  11. // we don't know if there's going to be a callback applied to a particular
  12. // call, so we just set it multiple times, letting each one override the last
  13. const addEndEvent = () =>
  14. smp.addTimeEvent("plugins", timeEventName, "end", {
  15. id,
  16. // we need to allow failure, since webpack can finish compilation and
  17. // cause our callbacks to fall on deaf ears
  18. allowFailure: true,
  19. });
  20. smp.addTimeEvent("plugins", timeEventName, "start", {
  21. id,
  22. name: pluginName,
  23. });
  24. // invoke an end event immediately in case the callback here causes webpack
  25. // to complete compilation. If this gets invoked and not the subsequent
  26. // call, then our data will be inaccurate, sadly
  27. addEndEvent();
  28. const normalArgMap = a => wrap(a, pluginName, smp);
  29. let ret;
  30. if (endType === "wrapDone")
  31. ret = func.apply(
  32. context,
  33. args.map(a => wrap(a, pluginName, smp, addEndEvent))
  34. );
  35. else if (endType === "async") {
  36. const argsButLast = args.slice(0, args.length - 1);
  37. const callback = args[args.length - 1];
  38. ret = func.apply(
  39. context,
  40. argsButLast.map(normalArgMap).concat((...callbackArgs) => {
  41. addEndEvent();
  42. callback(...callbackArgs);
  43. })
  44. );
  45. } else if (endType === "promise")
  46. ret = func.apply(context, args.map(normalArgMap)).then(promiseArg => {
  47. addEndEvent();
  48. return promiseArg;
  49. });
  50. else ret = func.apply(context, args.map(normalArgMap));
  51. addEndEvent();
  52. return ret;
  53. };
  54. const genPluginMethod = (orig, pluginName, smp, type) =>
  55. function(method, func) {
  56. const timeEventName = pluginName + "/" + type + "/" + method;
  57. const wrappedFunc = genWrappedFunc({
  58. func,
  59. smp,
  60. context: this,
  61. timeEventName,
  62. pluginName,
  63. endType: "wrapDone",
  64. });
  65. return orig.plugin(method, wrappedFunc);
  66. };
  67. const wrapTap = (tap, pluginName, smp, type, method) =>
  68. function(id, func) {
  69. const timeEventName = pluginName + "/" + type + "/" + method;
  70. const wrappedFunc = genWrappedFunc({
  71. func,
  72. smp,
  73. context: this,
  74. timeEventName,
  75. pluginName,
  76. });
  77. return tap.call(this, id, wrappedFunc);
  78. };
  79. const wrapTapAsync = (tapAsync, pluginName, smp, type, method) =>
  80. function(id, func) {
  81. const timeEventName = pluginName + "/" + type + "/" + method;
  82. const wrappedFunc = genWrappedFunc({
  83. func,
  84. smp,
  85. context: this,
  86. timeEventName,
  87. pluginName,
  88. endType: "async",
  89. });
  90. return tapAsync.call(this, id, wrappedFunc);
  91. };
  92. const wrapTapPromise = (tapPromise, pluginName, smp, type, method) =>
  93. function(id, func) {
  94. const timeEventName = pluginName + "/" + type + "/" + method;
  95. const wrappedFunc = genWrappedFunc({
  96. func,
  97. smp,
  98. context: this,
  99. timeEventName,
  100. pluginName,
  101. endType: "promise",
  102. });
  103. return tapPromise.call(this, id, wrappedFunc);
  104. };
  105. const wrappedHooks = [];
  106. const wrapHooks = (orig, pluginName, smp, type) => {
  107. const hooks = orig.hooks;
  108. if (!hooks) return hooks;
  109. const prevWrapped = wrappedHooks.find(
  110. w =>
  111. w.pluginName === pluginName && (w.orig === hooks || w.wrapped === hooks)
  112. );
  113. if (prevWrapped) return prevWrapped.wrapped;
  114. const genProxy = method => {
  115. const proxy = new Proxy(hooks[method], {
  116. get: (target, property) => {
  117. const raw = Reflect.get(target, property);
  118. if (property === "tap" && typeof raw === "function")
  119. return wrapTap(raw, pluginName, smp, type, method).bind(proxy);
  120. if (property === "tapAsync" && typeof raw === "function")
  121. return wrapTapAsync(raw, pluginName, smp, type, method).bind(proxy);
  122. if (property === "tapPromise" && typeof raw === "function")
  123. return wrapTapPromise(raw, pluginName, smp, type, method).bind(proxy);
  124. return raw;
  125. },
  126. set: (target, property, value) => {
  127. return Reflect.set(target, property, value);
  128. },
  129. deleteProperty: (target, property) => {
  130. return Reflect.deleteProperty(target, property);
  131. },
  132. });
  133. return proxy;
  134. };
  135. const wrapped = Object.keys(hooks).reduce((acc, method) => {
  136. acc[method] = genProxy(method);
  137. return acc;
  138. }, {});
  139. wrappedHooks.push({ orig: hooks, wrapped, pluginName });
  140. return wrapped;
  141. };
  142. const construcNamesToWrap = [
  143. "Compiler",
  144. "Compilation",
  145. "MainTemplate",
  146. "Parser",
  147. "NormalModuleFactory",
  148. "ContextModuleFactory",
  149. ];
  150. const wrappedObjs = [];
  151. const wrap = (orig, pluginName, smp, addEndEvent) => {
  152. if (!orig) return orig;
  153. const prevWrapped = wrappedObjs.find(
  154. w => w.pluginName === pluginName && (w.orig === orig || w.wrapped === orig)
  155. );
  156. if (prevWrapped) return prevWrapped.wrapped;
  157. const getOrigConstrucName = target =>
  158. target && target.constructor && target.constructor.name;
  159. const getShouldWrap = target => {
  160. const origConstrucName = getOrigConstrucName(target);
  161. return construcNamesToWrap.includes(origConstrucName);
  162. };
  163. const shouldWrap = getShouldWrap(orig);
  164. const shouldSoftWrap = Object.keys(orig)
  165. .map(k => orig[k])
  166. .some(getShouldWrap);
  167. let wrappedReturn;
  168. if (!shouldWrap && !shouldSoftWrap) {
  169. const vanillaFunc = orig.name === "next";
  170. wrappedReturn =
  171. vanillaFunc && addEndEvent
  172. ? function() {
  173. // do this before calling the callback, since the callback can start
  174. // the next plugin step
  175. addEndEvent();
  176. return orig.apply(this, arguments);
  177. }
  178. : orig;
  179. } else {
  180. const proxy = new Proxy(orig, {
  181. get: (target, property) => {
  182. const raw = Reflect.get(target, property);
  183. if (shouldWrap && property === "plugin")
  184. return genPluginMethod(
  185. target,
  186. pluginName,
  187. smp,
  188. getOrigConstrucName(target)
  189. ).bind(proxy);
  190. if (shouldWrap && property === "hooks")
  191. return wrapHooks(
  192. target,
  193. pluginName,
  194. smp,
  195. getOrigConstrucName(target)
  196. );
  197. if (typeof raw === "function") {
  198. const ret = raw.bind(proxy);
  199. if (property === "constructor")
  200. Object.defineProperty(ret, "name", {
  201. value: raw.name,
  202. });
  203. return ret;
  204. }
  205. return raw;
  206. },
  207. set: (target, property, value) => {
  208. return Reflect.set(target, property, value);
  209. },
  210. deleteProperty: (target, property) => {
  211. return Reflect.deleteProperty(target, property);
  212. },
  213. });
  214. wrappedReturn = proxy;
  215. }
  216. wrappedObjs.push({ pluginName, orig, wrapped: wrappedReturn });
  217. return wrappedReturn;
  218. };
  219. module.exports.clear = () => {
  220. wrappedObjs.length = 0;
  221. wrappedHooks.length = 0;
  222. };
  223. module.exports.WrappedPlugin = class WrappedPlugin {
  224. constructor(plugin, pluginName, smp) {
  225. this._smp_plugin = plugin;
  226. this._smp_pluginName = pluginName;
  227. this._smp = smp;
  228. this.apply = this.apply.bind(this);
  229. const wp = this;
  230. return new Proxy(plugin, {
  231. get(target, property) {
  232. if (property === "apply") {
  233. return wp.apply;
  234. }
  235. return target[property];
  236. },
  237. set: (target, property, value) => {
  238. return Reflect.set(target, property, value);
  239. },
  240. deleteProperty: (target, property) => {
  241. return Reflect.deleteProperty(target, property);
  242. },
  243. });
  244. }
  245. apply(compiler) {
  246. return this._smp_plugin.apply(
  247. wrap(compiler, this._smp_pluginName, this._smp)
  248. );
  249. }
  250. };