index.js 11 KB

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