index.js 12 KB

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