plugins.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.getPlugins = getPlugins;
  4. exports.resolvePlugin = resolvePlugin;
  5. exports.loadPlugin = loadPlugin;
  6. var _pluginJsx = _interopRequireDefault(require("@svgr/plugin-jsx"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. const DEFAULT_PLUGINS = [_pluginJsx.default];
  9. function getPlugins(config, state) {
  10. if (config.plugins) {
  11. return config.plugins;
  12. }
  13. if (state.caller && state.caller.defaultPlugins) {
  14. return state.caller.defaultPlugins;
  15. }
  16. return DEFAULT_PLUGINS;
  17. }
  18. function resolvePlugin(plugin) {
  19. if (typeof plugin === 'function') {
  20. return plugin;
  21. }
  22. if (typeof plugin === 'string') {
  23. return loadPlugin(plugin);
  24. }
  25. throw new Error(`Invalid plugin "${plugin}"`);
  26. }
  27. const pluginCache = {};
  28. function loadPlugin(moduleName) {
  29. if (pluginCache[moduleName]) {
  30. return pluginCache[moduleName];
  31. }
  32. try {
  33. // eslint-disable-next-line
  34. const plugin = require(moduleName);
  35. if (!plugin.default || !plugin) {
  36. throw new Error(`Invalid plugin "${moduleName}"`);
  37. }
  38. pluginCache[moduleName] = plugin.default || plugin;
  39. return pluginCache[moduleName];
  40. } catch (error) {
  41. throw new Error(`Module "${moduleName}" missing. Maybe \`npm install ${moduleName}\` could help!`);
  42. }
  43. }