index.js 12 KB

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