index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isPluginRequired = isPluginRequired;
  6. exports.default = exports.getPolyfillPlugins = exports.getModulesPluginNames = exports.transformIncludesAndExcludes = void 0;
  7. var _semver = require("semver");
  8. var _debug = require("./debug");
  9. var _getOptionSpecificExcludes = require("./get-option-specific-excludes");
  10. var _filterItems = require("./filter-items");
  11. var _moduleTransformations = require("./module-transformations");
  12. var _normalizeOptions = require("./normalize-options");
  13. var _shippedProposals = require("../data/shipped-proposals");
  14. var _pluginsCompatData = require("./plugins-compat-data");
  15. var _overlappingPlugins = require("@babel/compat-data/overlapping-plugins");
  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("proposal-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("proposal-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. return modulesPluginNames;
  111. };
  112. exports.getModulesPluginNames = getModulesPluginNames;
  113. const getPolyfillPlugins = ({
  114. useBuiltIns,
  115. corejs,
  116. polyfillTargets,
  117. include,
  118. exclude,
  119. proposals,
  120. shippedProposals,
  121. regenerator,
  122. debug
  123. }) => {
  124. const polyfillPlugins = [];
  125. if (useBuiltIns === "usage" || useBuiltIns === "entry") {
  126. const pluginOptions = {
  127. method: `${useBuiltIns}-global`,
  128. version: corejs ? corejs.toString() : undefined,
  129. targets: polyfillTargets,
  130. include,
  131. exclude,
  132. proposals,
  133. shippedProposals,
  134. debug
  135. };
  136. if (corejs) {
  137. if (useBuiltIns === "usage") {
  138. if (corejs.major === 2) {
  139. polyfillPlugins.push([pluginCoreJS2, pluginOptions], [_babelPolyfill.default, {
  140. usage: true
  141. }]);
  142. } else {
  143. polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babelPolyfill.default, {
  144. usage: true,
  145. deprecated: true
  146. }]);
  147. }
  148. if (regenerator) {
  149. polyfillPlugins.push([pluginRegenerator, {
  150. method: "usage-global",
  151. debug
  152. }]);
  153. }
  154. } else {
  155. if (corejs.major === 2) {
  156. polyfillPlugins.push([_babelPolyfill.default, {
  157. regenerator
  158. }], [pluginCoreJS2, pluginOptions]);
  159. } else {
  160. polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babelPolyfill.default, {
  161. deprecated: true
  162. }]);
  163. if (!regenerator) {
  164. polyfillPlugins.push([_regenerator.default, pluginOptions]);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. return polyfillPlugins;
  171. };
  172. exports.getPolyfillPlugins = getPolyfillPlugins;
  173. function getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv) {
  174. if (optionsTargets != null && optionsTargets.esmodules && optionsTargets.browsers) {
  175. console.warn(`
  176. @babel/preset-env: esmodules and browsers targets have been specified together.
  177. \`browsers\` target, \`${optionsTargets.browsers.toString()}\` will be ignored.
  178. `);
  179. }
  180. return (0, _helperCompilationTargets.default)(optionsTargets, {
  181. ignoreBrowserslistConfig,
  182. configPath,
  183. browserslistEnv
  184. });
  185. }
  186. function supportsStaticESM(caller) {
  187. return !!(caller != null && caller.supportsStaticESM);
  188. }
  189. function supportsDynamicImport(caller) {
  190. return !!(caller != null && caller.supportsDynamicImport);
  191. }
  192. function supportsExportNamespaceFrom(caller) {
  193. return !!(caller != null && caller.supportsExportNamespaceFrom);
  194. }
  195. function supportsTopLevelAwait(caller) {
  196. return !!(caller != null && caller.supportsTopLevelAwait);
  197. }
  198. var _default = (0, _helperPluginUtils.declare)((api, opts) => {
  199. api.assertVersion(7);
  200. const babelTargets = api.targets();
  201. const {
  202. bugfixes,
  203. configPath,
  204. debug,
  205. exclude: optionsExclude,
  206. forceAllTransforms,
  207. ignoreBrowserslistConfig,
  208. include: optionsInclude,
  209. loose,
  210. modules,
  211. shippedProposals,
  212. spec,
  213. targets: optionsTargets,
  214. useBuiltIns,
  215. corejs: {
  216. version: corejs,
  217. proposals
  218. },
  219. browserslistEnv
  220. } = (0, _normalizeOptions.default)(opts);
  221. let targets = babelTargets;
  222. if ((0, _semver.lt)(api.version, "7.13.0") || opts.targets || opts.configPath || opts.browserslistEnv || opts.ignoreBrowserslistConfig) {
  223. {
  224. var hasUglifyTarget = false;
  225. if (optionsTargets != null && optionsTargets.uglify) {
  226. hasUglifyTarget = true;
  227. delete optionsTargets.uglify;
  228. console.warn(`
  229. The uglify target has been deprecated. Set the top level
  230. option \`forceAllTransforms: true\` instead.
  231. `);
  232. }
  233. }
  234. targets = getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv);
  235. }
  236. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  237. const include = transformIncludesAndExcludes(optionsInclude);
  238. const exclude = transformIncludesAndExcludes(optionsExclude);
  239. const compatData = getPluginList(shippedProposals, bugfixes);
  240. const shouldSkipExportNamespaceFrom = modules === "auto" && (api.caller == null ? void 0 : api.caller(supportsExportNamespaceFrom)) || modules === false && !(0, _helperCompilationTargets.isRequired)("proposal-export-namespace-from", transformTargets, {
  241. compatData,
  242. includes: include.plugins,
  243. excludes: exclude.plugins
  244. });
  245. const modulesPluginNames = getModulesPluginNames({
  246. modules,
  247. transformations: _moduleTransformations.default,
  248. shouldTransformESM: modules !== "auto" || !(api.caller != null && api.caller(supportsStaticESM)),
  249. shouldTransformDynamicImport: modules !== "auto" || !(api.caller != null && api.caller(supportsDynamicImport)),
  250. shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom,
  251. shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait)
  252. });
  253. const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
  254. loose
  255. }), _shippedProposals.pluginSyntaxMap);
  256. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _overlappingPlugins);
  257. (0, _filterItems.removeUnsupportedItems)(pluginNames, api.version);
  258. const polyfillPlugins = getPolyfillPlugins({
  259. useBuiltIns,
  260. corejs,
  261. polyfillTargets: targets,
  262. include: include.builtIns,
  263. exclude: exclude.builtIns,
  264. proposals,
  265. shippedProposals,
  266. regenerator: pluginNames.has("transform-regenerator"),
  267. debug
  268. });
  269. const pluginUseBuiltIns = useBuiltIns !== false;
  270. const plugins = Array.from(pluginNames).map(pluginName => {
  271. if (pluginName === "proposal-class-properties" || pluginName === "proposal-private-methods" || pluginName === "proposal-private-property-in-object") {
  272. return [getPlugin(pluginName), {
  273. 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"
  274. }];
  275. }
  276. return [getPlugin(pluginName), {
  277. spec,
  278. loose,
  279. useBuiltIns: pluginUseBuiltIns
  280. }];
  281. }).concat(polyfillPlugins);
  282. if (debug) {
  283. console.log("@babel/preset-env: `DEBUG` option");
  284. console.log("\nUsing targets:");
  285. console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
  286. console.log(`\nUsing modules transform: ${modules.toString()}`);
  287. console.log("\nUsing plugins:");
  288. pluginNames.forEach(pluginName => {
  289. (0, _debug.logPlugin)(pluginName, targets, compatData);
  290. });
  291. if (!useBuiltIns) {
  292. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  293. }
  294. }
  295. return {
  296. plugins
  297. };
  298. });
  299. exports.default = _default;