index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. }) => {
  85. const modulesPluginNames = [];
  86. if (modules !== false && transformations[modules]) {
  87. if (shouldTransformESM) {
  88. modulesPluginNames.push(transformations[modules]);
  89. }
  90. if (shouldTransformDynamicImport) {
  91. if (shouldTransformESM && modules !== "umd") {
  92. modulesPluginNames.push("transform-dynamic-import");
  93. } else {
  94. console.warn("Dynamic import can only be transformed when transforming ES" + " modules to AMD, CommonJS or SystemJS.");
  95. }
  96. }
  97. }
  98. if (shouldTransformExportNamespaceFrom) {
  99. modulesPluginNames.push("transform-export-namespace-from");
  100. }
  101. {
  102. if (!shouldTransformDynamicImport) {
  103. modulesPluginNames.push("syntax-dynamic-import");
  104. }
  105. if (!shouldTransformExportNamespaceFrom) {
  106. modulesPluginNames.push("syntax-export-namespace-from");
  107. }
  108. modulesPluginNames.push("syntax-top-level-await");
  109. modulesPluginNames.push("syntax-import-meta");
  110. }
  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. var _default = (0, _helperPluginUtils.declarePreset)((api, opts) => {
  200. api.assertVersion(7);
  201. const babelTargets = api.targets();
  202. const {
  203. bugfixes,
  204. configPath,
  205. debug,
  206. exclude: optionsExclude,
  207. forceAllTransforms,
  208. ignoreBrowserslistConfig,
  209. include: optionsInclude,
  210. loose,
  211. modules,
  212. shippedProposals,
  213. spec,
  214. targets: optionsTargets,
  215. useBuiltIns,
  216. corejs: {
  217. version: corejs,
  218. proposals
  219. },
  220. browserslistEnv
  221. } = (0, _normalizeOptions.default)(opts);
  222. let targets = babelTargets;
  223. if (_semver.lt(api.version, "7.13.0") || opts.targets || opts.configPath || opts.browserslistEnv || opts.ignoreBrowserslistConfig) {
  224. {
  225. var hasUglifyTarget = false;
  226. if (optionsTargets != null && optionsTargets.uglify) {
  227. hasUglifyTarget = true;
  228. delete optionsTargets.uglify;
  229. console.warn(`
  230. The uglify target has been deprecated. Set the top level
  231. option \`forceAllTransforms: true\` instead.
  232. `);
  233. }
  234. }
  235. targets = getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv);
  236. }
  237. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  238. const include = transformIncludesAndExcludes(optionsInclude);
  239. const exclude = transformIncludesAndExcludes(optionsExclude);
  240. const compatData = getPluginList(shippedProposals, bugfixes);
  241. const shouldSkipExportNamespaceFrom = modules === "auto" && (api.caller == null ? void 0 : api.caller(supportsExportNamespaceFrom)) || modules === false && !(0, _helperCompilationTargets.isRequired)("transform-export-namespace-from", transformTargets, {
  242. compatData,
  243. includes: include.plugins,
  244. excludes: exclude.plugins
  245. });
  246. const modulesPluginNames = getModulesPluginNames({
  247. modules,
  248. transformations: _moduleTransformations.default,
  249. shouldTransformESM: modules !== "auto" || !(api.caller != null && api.caller(supportsStaticESM)),
  250. shouldTransformDynamicImport: modules !== "auto" || !(api.caller != null && api.caller(supportsDynamicImport)),
  251. shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom
  252. });
  253. const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
  254. loose
  255. }), _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: ${modules.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. exports.default = _default;
  308. //# sourceMappingURL=index.js.map