plugin-utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  40. function validatePlugins(plugins) {
  41. if (hasPlugin(plugins, "decorators")) {
  42. if (hasPlugin(plugins, "decorators-legacy")) {
  43. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  44. }
  45. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  46. if (decoratorsBeforeExport == null) {
  47. 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'.");
  48. } else if (typeof decoratorsBeforeExport !== "boolean") {
  49. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  50. }
  51. }
  52. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  53. throw new Error("Cannot combine flow and typescript plugins.");
  54. }
  55. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  56. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  57. }
  58. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  59. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  60. }
  61. if (hasPlugin(plugins, "moduleAttributes")) {
  62. if (hasPlugin(plugins, "importAssertions")) {
  63. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  64. }
  65. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  66. if (moduleAttributesVerionPluginOption !== "may-2020") {
  67. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  68. }
  69. }
  70. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  71. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  72. }
  73. }
  74. const mixinPlugins = {
  75. estree: _estree.default,
  76. jsx: _jsx.default,
  77. flow: _flow.default,
  78. typescript: _typescript.default,
  79. v8intrinsic: _v8intrinsic.default,
  80. placeholders: _placeholders.default
  81. };
  82. exports.mixinPlugins = mixinPlugins;
  83. const mixinPluginNames = Object.keys(mixinPlugins);
  84. exports.mixinPluginNames = mixinPluginNames;