index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.isPluginRequired = isPluginRequired;
  7. exports.transformIncludesAndExcludes = void 0;
  8. var _semver = require("semver");
  9. var _debug = require("./debug.js");
  10. var _getOptionSpecificExcludes = require("./get-option-specific-excludes.js");
  11. var _filterItems = require("./filter-items.js");
  12. var _moduleTransformations = require("./module-transformations.js");
  13. var _normalizeOptions = require("./normalize-options.js");
  14. var _shippedProposals = require("./shipped-proposals.js");
  15. var _pluginsCompatData = require("./plugins-compat-data.js");
  16. var _babelPluginPolyfillCorejs = require("babel-plugin-polyfill-corejs3");
  17. var _babel7Plugins = require("./polyfills/babel-7-plugins.cjs");
  18. var _helperCompilationTargets = require("@babel/helper-compilation-targets");
  19. var _availablePlugins = require("./available-plugins.js");
  20. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  21. const pluginCoreJS3 = _babelPluginPolyfillCorejs.default || _babelPluginPolyfillCorejs;
  22. function isPluginRequired(targets, support) {
  23. return (0, _helperCompilationTargets.isRequired)("fake-name", targets, {
  24. compatData: {
  25. "fake-name": support
  26. }
  27. });
  28. }
  29. function filterStageFromList(list, stageList) {
  30. return Object.keys(list).reduce((result, item) => {
  31. if (!stageList.has(item)) {
  32. result[item] = list[item];
  33. }
  34. return result;
  35. }, {});
  36. }
  37. const pluginLists = {
  38. withProposals: {
  39. withoutBugfixes: _pluginsCompatData.plugins,
  40. withBugfixes: Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes)
  41. },
  42. withoutProposals: {
  43. withoutBugfixes: filterStageFromList(_pluginsCompatData.plugins, _shippedProposals.proposalPlugins),
  44. withBugfixes: filterStageFromList(Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes), _shippedProposals.proposalPlugins)
  45. }
  46. };
  47. function getPluginList(proposals, bugfixes) {
  48. if (proposals) {
  49. if (bugfixes) return pluginLists.withProposals.withBugfixes;else return pluginLists.withProposals.withoutBugfixes;
  50. } else {
  51. if (bugfixes) return pluginLists.withoutProposals.withBugfixes;else return pluginLists.withoutProposals.withoutBugfixes;
  52. }
  53. }
  54. const getPlugin = pluginName => {
  55. const plugin = _availablePlugins.default[pluginName]();
  56. if (!plugin) {
  57. throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
  58. }
  59. return plugin;
  60. };
  61. const transformIncludesAndExcludes = opts => {
  62. return opts.reduce((result, opt) => {
  63. const target = opt.match(/^(es|es6|es7|esnext|web)\./) ? "builtIns" : "plugins";
  64. result[target].add(opt);
  65. return result;
  66. }, {
  67. all: opts,
  68. plugins: new Set(),
  69. builtIns: new Set()
  70. });
  71. };
  72. exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
  73. function getSpecialModulesPluginNames(modules, shouldTransformDynamicImport) {
  74. const modulesPluginNames = [];
  75. if (modules) {
  76. modulesPluginNames.push(_moduleTransformations.default[modules]);
  77. }
  78. if (shouldTransformDynamicImport) {
  79. if (modules && modules !== "umd") {
  80. modulesPluginNames.push("transform-dynamic-import");
  81. } else {
  82. console.warn("Dynamic import can only be transformed when transforming ES" + " modules to AMD, CommonJS or SystemJS.");
  83. }
  84. }
  85. {
  86. if (!shouldTransformDynamicImport) {
  87. modulesPluginNames.push("syntax-dynamic-import");
  88. }
  89. modulesPluginNames.push("syntax-top-level-await");
  90. modulesPluginNames.push("syntax-import-meta");
  91. }
  92. return modulesPluginNames;
  93. }
  94. const getCoreJSOptions = ({
  95. useBuiltIns,
  96. corejs,
  97. polyfillTargets,
  98. include,
  99. exclude,
  100. proposals,
  101. shippedProposals,
  102. debug
  103. }) => ({
  104. method: `${useBuiltIns}-global`,
  105. version: corejs ? corejs.toString() : undefined,
  106. targets: polyfillTargets,
  107. include,
  108. exclude,
  109. proposals,
  110. shippedProposals,
  111. debug,
  112. "#__secret_key__@babel/preset-env__compatibility": {
  113. noRuntimeName: true
  114. }
  115. });
  116. {
  117. var getPolyfillPlugins = ({
  118. useBuiltIns,
  119. corejs,
  120. polyfillTargets,
  121. include,
  122. exclude,
  123. proposals,
  124. shippedProposals,
  125. regenerator,
  126. debug
  127. }) => {
  128. const polyfillPlugins = [];
  129. if (useBuiltIns === "usage" || useBuiltIns === "entry") {
  130. const pluginOptions = getCoreJSOptions({
  131. useBuiltIns,
  132. corejs,
  133. polyfillTargets,
  134. include,
  135. exclude,
  136. proposals,
  137. shippedProposals,
  138. debug
  139. });
  140. if (corejs) {
  141. {
  142. if (useBuiltIns === "usage") {
  143. if (corejs.major === 2) {
  144. polyfillPlugins.push([_babel7Plugins.pluginCoreJS2, pluginOptions], [_babel7Plugins.legacyBabelPolyfillPlugin, {
  145. usage: true
  146. }]);
  147. } else {
  148. polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babel7Plugins.legacyBabelPolyfillPlugin, {
  149. usage: true,
  150. deprecated: true
  151. }]);
  152. }
  153. if (regenerator) {
  154. polyfillPlugins.push([_babel7Plugins.pluginRegenerator, {
  155. method: "usage-global",
  156. debug
  157. }]);
  158. }
  159. } else {
  160. if (corejs.major === 2) {
  161. polyfillPlugins.push([_babel7Plugins.legacyBabelPolyfillPlugin, {
  162. regenerator
  163. }], [_babel7Plugins.pluginCoreJS2, pluginOptions]);
  164. } else {
  165. polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babel7Plugins.legacyBabelPolyfillPlugin, {
  166. deprecated: true
  167. }]);
  168. if (!regenerator) {
  169. polyfillPlugins.push([_babel7Plugins.removeRegeneratorEntryPlugin, pluginOptions]);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. return polyfillPlugins;
  177. };
  178. {
  179. exports.getPolyfillPlugins = getPolyfillPlugins;
  180. }
  181. }
  182. function getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv) {
  183. if (optionsTargets != null && optionsTargets.esmodules && optionsTargets.browsers) {
  184. console.warn(`
  185. @babel/preset-env: esmodules and browsers targets have been specified together.
  186. \`browsers\` target, \`${optionsTargets.browsers.toString()}\` will be ignored.
  187. `);
  188. }
  189. return (0, _helperCompilationTargets.default)(optionsTargets, {
  190. ignoreBrowserslistConfig,
  191. configPath,
  192. browserslistEnv
  193. });
  194. }
  195. function supportsStaticESM(caller) {
  196. return !!(caller != null && caller.supportsStaticESM);
  197. }
  198. function supportsDynamicImport(caller) {
  199. return !!(caller != null && caller.supportsDynamicImport);
  200. }
  201. function supportsExportNamespaceFrom(caller) {
  202. return !!(caller != null && caller.supportsExportNamespaceFrom);
  203. }
  204. var _default = exports.default = (0, _helperPluginUtils.declarePreset)((api, opts) => {
  205. api.assertVersion(7);
  206. const babelTargets = api.targets();
  207. const {
  208. bugfixes,
  209. configPath,
  210. debug,
  211. exclude: optionsExclude,
  212. forceAllTransforms,
  213. ignoreBrowserslistConfig,
  214. include: optionsInclude,
  215. loose,
  216. modules: optionsModules,
  217. shippedProposals,
  218. spec,
  219. targets: optionsTargets,
  220. useBuiltIns,
  221. corejs: {
  222. version: corejs,
  223. proposals
  224. },
  225. browserslistEnv
  226. } = (0, _normalizeOptions.default)(opts);
  227. let targets = babelTargets;
  228. if (_semver.lt(api.version, "7.13.0") || opts.targets || opts.configPath || opts.browserslistEnv || opts.ignoreBrowserslistConfig) {
  229. {
  230. var hasUglifyTarget = false;
  231. if (optionsTargets != null && optionsTargets.uglify) {
  232. hasUglifyTarget = true;
  233. delete optionsTargets.uglify;
  234. console.warn(`
  235. The uglify target has been deprecated. Set the top level
  236. option \`forceAllTransforms: true\` instead.
  237. `);
  238. }
  239. }
  240. targets = getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv);
  241. }
  242. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  243. const include = transformIncludesAndExcludes(optionsInclude);
  244. const exclude = transformIncludesAndExcludes(optionsExclude);
  245. const compatData = getPluginList(shippedProposals, bugfixes);
  246. const modules = optionsModules === "auto" ? api.caller(supportsStaticESM) ? false : "commonjs" : optionsModules;
  247. const shouldTransformDynamicImport = optionsModules === "auto" ? !api.caller(supportsDynamicImport) : !!modules;
  248. if (optionsModules === "auto" && !api.caller(supportsExportNamespaceFrom) && !exclude.plugins.has("transform-export-namespace-from")) {
  249. include.plugins.add("transform-export-namespace-from");
  250. }
  251. const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, getSpecialModulesPluginNames(modules, shouldTransformDynamicImport), (0, _getOptionSpecificExcludes.default)({
  252. loose
  253. }), _shippedProposals.pluginSyntaxMap);
  254. if (shippedProposals) {
  255. (0, _filterItems.addProposalSyntaxPlugins)(pluginNames, _shippedProposals.proposalSyntaxPlugins);
  256. }
  257. (0, _filterItems.removeUnsupportedItems)(pluginNames, api.version);
  258. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _pluginsCompatData.overlappingPlugins);
  259. const polyfillPlugins = getPolyfillPlugins({
  260. useBuiltIns,
  261. corejs,
  262. polyfillTargets: targets,
  263. include: include.builtIns,
  264. exclude: exclude.builtIns,
  265. proposals,
  266. shippedProposals,
  267. regenerator: pluginNames.has("transform-regenerator"),
  268. debug
  269. });
  270. const pluginUseBuiltIns = useBuiltIns !== false;
  271. const plugins = Array.from(pluginNames).map(pluginName => {
  272. if (pluginName === "transform-class-properties" || pluginName === "transform-private-methods" || pluginName === "transform-private-property-in-object") {
  273. return [getPlugin(pluginName), {
  274. 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"
  275. }];
  276. }
  277. if (pluginName === "syntax-import-attributes") {
  278. return [getPlugin(pluginName), {
  279. deprecatedAssertSyntax: true
  280. }];
  281. }
  282. return [getPlugin(pluginName), {
  283. spec,
  284. loose,
  285. useBuiltIns: pluginUseBuiltIns
  286. }];
  287. }).concat(polyfillPlugins);
  288. if (debug) {
  289. console.log("@babel/preset-env: `DEBUG` option");
  290. console.log("\nUsing targets:");
  291. console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
  292. console.log(`\nUsing modules transform: ${optionsModules.toString()}`);
  293. console.log("\nUsing plugins:");
  294. pluginNames.forEach(pluginName => {
  295. (0, _debug.logPlugin)(pluginName, targets, compatData);
  296. });
  297. if (!useBuiltIns) {
  298. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  299. }
  300. }
  301. return {
  302. plugins
  303. };
  304. });
  305. {
  306. exports.getModulesPluginNames = ({
  307. modules,
  308. transformations,
  309. shouldTransformESM,
  310. shouldTransformDynamicImport,
  311. shouldTransformExportNamespaceFrom
  312. }) => {
  313. const modulesPluginNames = [];
  314. if (modules !== false && transformations[modules]) {
  315. if (shouldTransformESM) {
  316. modulesPluginNames.push(transformations[modules]);
  317. }
  318. if (shouldTransformDynamicImport) {
  319. if (shouldTransformESM && modules !== "umd") {
  320. modulesPluginNames.push("transform-dynamic-import");
  321. } else {
  322. console.warn("Dynamic import can only be transformed when transforming ES" + " modules to AMD, CommonJS or SystemJS.");
  323. }
  324. }
  325. }
  326. if (shouldTransformExportNamespaceFrom) {
  327. modulesPluginNames.push("transform-export-namespace-from");
  328. }
  329. if (!shouldTransformDynamicImport) {
  330. modulesPluginNames.push("syntax-dynamic-import");
  331. }
  332. if (!shouldTransformExportNamespaceFrom) {
  333. modulesPluginNames.push("syntax-export-namespace-from");
  334. }
  335. modulesPluginNames.push("syntax-top-level-await");
  336. modulesPluginNames.push("syntax-import-meta");
  337. return modulesPluginNames;
  338. };
  339. }
  340. //# sourceMappingURL=index.js.map