babel-polyfill.cjs 2.2 KB

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