plugin-utils.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.hasPlugin = hasPlugin;
  6. exports.getPluginOption = getPluginOption;
  7. exports.validatePlugins = validatePlugins;
  8. exports.mixinPluginNames = exports.mixinPlugins = void 0;
  9. var _estree = _interopRequireDefault(require("./plugins/estree"));
  10. var _flow = _interopRequireDefault(require("./plugins/flow"));
  11. var _jsx = _interopRequireDefault(require("./plugins/jsx"));
  12. var _typescript = _interopRequireDefault(require("./plugins/typescript"));
  13. var _placeholders = _interopRequireDefault(require("./plugins/placeholders"));
  14. var _v8intrinsic = _interopRequireDefault(require("./plugins/v8intrinsic"));
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function hasPlugin(plugins, name) {
  17. return plugins.some(plugin => {
  18. if (Array.isArray(plugin)) {
  19. return plugin[0] === name;
  20. } else {
  21. return plugin === name;
  22. }
  23. });
  24. }
  25. function getPluginOption(plugins, name, option) {
  26. const plugin = plugins.find(plugin => {
  27. if (Array.isArray(plugin)) {
  28. return plugin[0] === name;
  29. } else {
  30. return plugin === name;
  31. }
  32. });
  33. if (plugin && Array.isArray(plugin)) {
  34. return plugin[1][option];
  35. }
  36. return null;
  37. }
  38. const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
  39. function validatePlugins(plugins) {
  40. if (hasPlugin(plugins, "decorators")) {
  41. if (hasPlugin(plugins, "decorators-legacy")) {
  42. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  43. }
  44. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  45. if (decoratorsBeforeExport == null) {
  46. throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
  47. } else if (typeof decoratorsBeforeExport !== "boolean") {
  48. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  49. }
  50. }
  51. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  52. throw new Error("Cannot combine flow and typescript plugins.");
  53. }
  54. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  55. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  56. }
  57. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  58. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  59. }
  60. }
  61. const mixinPlugins = {
  62. estree: _estree.default,
  63. jsx: _jsx.default,
  64. flow: _flow.default,
  65. typescript: _typescript.default,
  66. v8intrinsic: _v8intrinsic.default,
  67. placeholders: _placeholders.default
  68. };
  69. exports.mixinPlugins = mixinPlugins;
  70. const mixinPluginNames = Object.keys(mixinPlugins);
  71. exports.mixinPluginNames = mixinPluginNames;