index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 =
  61. _availablePlugins.default[pluginName]();
  62. if (!plugin) {
  63. throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
  64. }
  65. return plugin;
  66. };
  67. const transformIncludesAndExcludes = opts => {
  68. return opts.reduce((result, opt) => {
  69. const target = opt.match(/^(es|es6|es7|esnext|web)\./) ? "builtIns" : "plugins";
  70. result[target].add(opt);
  71. return result;
  72. }, {
  73. all: opts,
  74. plugins: new Set(),
  75. builtIns: new Set()
  76. });
  77. };
  78. exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
  79. const getModulesPluginNames = ({
  80. modules,
  81. transformations,
  82. shouldTransformESM,
  83. shouldTransformDynamicImport,
  84. shouldTransformExportNamespaceFrom,
  85. shouldParseTopLevelAwait
  86. }) => {
  87. const modulesPluginNames = [];
  88. if (modules !== false && transformations[modules]) {
  89. if (shouldTransformESM) {
  90. modulesPluginNames.push(transformations[modules]);
  91. }
  92. if (shouldTransformDynamicImport && shouldTransformESM && modules !== "umd") {
  93. modulesPluginNames.push("transform-dynamic-import");
  94. } else {
  95. if (shouldTransformDynamicImport) {
  96. console.warn("Dynamic import can only be supported when transforming ES modules" + " to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.");
  97. }
  98. modulesPluginNames.push("syntax-dynamic-import");
  99. }
  100. } else {
  101. modulesPluginNames.push("syntax-dynamic-import");
  102. }
  103. if (shouldTransformExportNamespaceFrom) {
  104. modulesPluginNames.push("transform-export-namespace-from");
  105. } else {
  106. modulesPluginNames.push("syntax-export-namespace-from");
  107. }
  108. if (shouldParseTopLevelAwait) {
  109. modulesPluginNames.push("syntax-top-level-await");
  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. };
  137. if (corejs) {
  138. if (useBuiltIns === "usage") {
  139. if (corejs.major === 2) {
  140. polyfillPlugins.push([pluginCoreJS2, pluginOptions], [_babelPolyfill.default, {
  141. usage: true
  142. }]);
  143. } else {
  144. polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babelPolyfill.default, {
  145. usage: true,
  146. deprecated: true
  147. }]);
  148. }
  149. if (regenerator) {
  150. polyfillPlugins.push([pluginRegenerator, {
  151. method: "usage-global",
  152. debug
  153. }]);
  154. }
  155. } else {
  156. if (corejs.major === 2) {
  157. polyfillPlugins.push([_babelPolyfill.default, {
  158. regenerator
  159. }], [pluginCoreJS2, pluginOptions]);
  160. } else {
  161. polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babelPolyfill.default, {
  162. deprecated: true
  163. }]);
  164. if (!regenerator) {
  165. polyfillPlugins.push([_regenerator.default, pluginOptions]);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. return polyfillPlugins;
  172. };
  173. exports.getPolyfillPlugins = getPolyfillPlugins;
  174. function getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv) {
  175. if (optionsTargets != null && optionsTargets.esmodules && optionsTargets.browsers) {
  176. console.warn(`
  177. @babel/preset-env: esmodules and browsers targets have been specified together.
  178. \`browsers\` target, \`${optionsTargets.browsers.toString()}\` will be ignored.
  179. `);
  180. }
  181. return (0, _helperCompilationTargets.default)(optionsTargets, {
  182. ignoreBrowserslistConfig,
  183. configPath,
  184. browserslistEnv
  185. });
  186. }
  187. function supportsStaticESM(caller) {
  188. return !!(caller != null && caller.supportsStaticESM);
  189. }
  190. function supportsDynamicImport(caller) {
  191. return !!(caller != null && caller.supportsDynamicImport);
  192. }
  193. function supportsExportNamespaceFrom(caller) {
  194. return !!(caller != null && caller.supportsExportNamespaceFrom);
  195. }
  196. function supportsTopLevelAwait(caller) {
  197. return !!(caller != null && caller.supportsTopLevelAwait);
  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 (
  224. _semver.lt(api.version, "7.13.0") ||
  225. opts.targets || opts.configPath || opts.browserslistEnv || opts.ignoreBrowserslistConfig) {
  226. {
  227. var hasUglifyTarget = false;
  228. if (optionsTargets != null && optionsTargets.uglify) {
  229. hasUglifyTarget = true;
  230. delete optionsTargets.uglify;
  231. console.warn(`
  232. The uglify target has been deprecated. Set the top level
  233. option \`forceAllTransforms: true\` instead.
  234. `);
  235. }
  236. }
  237. targets = getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv);
  238. }
  239. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  240. const include = transformIncludesAndExcludes(optionsInclude);
  241. const exclude = transformIncludesAndExcludes(optionsExclude);
  242. const compatData = getPluginList(shippedProposals, bugfixes);
  243. const shouldSkipExportNamespaceFrom = modules === "auto" && (api.caller == null ? void 0 : api.caller(supportsExportNamespaceFrom)) || modules === false && !(0, _helperCompilationTargets.isRequired)("transform-export-namespace-from", transformTargets, {
  244. compatData,
  245. includes: include.plugins,
  246. excludes: exclude.plugins
  247. });
  248. const modulesPluginNames = getModulesPluginNames({
  249. modules,
  250. transformations: _moduleTransformations.default,
  251. shouldTransformESM: modules !== "auto" || !(api.caller != null && api.caller(supportsStaticESM)),
  252. shouldTransformDynamicImport: modules !== "auto" || !(api.caller != null && api.caller(supportsDynamicImport)),
  253. shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom,
  254. shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait)
  255. });
  256. const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
  257. loose
  258. }), _shippedProposals.pluginSyntaxMap);
  259. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _pluginsCompatData.overlappingPlugins);
  260. (0, _filterItems.removeUnsupportedItems)(pluginNames, api.version);
  261. if (shippedProposals) {
  262. (0, _filterItems.addProposalSyntaxPlugins)(pluginNames, _shippedProposals.proposalSyntaxPlugins);
  263. }
  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. return [getPlugin(pluginName), {
  283. spec,
  284. loose,
  285. useBuiltIns: pluginUseBuiltIns
  286. }];
  287. }).concat(polyfillPlugins);
  288. if (debug) {
  289. console.log("@babel/preset-env: `DEBUG` option");
  290. console.log("\nUsing targets:");
  291. console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
  292. console.log(`\nUsing modules transform: ${modules.toString()}`);
  293. console.log("\nUsing plugins:");
  294. pluginNames.forEach(pluginName => {
  295. (0, _debug.logPlugin)(pluginName, targets, compatData);
  296. });
  297. if (!useBuiltIns) {
  298. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  299. }
  300. }
  301. return {
  302. plugins
  303. };
  304. });
  305. exports.default = _default;
  306. //# sourceMappingURL=index.js.map