You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
9.3 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "isPluginRequired", {
  6. enumerable: true,
  7. get: function () {
  8. return _filterItems.isPluginRequired;
  9. }
  10. });
  11. exports.default = exports.getPolyfillPlugins = exports.getModulesPluginNames = exports.transformIncludesAndExcludes = void 0;
  12. var _semver = require("semver");
  13. var _debug = require("./debug");
  14. var _getOptionSpecificExcludes = _interopRequireDefault(require("./get-option-specific-excludes"));
  15. var _filterItems = _interopRequireWildcard(require("./filter-items"));
  16. var _moduleTransformations = _interopRequireDefault(require("./module-transformations"));
  17. var _normalizeOptions = _interopRequireDefault(require("./normalize-options"));
  18. var _plugins = _interopRequireDefault(require("../data/plugins.json"));
  19. var _shippedProposals = require("../data/shipped-proposals");
  20. var _overlappingPlugins = _interopRequireDefault(require("../data/overlapping-plugins"));
  21. var _usagePlugin = _interopRequireDefault(require("./polyfills/corejs2/usage-plugin"));
  22. var _usagePlugin2 = _interopRequireDefault(require("./polyfills/corejs3/usage-plugin"));
  23. var _usagePlugin3 = _interopRequireDefault(require("./polyfills/regenerator/usage-plugin"));
  24. var _entryPlugin = _interopRequireDefault(require("./polyfills/corejs2/entry-plugin"));
  25. var _entryPlugin2 = _interopRequireDefault(require("./polyfills/corejs3/entry-plugin"));
  26. var _entryPlugin3 = _interopRequireDefault(require("./polyfills/regenerator/entry-plugin"));
  27. var _targetsParser = _interopRequireDefault(require("./targets-parser"));
  28. var _availablePlugins = _interopRequireDefault(require("./available-plugins"));
  29. var _utils = require("./utils");
  30. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  31. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  32. 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; }
  33. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  34. const pluginListWithoutProposals = (0, _utils.filterStageFromList)(_plugins.default, _shippedProposals.proposalPlugins);
  35. const getPlugin = pluginName => {
  36. const plugin = _availablePlugins.default[pluginName];
  37. if (!plugin) {
  38. throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
  39. }
  40. return plugin;
  41. };
  42. const transformIncludesAndExcludes = opts => {
  43. return opts.reduce((result, opt) => {
  44. const target = opt.match(/^(es|es6|es7|esnext|web)\./) ? "builtIns" : "plugins";
  45. result[target].add(opt);
  46. return result;
  47. }, {
  48. all: opts,
  49. plugins: new Set(),
  50. builtIns: new Set()
  51. });
  52. };
  53. exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
  54. const getModulesPluginNames = ({
  55. modules,
  56. transformations,
  57. shouldTransformESM,
  58. shouldTransformDynamicImport,
  59. shouldParseTopLevelAwait
  60. }) => {
  61. const modulesPluginNames = [];
  62. if (modules !== false && transformations[modules]) {
  63. if (shouldTransformESM) {
  64. modulesPluginNames.push(transformations[modules]);
  65. }
  66. if (shouldTransformDynamicImport && shouldTransformESM && modules !== "umd") {
  67. modulesPluginNames.push("proposal-dynamic-import");
  68. } else {
  69. if (shouldTransformDynamicImport) {
  70. console.warn("Dynamic import can only be supported when transforming ES modules" + " to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.");
  71. }
  72. modulesPluginNames.push("syntax-dynamic-import");
  73. }
  74. } else {
  75. modulesPluginNames.push("syntax-dynamic-import");
  76. }
  77. if (shouldParseTopLevelAwait) {
  78. modulesPluginNames.push("syntax-top-level-await");
  79. }
  80. return modulesPluginNames;
  81. };
  82. exports.getModulesPluginNames = getModulesPluginNames;
  83. const getPolyfillPlugins = ({
  84. useBuiltIns,
  85. corejs,
  86. polyfillTargets,
  87. include,
  88. exclude,
  89. proposals,
  90. shippedProposals,
  91. regenerator,
  92. debug
  93. }) => {
  94. const polyfillPlugins = [];
  95. if (useBuiltIns === "usage" || useBuiltIns === "entry") {
  96. const pluginOptions = {
  97. corejs,
  98. polyfillTargets,
  99. include,
  100. exclude,
  101. proposals,
  102. shippedProposals,
  103. regenerator,
  104. debug
  105. };
  106. if (corejs) {
  107. if (useBuiltIns === "usage") {
  108. if (corejs.major === 2) {
  109. polyfillPlugins.push([_usagePlugin.default, pluginOptions]);
  110. } else {
  111. polyfillPlugins.push([_usagePlugin2.default, pluginOptions]);
  112. }
  113. if (regenerator) {
  114. polyfillPlugins.push([_usagePlugin3.default, pluginOptions]);
  115. }
  116. } else {
  117. if (corejs.major === 2) {
  118. polyfillPlugins.push([_entryPlugin.default, pluginOptions]);
  119. } else {
  120. polyfillPlugins.push([_entryPlugin2.default, pluginOptions]);
  121. if (!regenerator) {
  122. polyfillPlugins.push([_entryPlugin3.default, pluginOptions]);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. return polyfillPlugins;
  129. };
  130. exports.getPolyfillPlugins = getPolyfillPlugins;
  131. function supportsStaticESM(caller) {
  132. return !!(caller && caller.supportsStaticESM);
  133. }
  134. function supportsDynamicImport(caller) {
  135. return !!(caller && caller.supportsDynamicImport);
  136. }
  137. function supportsTopLevelAwait(caller) {
  138. return !!(caller && caller.supportsTopLevelAwait);
  139. }
  140. var _default = (0, _helperPluginUtils.declare)((api, opts) => {
  141. api.assertVersion(7);
  142. const {
  143. configPath,
  144. debug,
  145. exclude: optionsExclude,
  146. forceAllTransforms,
  147. ignoreBrowserslistConfig,
  148. include: optionsInclude,
  149. loose,
  150. modules,
  151. shippedProposals,
  152. spec,
  153. targets: optionsTargets,
  154. useBuiltIns,
  155. corejs: {
  156. version: corejs,
  157. proposals
  158. }
  159. } = (0, _normalizeOptions.default)(opts);
  160. let hasUglifyTarget = false;
  161. if (optionsTargets && optionsTargets.uglify) {
  162. hasUglifyTarget = true;
  163. delete optionsTargets.uglify;
  164. console.log("");
  165. console.log("The uglify target has been deprecated. Set the top level");
  166. console.log("option `forceAllTransforms: true` instead.");
  167. console.log("");
  168. }
  169. if (optionsTargets && optionsTargets.esmodules && optionsTargets.browsers) {
  170. console.log("");
  171. console.log("@babel/preset-env: esmodules and browsers targets have been specified together.");
  172. console.log(`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`);
  173. console.log("");
  174. }
  175. const targets = (0, _targetsParser.default)(optionsTargets, {
  176. ignoreBrowserslistConfig,
  177. configPath
  178. });
  179. const include = transformIncludesAndExcludes(optionsInclude);
  180. const exclude = transformIncludesAndExcludes(optionsExclude);
  181. const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
  182. const modulesPluginNames = getModulesPluginNames({
  183. modules,
  184. transformations: _moduleTransformations.default,
  185. shouldTransformESM: modules !== "auto" || !api.caller || !api.caller(supportsStaticESM),
  186. shouldTransformDynamicImport: modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport),
  187. shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait)
  188. });
  189. const pluginNames = (0, _filterItems.default)(shippedProposals ? _plugins.default : pluginListWithoutProposals, include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
  190. loose
  191. }), _shippedProposals.pluginSyntaxMap);
  192. (0, _filterItems.removeUnnecessaryItems)(pluginNames, _overlappingPlugins.default);
  193. const polyfillPlugins = getPolyfillPlugins({
  194. useBuiltIns,
  195. corejs,
  196. polyfillTargets: targets,
  197. include: include.builtIns,
  198. exclude: exclude.builtIns,
  199. proposals,
  200. shippedProposals,
  201. regenerator: pluginNames.has("transform-regenerator"),
  202. debug
  203. });
  204. const pluginUseBuiltIns = useBuiltIns !== false;
  205. const plugins = Array.from(pluginNames).map(pluginName => [getPlugin(pluginName), {
  206. spec,
  207. loose,
  208. useBuiltIns: pluginUseBuiltIns
  209. }]).concat(polyfillPlugins);
  210. if (debug) {
  211. console.log("@babel/preset-env: `DEBUG` option");
  212. console.log("\nUsing targets:");
  213. console.log(JSON.stringify((0, _utils.prettifyTargets)(targets), null, 2));
  214. console.log(`\nUsing modules transform: ${modules.toString()}`);
  215. console.log("\nUsing plugins:");
  216. pluginNames.forEach(pluginName => {
  217. (0, _debug.logPluginOrPolyfill)(pluginName, targets, _plugins.default);
  218. });
  219. if (!useBuiltIns) {
  220. console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
  221. } else {
  222. console.log(`\nUsing polyfills with \`${useBuiltIns}\` option:`);
  223. }
  224. }
  225. return {
  226. plugins
  227. };
  228. });
  229. exports.default = _default;