You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
12 KiB

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