Tapable.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 SyncBailHook = require("./SyncBailHook");
  8. function Tapable() {
  9. this._pluginCompat = new SyncBailHook(["options"]);
  10. this._pluginCompat.tap({
  11. name: "Tapable camelCase",
  12. stage: 100
  13. }, options => {
  14. options.names.add(options.name.replace(/[- ]([a-z])/g, str => str.substr(1).toUpperCase()));
  15. });
  16. this._pluginCompat.tap({
  17. name: "Tapable this.hooks",
  18. stage: 200
  19. }, options => {
  20. let hook;
  21. for(const name of options.names) {
  22. hook = this.hooks[name];
  23. if(hook !== undefined) {
  24. break;
  25. }
  26. }
  27. if(hook !== undefined) {
  28. const tapOpt = {
  29. name: options.fn.name || "unnamed compat plugin",
  30. stage: options.stage || 0
  31. };
  32. if(options.async)
  33. hook.tapAsync(tapOpt, options.fn);
  34. else
  35. hook.tap(tapOpt, options.fn);
  36. return true;
  37. }
  38. });
  39. }
  40. module.exports = Tapable;
  41. Tapable.addCompatLayer = function addCompatLayer(instance) {
  42. Tapable.call(instance);
  43. instance.plugin = Tapable.prototype.plugin;
  44. instance.apply = Tapable.prototype.apply;
  45. };
  46. Tapable.prototype.plugin = util.deprecate(function plugin(name, fn) {
  47. if(Array.isArray(name)) {
  48. name.forEach(function(name) {
  49. this.plugin(name, fn);
  50. }, this);
  51. return;
  52. }
  53. const result = this._pluginCompat.call({
  54. name: name,
  55. fn: fn,
  56. names: new Set([name])
  57. });
  58. if(!result) {
  59. throw new Error(`Plugin could not be registered at '${name}'. Hook was not found.\n` +
  60. "BREAKING CHANGE: There need to exist a hook at 'this.hooks'. " +
  61. "To create a compatiblity layer for this hook, hook into 'this._pluginCompat'.");
  62. }
  63. }, "Tapable.plugin is deprecated. Use new API on `.hooks` instead");
  64. Tapable.prototype.apply = util.deprecate(function apply() {
  65. for(var i = 0; i < arguments.length; i++) {
  66. arguments[i].apply(this);
  67. }
  68. }, "Tapable.apply is deprecated. Call apply on the plugin directly instead");