compose.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.composePlugins = exports.parsePluginConfig = void 0;
  6. var _phases = require("./phases");
  7. var _optional = require("./optional");
  8. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  9. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  10. /**
  11. * Plugins can be added to `withPlugins` in multiple ways.
  12. * All possibilities are handled here and returned in a standardized way.
  13. *
  14. * @param {array|function} plugin - plugin configuration
  15. */
  16. const parsePluginConfig = plugin => {
  17. // it can only depend on phases if it has specific configuration
  18. if (plugin instanceof Array) {
  19. // if the plugin array contains 3 values, it always depends on phases
  20. // [plugin: function, config: object, phases: array]
  21. if (plugin.length > 2) {
  22. return {
  23. pluginFunction: plugin[0],
  24. pluginConfig: plugin[1],
  25. phases: plugin[2]
  26. };
  27. } // if the plugin array contains 2 values and the second one is an array, it depends on phases
  28. // [plugin: function, phases: array]
  29. if (plugin.length > 1 && plugin[1] instanceof Array) {
  30. return {
  31. pluginFunction: plugin[0],
  32. pluginConfig: {},
  33. phases: plugin[1]
  34. };
  35. } // plugin does not contain phase specific config but could have plugin configuration
  36. // [plugin: function, config?: object]
  37. return {
  38. pluginFunction: plugin[0],
  39. pluginConfig: plugin[1] || {},
  40. phases: null
  41. };
  42. }
  43. return {
  44. pluginFunction: plugin,
  45. pluginConfig: {},
  46. phases: null
  47. };
  48. };
  49. /**
  50. * Composes all plugins
  51. *
  52. * @param {string} phase - current phase
  53. * @param {array} plugins - all plugins
  54. * @param {object} initialConfig - initial configuration
  55. */
  56. exports.parsePluginConfig = parsePluginConfig;
  57. const composePlugins = (phase, plugins, initialConfig) => {
  58. const nextComposePluginsParam = {
  59. nextComposePlugins: true,
  60. phase
  61. };
  62. let config = (0, _phases.mergePhaseConfiguration)(phase, _objectSpread({}, initialConfig));
  63. plugins.forEach(plugin => {
  64. const _parsePluginConfig = parsePluginConfig(plugin),
  65. pluginFunction = _parsePluginConfig.pluginFunction,
  66. pluginConfig = _parsePluginConfig.pluginConfig,
  67. phases = _parsePluginConfig.phases; // check if the plugin should not get executed in the current phase
  68. if (phases !== null) {
  69. if (!(0, _phases.isInCurrentPhase)(phase, phases)) {
  70. return;
  71. }
  72. }
  73. let resolvedPlugin = pluginFunction;
  74. if ((0, _optional.isOptional)(pluginFunction)) {
  75. resolvedPlugin = (0, _optional.resolveOptionalPlugin)(pluginFunction);
  76. }
  77. const mergedPluginConfig = (0, _phases.mergePhaseConfiguration)(phase, pluginConfig);
  78. let updatedConfig;
  79. if (typeof resolvedPlugin === 'function') {
  80. updatedConfig = resolvedPlugin(_objectSpread({}, config, mergedPluginConfig), nextComposePluginsParam);
  81. } else if (typeof resolvedPlugin === 'object') {
  82. updatedConfig = resolvedPlugin;
  83. } else {
  84. throw new Error('Incompatible plugin: plugin needs to export either a function or an object!');
  85. } // check if the plugin itself has defined in phases it should run
  86. // and the user did not overwrite it
  87. if (phases === null && updatedConfig.phases) {
  88. if (!(0, _phases.isInCurrentPhase)(phase, updatedConfig.phases)) {
  89. return;
  90. }
  91. } // delete plugin specific phases array so it doesn't propagate to the next plugin
  92. if (updatedConfig.phases) {
  93. delete updatedConfig.phases;
  94. } // merge config back to the main one
  95. config = _objectSpread({}, config, updatedConfig);
  96. });
  97. return config;
  98. };
  99. exports.composePlugins = composePlugins;