index.js 11 KB

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