index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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("../data/shipped-proposals");
  15. var _pluginsCompatData = require("./plugins-compat-data");
  16. var _overlappingPlugins = require("@babel/compat-data/overlapping-plugins");
  17. var _regenerator = require("./polyfills/regenerator");
  18. var _babelPolyfill = require("./polyfills/babel-polyfill");
  19. var _babelPluginPolyfillCorejs = require("babel-plugin-polyfill-corejs2");
  20. var _babelPluginPolyfillCorejs2 = require("babel-plugin-polyfill-corejs3");
  21. var _babelPluginPolyfillRegenerator = require("babel-plugin-polyfill-regenerator");
  22. var _helperCompilationTargets = require("@babel/helper-compilation-targets");
  23. var _availablePlugins = require("./available-plugins");
  24. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  25. const pluginCoreJS2 = _babelPluginPolyfillCorejs.default || _babelPluginPolyfillCorejs;
  26. const pluginCoreJS3 = _babelPluginPolyfillCorejs2.default || _babelPluginPolyfillCorejs2;
  27. const pluginRegenerator = _babelPluginPolyfillRegenerator.default || _babelPluginPolyfillRegenerator;
  28. function isPluginRequired(targets, support) {
  29. return (0, _helperCompilationTargets.isRequired)("fake-name", targets, {
  30. compatData: {
  31. "fake-name": support
  32. }
  33. });
  34. }
  35. function filterStageFromList(list, stageList) {
  36. return Object.keys(list).reduce((result, item) => {
  37. if (!stageList.has(item)) {
  38. result[item] = list[item];
  39. }
  40. return result;
  41. }, {});
  42. }
  43. const pluginLists = {
  44. withProposals: {
  45. withoutBugfixes: _pluginsCompatData.plugins,
  46. withBugfixes: Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes)
  47. },
  48. withoutProposals: {
  49. withoutBugfixes: filterStageFromList(_pluginsCompatData.plugins, _shippedProposals.proposalPlugins),
  50. withBugfixes: filterStageFromList(Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes), _shippedProposals.proposalPlugins)
  51. }
  52. };
  53. function getPluginList(proposals, bugfixes) {
  54. if (proposals) {
  55. if (bugfixes) return pluginLists.withProposals.withBugfixes;else return pluginLists.withProposals.withoutBugfixes;
  56. } else {
  57. if (bugfixes) return pluginLists.withoutProposals.withBugfixes;else return pluginLists.withoutProposals.withoutBugfixes;
  58. }
  59. }
  60. const getPlugin = pluginName => {
  61. const plugin = _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("proposal-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("proposal-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.declare)((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 ((0, _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)("proposal-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. shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait)
  253. });
  254. const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
  255. loose
  256. }), _shippedProposals.pluginSyntaxMap);
  257. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _overlappingPlugins);
  258. (0, _filterItems.removeUnsupportedItems)(pluginNames, api.version);
  259. const polyfillPlugins = getPolyfillPlugins({
  260. useBuiltIns,
  261. corejs,
  262. polyfillTargets: targets,
  263. include: include.builtIns,
  264. exclude: exclude.builtIns,
  265. proposals,
  266. shippedProposals,
  267. regenerator: pluginNames.has("transform-regenerator"),
  268. debug
  269. });
  270. const pluginUseBuiltIns = useBuiltIns !== false;
  271. const plugins = Array.from(pluginNames).map(pluginName => {
  272. if (pluginName === "proposal-class-properties" || pluginName === "proposal-private-methods" || pluginName === "proposal-private-property-in-object") {
  273. return [getPlugin(pluginName), {
  274. 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"
  275. }];
  276. }
  277. return [getPlugin(pluginName), {
  278. spec,
  279. loose,
  280. useBuiltIns: pluginUseBuiltIns
  281. }];
  282. }).concat(polyfillPlugins);
  283. if (debug) {
  284. console.log("@babel/preset-env: `DEBUG` option");
  285. console.log("\nUsing targets:");
  286. console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
  287. console.log(`\nUsing modules transform: ${modules.toString()}`);
  288. console.log("\nUsing plugins:");
  289. pluginNames.forEach(pluginName => {
  290. (0, _debug.logPlugin)(pluginName, targets, compatData);
  291. });
  292. if (!useBuiltIns) {
  293. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  294. }
  295. }
  296. return {
  297. plugins
  298. };
  299. });
  300. exports.default = _default;