index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "isPluginRequired", {
  6. enumerable: true,
  7. get: function () {
  8. return _filterItems.isPluginRequired;
  9. }
  10. });
  11. exports.default = exports.transformIncludesAndExcludes = void 0;
  12. var _debug = require("./debug");
  13. var _getOptionSpecificExcludes = _interopRequireDefault(require("./get-option-specific-excludes"));
  14. var _filterItems = _interopRequireWildcard(require("./filter-items"));
  15. var _moduleTransformations = _interopRequireDefault(require("./module-transformations"));
  16. var _normalizeOptions = _interopRequireDefault(require("./normalize-options"));
  17. var _plugins = _interopRequireDefault(require("../data/plugins.json"));
  18. var _shippedProposals = require("../data/shipped-proposals");
  19. var _usagePlugin = _interopRequireDefault(require("./polyfills/corejs2/usage-plugin"));
  20. var _usagePlugin2 = _interopRequireDefault(require("./polyfills/corejs3/usage-plugin"));
  21. var _usagePlugin3 = _interopRequireDefault(require("./polyfills/regenerator/usage-plugin"));
  22. var _entryPlugin = _interopRequireDefault(require("./polyfills/corejs2/entry-plugin"));
  23. var _entryPlugin2 = _interopRequireDefault(require("./polyfills/corejs3/entry-plugin"));
  24. var _entryPlugin3 = _interopRequireDefault(require("./polyfills/regenerator/entry-plugin"));
  25. var _targetsParser = _interopRequireDefault(require("./targets-parser"));
  26. var _availablePlugins = _interopRequireDefault(require("./available-plugins"));
  27. var _utils = require("./utils");
  28. function _helperPluginUtils() {
  29. const data = require("@babel/helper-plugin-utils");
  30. _helperPluginUtils = function () {
  31. return data;
  32. };
  33. return data;
  34. }
  35. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. const pluginListWithoutProposals = (0, _utils.filterStageFromList)(_plugins.default, _shippedProposals.proposalPlugins);
  38. const getPlugin = pluginName => {
  39. const plugin = _availablePlugins.default[pluginName];
  40. if (!plugin) {
  41. throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
  42. }
  43. return plugin;
  44. };
  45. const transformIncludesAndExcludes = opts => {
  46. return opts.reduce((result, opt) => {
  47. const target = opt.match(/^(es|es6|es7|esnext|web)\./) ? "builtIns" : "plugins";
  48. result[target].add(opt);
  49. return result;
  50. }, {
  51. all: opts,
  52. plugins: new Set(),
  53. builtIns: new Set()
  54. });
  55. };
  56. exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
  57. function supportsStaticESM(caller) {
  58. return !!(caller && caller.supportsStaticESM);
  59. }
  60. function supportsDynamicImport(caller) {
  61. return !!(caller && caller.supportsDynamicImport);
  62. }
  63. var _default = (0, _helperPluginUtils().declare)((api, opts) => {
  64. api.assertVersion(7);
  65. const {
  66. configPath,
  67. debug,
  68. exclude: optionsExclude,
  69. forceAllTransforms,
  70. ignoreBrowserslistConfig,
  71. include: optionsInclude,
  72. loose,
  73. modules,
  74. shippedProposals,
  75. spec,
  76. targets: optionsTargets,
  77. useBuiltIns,
  78. corejs: {
  79. version: corejs,
  80. proposals
  81. }
  82. } = (0, _normalizeOptions.default)(opts);
  83. let hasUglifyTarget = false;
  84. if (optionsTargets && optionsTargets.uglify) {
  85. hasUglifyTarget = true;
  86. delete optionsTargets.uglify;
  87. console.log("");
  88. console.log("The uglify target has been deprecated. Set the top level");
  89. console.log("option `forceAllTransforms: true` instead.");
  90. console.log("");
  91. }
  92. if (optionsTargets && optionsTargets.esmodules && optionsTargets.browsers) {
  93. console.log("");
  94. console.log("@babel/preset-env: esmodules and browsers targets have been specified together.");
  95. console.log(`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`);
  96. console.log("");
  97. }
  98. const targets = (0, _targetsParser.default)(optionsTargets, {
  99. ignoreBrowserslistConfig,
  100. configPath
  101. });
  102. const include = transformIncludesAndExcludes(optionsInclude);
  103. const exclude = transformIncludesAndExcludes(optionsExclude);
  104. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  105. const transformations = (0, _filterItems.default)(shippedProposals ? _plugins.default : pluginListWithoutProposals, include.plugins, exclude.plugins, transformTargets, null, (0, _getOptionSpecificExcludes.default)({
  106. loose
  107. }), _shippedProposals.pluginSyntaxMap);
  108. const plugins = [];
  109. const pluginUseBuiltIns = useBuiltIns !== false;
  110. if (modules !== false && _moduleTransformations.default[modules]) {
  111. const shouldTransformESM = modules !== "auto" || !api.caller || !api.caller(supportsStaticESM);
  112. const shouldTransformDynamicImport = modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport);
  113. if (shouldTransformESM) {
  114. plugins.push([getPlugin(_moduleTransformations.default[modules]), {
  115. loose
  116. }]);
  117. }
  118. if (shouldTransformDynamicImport && shouldTransformESM && modules !== "umd") {
  119. plugins.push([getPlugin("proposal-dynamic-import"), {
  120. loose
  121. }]);
  122. } else {
  123. if (shouldTransformDynamicImport) {
  124. console.warn("Dynamic import can only be supported when transforming ES modules" + " to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.");
  125. }
  126. plugins.push(getPlugin("syntax-dynamic-import"));
  127. }
  128. } else {
  129. plugins.push(getPlugin("syntax-dynamic-import"));
  130. }
  131. transformations.forEach(pluginName => plugins.push([getPlugin(pluginName), {
  132. spec,
  133. loose,
  134. useBuiltIns: pluginUseBuiltIns
  135. }]));
  136. if (debug) {
  137. console.log("@babel/preset-env: `DEBUG` option");
  138. console.log("\nUsing targets:");
  139. console.log(JSON.stringify((0, _utils.prettifyTargets)(targets), null, 2));
  140. console.log(`\nUsing modules transform: ${modules.toString()}`);
  141. console.log("\nUsing plugins:");
  142. transformations.forEach(transform => {
  143. (0, _debug.logPluginOrPolyfill)(transform, targets, _plugins.default);
  144. });
  145. if (!useBuiltIns) {
  146. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  147. } else {
  148. console.log(`\nUsing polyfills with \`${useBuiltIns}\` option:`);
  149. }
  150. }
  151. if (useBuiltIns === "usage" || useBuiltIns === "entry") {
  152. const regenerator = transformations.has("transform-regenerator");
  153. const pluginOptions = {
  154. corejs,
  155. polyfillTargets: targets,
  156. include: include.builtIns,
  157. exclude: exclude.builtIns,
  158. proposals,
  159. shippedProposals,
  160. regenerator,
  161. debug
  162. };
  163. if (corejs) {
  164. if (useBuiltIns === "usage") {
  165. if (corejs.major === 2) {
  166. plugins.push([_usagePlugin.default, pluginOptions]);
  167. } else {
  168. plugins.push([_usagePlugin2.default, pluginOptions]);
  169. }
  170. if (regenerator) {
  171. plugins.push([_usagePlugin3.default, pluginOptions]);
  172. }
  173. } else {
  174. if (corejs.major === 2) {
  175. plugins.push([_entryPlugin.default, pluginOptions]);
  176. } else {
  177. plugins.push([_entryPlugin2.default, pluginOptions]);
  178. if (!regenerator) {
  179. plugins.push([_entryPlugin3.default, pluginOptions]);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. return {
  186. plugins
  187. };
  188. });
  189. exports.default = _default;