index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.transformIncludesAndExcludes = exports.isPluginRequired = undefined;
  4. exports.default = buildPreset;
  5. var _semver = require("semver");
  6. var _semver2 = _interopRequireDefault(_semver);
  7. var _builtIns = require("../data/built-ins.json");
  8. var _builtIns2 = _interopRequireDefault(_builtIns);
  9. var _defaultIncludes = require("./default-includes");
  10. var _moduleTransformations = require("./module-transformations");
  11. var _moduleTransformations2 = _interopRequireDefault(_moduleTransformations);
  12. var _normalizeOptions = require("./normalize-options.js");
  13. var _normalizeOptions2 = _interopRequireDefault(_normalizeOptions);
  14. var _plugins = require("../data/plugins.json");
  15. var _plugins2 = _interopRequireDefault(_plugins);
  16. var _transformPolyfillRequirePlugin = require("./transform-polyfill-require-plugin");
  17. var _transformPolyfillRequirePlugin2 = _interopRequireDefault(_transformPolyfillRequirePlugin);
  18. var _targetsParser = require("./targets-parser");
  19. var _targetsParser2 = _interopRequireDefault(_targetsParser);
  20. var _utils = require("./utils");
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. /**
  23. * Determine if a transformation is required
  24. *
  25. * NOTE: This assumes `supportedEnvironments` has already been parsed by `getTargets`
  26. *
  27. * @param {Object} supportedEnvironments An Object containing environment keys and the lowest
  28. * supported version as a value
  29. * @param {Object} plugin An Object containing environment keys and the lowest
  30. * version the feature was implemented in as a value
  31. * @return {Boolean} Whether or not the transformation is required
  32. */
  33. var isPluginRequired = exports.isPluginRequired = function isPluginRequired(supportedEnvironments, plugin) {
  34. var targetEnvironments = Object.keys(supportedEnvironments);
  35. if (targetEnvironments.length === 0) {
  36. return true;
  37. }
  38. var isRequiredForEnvironments = targetEnvironments.filter(function (environment) {
  39. // Feature is not implemented in that environment
  40. if (!plugin[environment]) {
  41. return true;
  42. }
  43. var lowestImplementedVersion = plugin[environment];
  44. var lowestTargetedVersion = supportedEnvironments[environment];
  45. if (!_semver2.default.valid(lowestTargetedVersion)) {
  46. throw new Error(
  47. // eslint-disable-next-line max-len
  48. "Invalid version passed for target \"" + environment + "\": \"" + lowestTargetedVersion + "\". Versions must be in semver format (major.minor.patch)");
  49. }
  50. return _semver2.default.gt((0, _utils.semverify)(lowestImplementedVersion), lowestTargetedVersion);
  51. });
  52. return isRequiredForEnvironments.length > 0;
  53. };
  54. var hasBeenLogged = false;
  55. var logPlugin = function logPlugin(plugin, targets, list) {
  56. var envList = list[plugin] || {};
  57. var filteredList = Object.keys(targets).reduce(function (a, b) {
  58. if (!envList[b] || _semver2.default.lt(targets[b], (0, _utils.semverify)(envList[b]))) {
  59. a[b] = (0, _utils.prettifyVersion)(targets[b]);
  60. }
  61. return a;
  62. }, {});
  63. var logStr = " " + plugin + " " + JSON.stringify(filteredList);
  64. console.log(logStr);
  65. };
  66. var filterItem = function filterItem(targets, exclusions, list, item) {
  67. var isDefault = _defaultIncludes.defaultWebIncludes.indexOf(item) >= 0;
  68. var notExcluded = exclusions.indexOf(item) === -1;
  69. if (isDefault) return notExcluded;
  70. var isRequired = isPluginRequired(targets, list[item]);
  71. return isRequired && notExcluded;
  72. };
  73. var getBuiltInTargets = function getBuiltInTargets(targets) {
  74. var builtInTargets = (0, _utils._extends)({}, targets);
  75. if (builtInTargets.uglify != null) {
  76. delete builtInTargets.uglify;
  77. }
  78. return builtInTargets;
  79. };
  80. var transformIncludesAndExcludes = exports.transformIncludesAndExcludes = function transformIncludesAndExcludes(opts) {
  81. return {
  82. all: opts,
  83. plugins: opts.filter(function (opt) {
  84. return !opt.match(/^(es\d+|web)\./);
  85. }),
  86. builtIns: opts.filter(function (opt) {
  87. return opt.match(/^(es\d+|web)\./);
  88. })
  89. };
  90. };
  91. function getPlatformSpecificDefaultFor(targets) {
  92. var targetNames = Object.keys(targets);
  93. var isAnyTarget = !targetNames.length;
  94. var isWebTarget = targetNames.some(function (name) {
  95. return name !== "node";
  96. });
  97. return isAnyTarget || isWebTarget ? _defaultIncludes.defaultWebIncludes : [];
  98. }
  99. function buildPreset(context) {
  100. var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  101. var validatedOptions = (0, _normalizeOptions2.default)(opts);
  102. var debug = validatedOptions.debug,
  103. loose = validatedOptions.loose,
  104. moduleType = validatedOptions.moduleType,
  105. spec = validatedOptions.spec,
  106. useBuiltIns = validatedOptions.useBuiltIns;
  107. var targets = (0, _targetsParser2.default)(validatedOptions.targets);
  108. var include = transformIncludesAndExcludes(validatedOptions.include);
  109. var exclude = transformIncludesAndExcludes(validatedOptions.exclude);
  110. var filterPlugins = filterItem.bind(null, targets, exclude.plugins, _plugins2.default);
  111. var transformations = Object.keys(_plugins2.default).filter(filterPlugins).concat(include.plugins);
  112. var polyfills = void 0;
  113. var polyfillTargets = void 0;
  114. if (useBuiltIns) {
  115. polyfillTargets = getBuiltInTargets(targets);
  116. var filterBuiltIns = filterItem.bind(null, polyfillTargets, exclude.builtIns, _builtIns2.default);
  117. polyfills = Object.keys(_builtIns2.default).concat(getPlatformSpecificDefaultFor(polyfillTargets)).filter(filterBuiltIns).concat(include.builtIns);
  118. }
  119. if (debug && !hasBeenLogged) {
  120. hasBeenLogged = true;
  121. console.log("babel-preset-env: `DEBUG` option");
  122. console.log("\nUsing targets:");
  123. console.log(JSON.stringify((0, _utils.prettifyTargets)(targets), null, 2));
  124. console.log("\nModules transform: " + moduleType);
  125. console.log("\nUsing plugins:");
  126. transformations.forEach(function (transform) {
  127. logPlugin(transform, targets, _plugins2.default);
  128. });
  129. if (useBuiltIns && polyfills.length) {
  130. console.log("\nUsing polyfills:");
  131. polyfills.forEach(function (polyfill) {
  132. logPlugin(polyfill, polyfillTargets, _builtIns2.default);
  133. });
  134. }
  135. }
  136. var regenerator = transformations.indexOf("transform-regenerator") >= 0;
  137. var modulePlugin = moduleType !== false && _moduleTransformations2.default[moduleType];
  138. var plugins = [];
  139. // NOTE: not giving spec here yet to avoid compatibility issues when
  140. // babel-plugin-transform-es2015-modules-commonjs gets its spec mode
  141. modulePlugin && plugins.push([require("babel-plugin-" + modulePlugin), { loose: loose }]);
  142. plugins.push.apply(plugins, transformations.map(function (pluginName) {
  143. return [require("babel-plugin-" + pluginName), { spec: spec, loose: loose }];
  144. }));
  145. useBuiltIns && plugins.push([_transformPolyfillRequirePlugin2.default, { polyfills: polyfills, regenerator: regenerator }]);
  146. return {
  147. plugins: plugins
  148. };
  149. }