index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isPluginRequired = isPluginRequired;
  6. exports.default = exports.getPolyfillPlugins = exports.getModulesPluginNames = exports.transformIncludesAndExcludes = void 0;
  7. var _semver = require("semver");
  8. var _debug = require("./debug");
  9. var _getOptionSpecificExcludes = _interopRequireDefault(require("./get-option-specific-excludes"));
  10. var _filterItems = require("./filter-items");
  11. var _moduleTransformations = _interopRequireDefault(require("./module-transformations"));
  12. var _normalizeOptions = _interopRequireDefault(require("./normalize-options"));
  13. var _shippedProposals = require("../data/shipped-proposals");
  14. var _pluginsCompatData = require("./plugins-compat-data");
  15. var _overlappingPlugins = _interopRequireDefault(require("@babel/compat-data/overlapping-plugins"));
  16. var _usagePlugin = _interopRequireDefault(require("./polyfills/corejs2/usage-plugin"));
  17. var _usagePlugin2 = _interopRequireDefault(require("./polyfills/corejs3/usage-plugin"));
  18. var _usagePlugin3 = _interopRequireDefault(require("./polyfills/regenerator/usage-plugin"));
  19. var _entryPlugin = _interopRequireDefault(require("./polyfills/corejs2/entry-plugin"));
  20. var _entryPlugin2 = _interopRequireDefault(require("./polyfills/corejs3/entry-plugin"));
  21. var _entryPlugin3 = _interopRequireDefault(require("./polyfills/regenerator/entry-plugin"));
  22. var _helperCompilationTargets = _interopRequireWildcard(require("@babel/helper-compilation-targets"));
  23. var _availablePlugins = _interopRequireDefault(require("./available-plugins"));
  24. var _utils = require("./utils");
  25. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  26. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  27. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. function isPluginRequired(targets, support) {
  30. return (0, _helperCompilationTargets.isRequired)("fake-name", targets, {
  31. compatData: {
  32. "fake-name": support
  33. }
  34. });
  35. }
  36. const pluginLists = {
  37. withProposals: {
  38. withoutBugfixes: _pluginsCompatData.plugins,
  39. withBugfixes: Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes)
  40. },
  41. withoutProposals: {
  42. withoutBugfixes: (0, _utils.filterStageFromList)(_pluginsCompatData.plugins, _shippedProposals.proposalPlugins),
  43. withBugfixes: (0, _utils.filterStageFromList)(Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes), _shippedProposals.proposalPlugins)
  44. }
  45. };
  46. function getPluginList(proposals, bugfixes) {
  47. if (proposals) {
  48. if (bugfixes) return pluginLists.withProposals.withBugfixes;else return pluginLists.withProposals.withoutBugfixes;
  49. } else {
  50. if (bugfixes) return pluginLists.withoutProposals.withBugfixes;else return pluginLists.withoutProposals.withoutBugfixes;
  51. }
  52. }
  53. const getPlugin = pluginName => {
  54. const plugin = _availablePlugins.default[pluginName];
  55. if (!plugin) {
  56. throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
  57. }
  58. return plugin;
  59. };
  60. const transformIncludesAndExcludes = opts => {
  61. return opts.reduce((result, opt) => {
  62. const target = opt.match(/^(es|es6|es7|esnext|web)\./) ? "builtIns" : "plugins";
  63. result[target].add(opt);
  64. return result;
  65. }, {
  66. all: opts,
  67. plugins: new Set(),
  68. builtIns: new Set()
  69. });
  70. };
  71. exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
  72. const getModulesPluginNames = ({
  73. modules,
  74. transformations,
  75. shouldTransformESM,
  76. shouldTransformDynamicImport,
  77. shouldParseTopLevelAwait
  78. }) => {
  79. const modulesPluginNames = [];
  80. if (modules !== false && transformations[modules]) {
  81. if (shouldTransformESM) {
  82. modulesPluginNames.push(transformations[modules]);
  83. }
  84. if (shouldTransformDynamicImport && shouldTransformESM && modules !== "umd") {
  85. modulesPluginNames.push("proposal-dynamic-import");
  86. } else {
  87. if (shouldTransformDynamicImport) {
  88. console.warn("Dynamic import can only be supported when transforming ES modules" + " to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.");
  89. }
  90. modulesPluginNames.push("syntax-dynamic-import");
  91. }
  92. } else {
  93. modulesPluginNames.push("syntax-dynamic-import");
  94. }
  95. if (shouldParseTopLevelAwait) {
  96. modulesPluginNames.push("syntax-top-level-await");
  97. }
  98. return modulesPluginNames;
  99. };
  100. exports.getModulesPluginNames = getModulesPluginNames;
  101. const getPolyfillPlugins = ({
  102. useBuiltIns,
  103. corejs,
  104. polyfillTargets,
  105. include,
  106. exclude,
  107. proposals,
  108. shippedProposals,
  109. regenerator,
  110. debug
  111. }) => {
  112. const polyfillPlugins = [];
  113. if (useBuiltIns === "usage" || useBuiltIns === "entry") {
  114. const pluginOptions = {
  115. corejs,
  116. polyfillTargets,
  117. include,
  118. exclude,
  119. proposals,
  120. shippedProposals,
  121. regenerator,
  122. debug
  123. };
  124. if (corejs) {
  125. if (useBuiltIns === "usage") {
  126. if (corejs.major === 2) {
  127. polyfillPlugins.push([_usagePlugin.default, pluginOptions]);
  128. } else {
  129. polyfillPlugins.push([_usagePlugin2.default, pluginOptions]);
  130. }
  131. if (regenerator) {
  132. polyfillPlugins.push([_usagePlugin3.default, pluginOptions]);
  133. }
  134. } else {
  135. if (corejs.major === 2) {
  136. polyfillPlugins.push([_entryPlugin.default, pluginOptions]);
  137. } else {
  138. polyfillPlugins.push([_entryPlugin2.default, pluginOptions]);
  139. if (!regenerator) {
  140. polyfillPlugins.push([_entryPlugin3.default, pluginOptions]);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. return polyfillPlugins;
  147. };
  148. exports.getPolyfillPlugins = getPolyfillPlugins;
  149. function supportsStaticESM(caller) {
  150. return !!(caller && caller.supportsStaticESM);
  151. }
  152. function supportsDynamicImport(caller) {
  153. return !!(caller && caller.supportsDynamicImport);
  154. }
  155. function supportsTopLevelAwait(caller) {
  156. return !!(caller && caller.supportsTopLevelAwait);
  157. }
  158. var _default = (0, _helperPluginUtils.declare)((api, opts) => {
  159. api.assertVersion(7);
  160. const {
  161. bugfixes,
  162. configPath,
  163. debug,
  164. exclude: optionsExclude,
  165. forceAllTransforms,
  166. ignoreBrowserslistConfig,
  167. include: optionsInclude,
  168. loose,
  169. modules,
  170. shippedProposals,
  171. spec,
  172. targets: optionsTargets,
  173. useBuiltIns,
  174. corejs: {
  175. version: corejs,
  176. proposals
  177. }
  178. } = (0, _normalizeOptions.default)(opts);
  179. let hasUglifyTarget = false;
  180. if (optionsTargets && optionsTargets.uglify) {
  181. hasUglifyTarget = true;
  182. delete optionsTargets.uglify;
  183. console.log("");
  184. console.log("The uglify target has been deprecated. Set the top level");
  185. console.log("option `forceAllTransforms: true` instead.");
  186. console.log("");
  187. }
  188. if (optionsTargets && optionsTargets.esmodules && optionsTargets.browsers) {
  189. console.log("");
  190. console.log("@babel/preset-env: esmodules and browsers targets have been specified together.");
  191. console.log(`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`);
  192. console.log("");
  193. }
  194. const targets = (0, _helperCompilationTargets.default)(optionsTargets, {
  195. ignoreBrowserslistConfig,
  196. configPath
  197. });
  198. const include = transformIncludesAndExcludes(optionsInclude);
  199. const exclude = transformIncludesAndExcludes(optionsExclude);
  200. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  201. const modulesPluginNames = getModulesPluginNames({
  202. modules,
  203. transformations: _moduleTransformations.default,
  204. shouldTransformESM: modules !== "auto" || !api.caller || !api.caller(supportsStaticESM),
  205. shouldTransformDynamicImport: modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport),
  206. shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait)
  207. });
  208. const pluginNames = (0, _helperCompilationTargets.filterItems)(getPluginList(shippedProposals, bugfixes), include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
  209. loose
  210. }), _shippedProposals.pluginSyntaxMap);
  211. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _overlappingPlugins.default);
  212. const polyfillPlugins = getPolyfillPlugins({
  213. useBuiltIns,
  214. corejs,
  215. polyfillTargets: targets,
  216. include: include.builtIns,
  217. exclude: exclude.builtIns,
  218. proposals,
  219. shippedProposals,
  220. regenerator: pluginNames.has("transform-regenerator"),
  221. debug
  222. });
  223. const pluginUseBuiltIns = useBuiltIns !== false;
  224. const plugins = Array.from(pluginNames).map(pluginName => [getPlugin(pluginName), {
  225. spec,
  226. loose,
  227. useBuiltIns: pluginUseBuiltIns
  228. }]).concat(polyfillPlugins);
  229. if (debug) {
  230. console.log("@babel/preset-env: `DEBUG` option");
  231. console.log("\nUsing targets:");
  232. console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
  233. console.log(`\nUsing modules transform: ${modules.toString()}`);
  234. console.log("\nUsing plugins:");
  235. pluginNames.forEach(pluginName => {
  236. (0, _debug.logPluginOrPolyfill)(pluginName, targets, _pluginsCompatData.plugins);
  237. });
  238. if (!useBuiltIns) {
  239. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  240. } else {
  241. console.log(`\nUsing polyfills with \`${useBuiltIns}\` option:`);
  242. }
  243. }
  244. return {
  245. plugins
  246. };
  247. });
  248. exports.default = _default;