index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. shouldTransformExportNamespaceFrom,
  78. shouldParseTopLevelAwait
  79. }) => {
  80. const modulesPluginNames = [];
  81. if (modules !== false && transformations[modules]) {
  82. if (shouldTransformESM) {
  83. modulesPluginNames.push(transformations[modules]);
  84. }
  85. if (shouldTransformDynamicImport && shouldTransformESM && modules !== "umd") {
  86. modulesPluginNames.push("proposal-dynamic-import");
  87. } else {
  88. if (shouldTransformDynamicImport) {
  89. console.warn("Dynamic import can only be supported when transforming ES modules" + " to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.");
  90. }
  91. modulesPluginNames.push("syntax-dynamic-import");
  92. }
  93. } else {
  94. modulesPluginNames.push("syntax-dynamic-import");
  95. }
  96. if (shouldTransformExportNamespaceFrom) {
  97. modulesPluginNames.push("proposal-export-namespace-from");
  98. } else {
  99. modulesPluginNames.push("syntax-export-namespace-from");
  100. }
  101. if (shouldParseTopLevelAwait) {
  102. modulesPluginNames.push("syntax-top-level-await");
  103. }
  104. return modulesPluginNames;
  105. };
  106. exports.getModulesPluginNames = getModulesPluginNames;
  107. const getPolyfillPlugins = ({
  108. useBuiltIns,
  109. corejs,
  110. polyfillTargets,
  111. include,
  112. exclude,
  113. proposals,
  114. shippedProposals,
  115. regenerator,
  116. debug
  117. }) => {
  118. const polyfillPlugins = [];
  119. if (useBuiltIns === "usage" || useBuiltIns === "entry") {
  120. const pluginOptions = {
  121. corejs,
  122. polyfillTargets,
  123. include,
  124. exclude,
  125. proposals,
  126. shippedProposals,
  127. regenerator,
  128. debug
  129. };
  130. if (corejs) {
  131. if (useBuiltIns === "usage") {
  132. if (corejs.major === 2) {
  133. polyfillPlugins.push([_usagePlugin.default, pluginOptions]);
  134. } else {
  135. polyfillPlugins.push([_usagePlugin2.default, pluginOptions]);
  136. }
  137. if (regenerator) {
  138. polyfillPlugins.push([_usagePlugin3.default, pluginOptions]);
  139. }
  140. } else {
  141. if (corejs.major === 2) {
  142. polyfillPlugins.push([_entryPlugin.default, pluginOptions]);
  143. } else {
  144. polyfillPlugins.push([_entryPlugin2.default, pluginOptions]);
  145. if (!regenerator) {
  146. polyfillPlugins.push([_entryPlugin3.default, pluginOptions]);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. return polyfillPlugins;
  153. };
  154. exports.getPolyfillPlugins = getPolyfillPlugins;
  155. function supportsStaticESM(caller) {
  156. return !!(caller == null ? void 0 : caller.supportsStaticESM);
  157. }
  158. function supportsDynamicImport(caller) {
  159. return !!(caller == null ? void 0 : caller.supportsDynamicImport);
  160. }
  161. function supportsExportNamespaceFrom(caller) {
  162. return !!(caller == null ? void 0 : caller.supportsExportNamespaceFrom);
  163. }
  164. function supportsTopLevelAwait(caller) {
  165. return !!(caller == null ? void 0 : caller.supportsTopLevelAwait);
  166. }
  167. var _default = (0, _helperPluginUtils.declare)((api, opts) => {
  168. api.assertVersion(7);
  169. const {
  170. bugfixes,
  171. configPath,
  172. debug,
  173. exclude: optionsExclude,
  174. forceAllTransforms,
  175. ignoreBrowserslistConfig,
  176. include: optionsInclude,
  177. loose,
  178. modules,
  179. shippedProposals,
  180. spec,
  181. targets: optionsTargets,
  182. useBuiltIns,
  183. corejs: {
  184. version: corejs,
  185. proposals
  186. },
  187. browserslistEnv
  188. } = (0, _normalizeOptions.default)(opts);
  189. let hasUglifyTarget = false;
  190. if (optionsTargets == null ? void 0 : optionsTargets.uglify) {
  191. hasUglifyTarget = true;
  192. delete optionsTargets.uglify;
  193. console.log("");
  194. console.log("The uglify target has been deprecated. Set the top level");
  195. console.log("option `forceAllTransforms: true` instead.");
  196. console.log("");
  197. }
  198. if ((optionsTargets == null ? void 0 : optionsTargets.esmodules) && optionsTargets.browsers) {
  199. console.log("");
  200. console.log("@babel/preset-env: esmodules and browsers targets have been specified together.");
  201. console.log(`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`);
  202. console.log("");
  203. }
  204. const targets = (0, _helperCompilationTargets.default)(optionsTargets, {
  205. ignoreBrowserslistConfig,
  206. configPath,
  207. browserslistEnv
  208. });
  209. const include = transformIncludesAndExcludes(optionsInclude);
  210. const exclude = transformIncludesAndExcludes(optionsExclude);
  211. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  212. const compatData = getPluginList(shippedProposals, bugfixes);
  213. const shouldSkipExportNamespaceFrom = modules === "auto" && (api.caller == null ? void 0 : api.caller(supportsExportNamespaceFrom)) || modules === false && !(0, _helperCompilationTargets.isRequired)("proposal-export-namespace-from", transformTargets, {
  214. compatData,
  215. includes: include.plugins,
  216. excludes: exclude.plugins
  217. });
  218. const modulesPluginNames = getModulesPluginNames({
  219. modules,
  220. transformations: _moduleTransformations.default,
  221. shouldTransformESM: modules !== "auto" || !(api.caller == null ? void 0 : api.caller(supportsStaticESM)),
  222. shouldTransformDynamicImport: modules !== "auto" || !(api.caller == null ? void 0 : api.caller(supportsDynamicImport)),
  223. shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom,
  224. shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait)
  225. });
  226. const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
  227. loose
  228. }), _shippedProposals.pluginSyntaxMap);
  229. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _overlappingPlugins.default);
  230. const polyfillPlugins = getPolyfillPlugins({
  231. useBuiltIns,
  232. corejs,
  233. polyfillTargets: targets,
  234. include: include.builtIns,
  235. exclude: exclude.builtIns,
  236. proposals,
  237. shippedProposals,
  238. regenerator: pluginNames.has("transform-regenerator"),
  239. debug
  240. });
  241. const pluginUseBuiltIns = useBuiltIns !== false;
  242. const plugins = Array.from(pluginNames).map(pluginName => {
  243. if (pluginName === "proposal-class-properties" || pluginName === "proposal-private-methods" || pluginName === "proposal-private-property-in-object") {
  244. return [getPlugin(pluginName), {
  245. loose: loose ? "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error" : "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error"
  246. }];
  247. }
  248. return [getPlugin(pluginName), {
  249. spec,
  250. loose,
  251. useBuiltIns: pluginUseBuiltIns
  252. }];
  253. }).concat(polyfillPlugins);
  254. if (debug) {
  255. console.log("@babel/preset-env: `DEBUG` option");
  256. console.log("\nUsing targets:");
  257. console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
  258. console.log(`\nUsing modules transform: ${modules.toString()}`);
  259. console.log("\nUsing plugins:");
  260. pluginNames.forEach(pluginName => {
  261. (0, _debug.logPluginOrPolyfill)(pluginName, targets, _pluginsCompatData.plugins);
  262. });
  263. if (!useBuiltIns) {
  264. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  265. } else {
  266. console.log(`\nUsing polyfills with \`${useBuiltIns}\` option:`);
  267. }
  268. }
  269. return {
  270. plugins
  271. };
  272. });
  273. exports.default = _default;