index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.transformIncludesAndExcludes = exports.isPluginRequired = void 0;
  6. function _semver() {
  7. const data = _interopRequireDefault(require("semver"));
  8. _semver = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _builtIns = _interopRequireDefault(require("../data/built-ins.json"));
  14. var _debug = require("./debug");
  15. var _defaults = require("./defaults");
  16. var _moduleTransformations = _interopRequireDefault(require("./module-transformations"));
  17. var _normalizeOptions = _interopRequireDefault(require("./normalize-options.js"));
  18. var _plugins = _interopRequireDefault(require("../data/plugins.json"));
  19. var _shippedProposals = require("../data/shipped-proposals.js");
  20. var _useBuiltInsEntryPlugin = _interopRequireDefault(require("./use-built-ins-entry-plugin"));
  21. var _useBuiltInsPlugin = _interopRequireDefault(require("./use-built-ins-plugin"));
  22. var _targetsParser = _interopRequireDefault(require("./targets-parser"));
  23. var _availablePlugins = _interopRequireDefault(require("./available-plugins"));
  24. var _utils = require("./utils");
  25. function _helperPluginUtils() {
  26. const data = require("@babel/helper-plugin-utils");
  27. _helperPluginUtils = function () {
  28. return data;
  29. };
  30. return data;
  31. }
  32. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  33. const getPlugin = pluginName => {
  34. const plugin = _availablePlugins.default[pluginName];
  35. if (!plugin) {
  36. throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
  37. }
  38. return plugin;
  39. };
  40. const builtInsListWithoutProposals = (0, _utils.filterStageFromList)(_builtIns.default, _shippedProposals.builtIns);
  41. const pluginListWithoutProposals = (0, _utils.filterStageFromList)(_plugins.default, _shippedProposals.features);
  42. const isPluginRequired = (supportedEnvironments, plugin) => {
  43. const targetEnvironments = Object.keys(supportedEnvironments);
  44. if (targetEnvironments.length === 0) {
  45. return true;
  46. }
  47. const isRequiredForEnvironments = targetEnvironments.filter(environment => {
  48. if (!plugin[environment]) {
  49. return true;
  50. }
  51. const lowestImplementedVersion = plugin[environment];
  52. const lowestTargetedVersion = supportedEnvironments[environment];
  53. if ((0, _utils.isUnreleasedVersion)(lowestTargetedVersion, environment)) {
  54. return false;
  55. } else if ((0, _utils.isUnreleasedVersion)(lowestImplementedVersion, environment)) {
  56. return true;
  57. }
  58. if (!_semver().default.valid(lowestTargetedVersion)) {
  59. throw new Error(`Invalid version passed for target "${environment}": "${lowestTargetedVersion}". ` + "Versions must be in semver format (major.minor.patch)");
  60. }
  61. return _semver().default.gt((0, _utils.semverify)(lowestImplementedVersion), lowestTargetedVersion);
  62. });
  63. return isRequiredForEnvironments.length > 0;
  64. };
  65. exports.isPluginRequired = isPluginRequired;
  66. const getBuiltInTargets = targets => {
  67. const builtInTargets = Object.assign({}, targets);
  68. if (builtInTargets.uglify != null) {
  69. delete builtInTargets.uglify;
  70. }
  71. return builtInTargets;
  72. };
  73. const transformIncludesAndExcludes = opts => {
  74. return opts.reduce((result, opt) => {
  75. const target = opt.match(/^(es\d+|web)\./) ? "builtIns" : "plugins";
  76. result[target].add(opt);
  77. return result;
  78. }, {
  79. all: opts,
  80. plugins: new Set(),
  81. builtIns: new Set()
  82. });
  83. };
  84. exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
  85. const filterItems = (list, includes, excludes, targets, defaultIncludes, defaultExcludes) => {
  86. const result = new Set();
  87. for (const item in list) {
  88. if (!excludes.has(item) && (isPluginRequired(targets, list[item]) || includes.has(item))) {
  89. result.add(item);
  90. } else {
  91. const shippedProposalsSyntax = _shippedProposals.pluginSyntaxMap.get(item);
  92. if (shippedProposalsSyntax) {
  93. result.add(shippedProposalsSyntax);
  94. }
  95. }
  96. }
  97. if (defaultIncludes) {
  98. defaultIncludes.forEach(item => !excludes.has(item) && result.add(item));
  99. }
  100. if (defaultExcludes) {
  101. defaultExcludes.forEach(item => !includes.has(item) && result.delete(item));
  102. }
  103. return result;
  104. };
  105. function supportsStaticESM(caller) {
  106. return !!(caller && caller.supportsStaticESM);
  107. }
  108. var _default = (0, _helperPluginUtils().declare)((api, opts) => {
  109. api.assertVersion(7);
  110. const {
  111. configPath,
  112. debug,
  113. exclude: optionsExclude,
  114. forceAllTransforms,
  115. ignoreBrowserslistConfig,
  116. include: optionsInclude,
  117. loose,
  118. modules,
  119. shippedProposals,
  120. spec,
  121. targets: optionsTargets,
  122. useBuiltIns
  123. } = (0, _normalizeOptions.default)(opts);
  124. let hasUglifyTarget = false;
  125. if (optionsTargets && optionsTargets.uglify) {
  126. hasUglifyTarget = true;
  127. delete optionsTargets.uglify;
  128. console.log("");
  129. console.log("The uglify target has been deprecated. Set the top level");
  130. console.log("option `forceAllTransforms: true` instead.");
  131. console.log("");
  132. }
  133. if (optionsTargets && optionsTargets.esmodules && optionsTargets.browsers) {
  134. console.log("");
  135. console.log("@babel/preset-env: esmodules and browsers targets have been specified together.");
  136. console.log(`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`);
  137. console.log("");
  138. }
  139. const targets = (0, _targetsParser.default)(optionsTargets, {
  140. ignoreBrowserslistConfig,
  141. configPath
  142. });
  143. const include = transformIncludesAndExcludes(optionsInclude);
  144. const exclude = transformIncludesAndExcludes(optionsExclude);
  145. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  146. const transformations = filterItems(shippedProposals ? _plugins.default : pluginListWithoutProposals, include.plugins, exclude.plugins, transformTargets, null, (0, _defaults.getOptionSpecificExcludesFor)({
  147. loose
  148. }));
  149. let polyfills;
  150. let polyfillTargets;
  151. if (useBuiltIns) {
  152. polyfillTargets = getBuiltInTargets(targets);
  153. polyfills = filterItems(shippedProposals ? _builtIns.default : builtInsListWithoutProposals, include.builtIns, exclude.builtIns, polyfillTargets, (0, _defaults.getPlatformSpecificDefaultFor)(polyfillTargets));
  154. }
  155. const plugins = [];
  156. const pluginUseBuiltIns = useBuiltIns !== false;
  157. if (modules !== false && _moduleTransformations.default[modules] && (modules !== "auto" || !api.caller || !api.caller(supportsStaticESM))) {
  158. plugins.push([getPlugin(_moduleTransformations.default[modules]), {
  159. loose
  160. }]);
  161. }
  162. transformations.forEach(pluginName => plugins.push([getPlugin(pluginName), {
  163. spec,
  164. loose,
  165. useBuiltIns: pluginUseBuiltIns
  166. }]));
  167. const regenerator = transformations.has("transform-regenerator");
  168. if (debug) {
  169. console.log("@babel/preset-env: `DEBUG` option");
  170. console.log("\nUsing targets:");
  171. console.log(JSON.stringify((0, _utils.prettifyTargets)(targets), null, 2));
  172. console.log(`\nUsing modules transform: ${modules.toString()}`);
  173. console.log("\nUsing plugins:");
  174. transformations.forEach(transform => {
  175. (0, _debug.logPlugin)(transform, targets, _plugins.default);
  176. });
  177. if (!useBuiltIns) {
  178. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  179. } else {
  180. console.log(`
  181. Using polyfills with \`${useBuiltIns}\` option:`);
  182. }
  183. }
  184. if (useBuiltIns === "usage" || useBuiltIns === "entry") {
  185. const pluginOptions = {
  186. debug,
  187. polyfills,
  188. regenerator,
  189. onDebug: (polyfills, context) => {
  190. polyfills.forEach(polyfill => (0, _debug.logPlugin)(polyfill, polyfillTargets, _builtIns.default, context));
  191. }
  192. };
  193. plugins.push([useBuiltIns === "usage" ? _useBuiltInsPlugin.default : _useBuiltInsEntryPlugin.default, pluginOptions]);
  194. }
  195. return {
  196. plugins
  197. };
  198. });
  199. exports.default = _default;