index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 = /^(?:es|es6|es7|esnext|web)\./.test(opt) ? "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, babelVersion) {
  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. if (babelVersion[0] !== "8") {
  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, api) {
  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. onBrowserslistConfigFound(config) {
  193. api.addExternalDependency(config);
  194. }
  195. });
  196. }
  197. function supportsStaticESM(caller) {
  198. return !!(caller != null && caller.supportsStaticESM);
  199. }
  200. function supportsDynamicImport(caller) {
  201. return !!(caller != null && caller.supportsDynamicImport);
  202. }
  203. function supportsExportNamespaceFrom(caller) {
  204. return !!(caller != null && caller.supportsExportNamespaceFrom);
  205. }
  206. var _default = exports.default = (0, _helperPluginUtils.declarePreset)((api, opts) => {
  207. api.assertVersion(7);
  208. const babelTargets = api.targets();
  209. ;
  210. const {
  211. bugfixes,
  212. configPath,
  213. debug,
  214. exclude: optionsExclude,
  215. forceAllTransforms,
  216. ignoreBrowserslistConfig,
  217. include: optionsInclude,
  218. modules: optionsModules,
  219. shippedProposals,
  220. targets: optionsTargets,
  221. useBuiltIns,
  222. corejs: {
  223. version: corejs,
  224. proposals
  225. },
  226. browserslistEnv
  227. } = (0, _normalizeOptions.default)(opts);
  228. {
  229. var {
  230. loose,
  231. spec = false
  232. } = opts;
  233. }
  234. let targets = babelTargets;
  235. if (_semver.lt(api.version, "7.13.0") || opts.targets || opts.configPath || opts.browserslistEnv || opts.ignoreBrowserslistConfig) {
  236. {
  237. var hasUglifyTarget = false;
  238. if (optionsTargets != null && optionsTargets.uglify) {
  239. hasUglifyTarget = true;
  240. delete optionsTargets.uglify;
  241. console.warn(`
  242. The uglify target has been deprecated. Set the top level
  243. option \`forceAllTransforms: true\` instead.
  244. `);
  245. }
  246. }
  247. targets = getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv, api);
  248. }
  249. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  250. const include = transformIncludesAndExcludes(optionsInclude);
  251. const exclude = transformIncludesAndExcludes(optionsExclude);
  252. const compatData = getPluginList(shippedProposals, bugfixes);
  253. const modules = optionsModules === "auto" ? api.caller(supportsStaticESM) ? false : "commonjs" : optionsModules;
  254. const shouldTransformDynamicImport = optionsModules === "auto" ? !api.caller(supportsDynamicImport) : !!modules;
  255. if (!exclude.plugins.has("transform-export-namespace-from") && (optionsModules === "auto" ? !api.caller(supportsExportNamespaceFrom) : !!modules)) {
  256. include.plugins.add("transform-export-namespace-from");
  257. }
  258. const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, getSpecialModulesPluginNames(modules, shouldTransformDynamicImport, api.version), !loose ? undefined : ["transform-typeof-symbol"], _shippedProposals.pluginSyntaxMap);
  259. if (shippedProposals) {
  260. (0, _filterItems.addProposalSyntaxPlugins)(pluginNames, _shippedProposals.proposalSyntaxPlugins);
  261. }
  262. (0, _filterItems.removeUnsupportedItems)(pluginNames, api.version);
  263. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _pluginsCompatData.overlappingPlugins);
  264. const polyfillPlugins = getPolyfillPlugins({
  265. useBuiltIns,
  266. corejs,
  267. polyfillTargets: targets,
  268. include: include.builtIns,
  269. exclude: exclude.builtIns,
  270. proposals,
  271. shippedProposals,
  272. regenerator: pluginNames.has("transform-regenerator"),
  273. debug
  274. });
  275. const pluginUseBuiltIns = useBuiltIns !== false;
  276. const plugins = Array.from(pluginNames).map(pluginName => {
  277. if (pluginName === "transform-class-properties" || pluginName === "transform-private-methods" || pluginName === "transform-private-property-in-object") {
  278. return [getPlugin(pluginName), {
  279. 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"
  280. }];
  281. }
  282. if (pluginName === "syntax-import-attributes") {
  283. return [getPlugin(pluginName), {
  284. deprecatedAssertSyntax: true
  285. }];
  286. }
  287. return [getPlugin(pluginName), {
  288. spec,
  289. loose,
  290. useBuiltIns: pluginUseBuiltIns
  291. }];
  292. }).concat(polyfillPlugins);
  293. if (debug) {
  294. console.log("@babel/preset-env: `DEBUG` option");
  295. console.log("\nUsing targets:");
  296. console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
  297. console.log(`\nUsing modules transform: ${optionsModules.toString()}`);
  298. console.log("\nUsing plugins:");
  299. pluginNames.forEach(pluginName => {
  300. (0, _debug.logPlugin)(pluginName, targets, compatData);
  301. });
  302. if (!useBuiltIns) {
  303. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  304. }
  305. }
  306. return {
  307. plugins
  308. };
  309. });
  310. {
  311. exports.getModulesPluginNames = ({
  312. modules,
  313. transformations,
  314. shouldTransformESM,
  315. shouldTransformDynamicImport,
  316. shouldTransformExportNamespaceFrom
  317. }) => {
  318. const modulesPluginNames = [];
  319. if (modules !== false && transformations[modules]) {
  320. if (shouldTransformESM) {
  321. modulesPluginNames.push(transformations[modules]);
  322. }
  323. if (shouldTransformDynamicImport) {
  324. if (shouldTransformESM && modules !== "umd") {
  325. modulesPluginNames.push("transform-dynamic-import");
  326. } else {
  327. console.warn("Dynamic import can only be transformed when transforming ES" + " modules to AMD, CommonJS or SystemJS.");
  328. }
  329. }
  330. }
  331. if (shouldTransformExportNamespaceFrom) {
  332. modulesPluginNames.push("transform-export-namespace-from");
  333. }
  334. if (!shouldTransformDynamicImport) {
  335. modulesPluginNames.push("syntax-dynamic-import");
  336. }
  337. if (!shouldTransformExportNamespaceFrom) {
  338. modulesPluginNames.push("syntax-export-namespace-from");
  339. }
  340. modulesPluginNames.push("syntax-top-level-await");
  341. modulesPluginNames.push("syntax-import-meta");
  342. return modulesPluginNames;
  343. };
  344. }
  345. //# sourceMappingURL=index.js.map