babel-polyfill.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var _utils = require("./utils");
  7. const BABEL_POLYFILL_DEPRECATION = `
  8. \`@babel/polyfill\` is deprecated. Please, use required parts of \`core-js\`
  9. and \`regenerator-runtime/runtime\` separately`;
  10. const NO_DIRECT_POLYFILL_IMPORT = `
  11. When setting \`useBuiltIns: 'usage'\`, polyfills are automatically imported when needed.
  12. Please remove the direct import of \`SPECIFIER\` or use \`useBuiltIns: 'entry'\` instead.`;
  13. function _default({
  14. template
  15. }, {
  16. regenerator,
  17. deprecated,
  18. usage
  19. }) {
  20. return {
  21. name: "preset-env/replace-babel-polyfill",
  22. visitor: {
  23. ImportDeclaration(path) {
  24. const src = (0, _utils.getImportSource)(path);
  25. if (usage && (0, _utils.isPolyfillSource)(src)) {
  26. console.warn(NO_DIRECT_POLYFILL_IMPORT.replace("SPECIFIER", src));
  27. if (!deprecated) path.remove();
  28. } else if (src === "@babel/polyfill") {
  29. if (deprecated) {
  30. console.warn(BABEL_POLYFILL_DEPRECATION);
  31. } else if (regenerator) {
  32. path.replaceWithMultiple(template.ast`
  33. import "core-js";
  34. import "regenerator-runtime/runtime.js";
  35. `);
  36. } else {
  37. path.replaceWith(template.ast`
  38. import "core-js";
  39. `);
  40. }
  41. }
  42. },
  43. Program(path) {
  44. path.get("body").forEach(bodyPath => {
  45. const src = (0, _utils.getRequireSource)(bodyPath);
  46. if (usage && (0, _utils.isPolyfillSource)(src)) {
  47. console.warn(NO_DIRECT_POLYFILL_IMPORT.replace("SPECIFIER", src));
  48. if (!deprecated) bodyPath.remove();
  49. } else if (src === "@babel/polyfill") {
  50. if (deprecated) {
  51. console.warn(BABEL_POLYFILL_DEPRECATION);
  52. } else if (regenerator) {
  53. bodyPath.replaceWithMultiple(template.ast`
  54. require("core-js");
  55. require("regenerator-runtime/runtime.js");
  56. `);
  57. } else {
  58. bodyPath.replaceWith(template.ast`
  59. require("core-js");
  60. `);
  61. }
  62. }
  63. });
  64. }
  65. }
  66. };
  67. }