transform-polyfill-require-plugin.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (_ref) {
  4. var t = _ref.types;
  5. function createImportDeclaration(polyfill) {
  6. var declar = t.importDeclaration([], t.stringLiteral(polyfill));
  7. declar._blockHoist = 3;
  8. return declar;
  9. }
  10. function createRequireStatement(polyfill) {
  11. return t.expressionStatement(t.callExpression(t.identifier("require"), [t.stringLiteral(polyfill)]));
  12. }
  13. function isRequire(path) {
  14. return t.isExpressionStatement(path.node) && t.isCallExpression(path.node.expression) && t.isIdentifier(path.node.expression.callee) && path.node.expression.callee.name === "require" && path.node.expression.arguments.length === 1 && t.isStringLiteral(path.node.expression.arguments[0]) && isPolyfillSource(path.node.expression.arguments[0].value);
  15. }
  16. function createImport(polyfill, requireType, core) {
  17. if (core) {
  18. polyfill = "core-js/modules/" + polyfill;
  19. }
  20. if (requireType === "import") {
  21. return createImportDeclaration(polyfill);
  22. } else {
  23. return createRequireStatement(polyfill);
  24. }
  25. }
  26. function createImports(polyfills, requireType, regenerator) {
  27. var imports = polyfills.filter(function (el, i, arr) {
  28. return arr.indexOf(el) === i;
  29. }).map(function (polyfill) {
  30. return createImport(polyfill, requireType, true);
  31. });
  32. return [].concat(imports, [regenerator && createImport("regenerator-runtime/runtime", requireType)]).filter(Boolean);
  33. }
  34. var isPolyfillImport = {
  35. ImportDeclaration: function ImportDeclaration(path, state) {
  36. if (path.node.specifiers.length === 0 && isPolyfillSource(path.node.source.value)) {
  37. this.numPolyfillImports++;
  38. if (this.numPolyfillImports > 1) {
  39. path.remove();
  40. return;
  41. }
  42. path.replaceWithMultiple(createImports(state.opts.polyfills, "import", state.opts.regenerator));
  43. }
  44. },
  45. Program: function Program(path, state) {
  46. var _this = this;
  47. if (!state.opts.polyfills) {
  48. throw path.buildCodeFrameError("\nThere was an issue in \"babel-preset-env\" such that\nthe \"polyfills\" option was not correctly passed\nto the \"transform-polyfill-require\" plugin\n");
  49. }
  50. path.get("body").forEach(function (bodyPath) {
  51. if (isRequire(bodyPath)) {
  52. _this.numPolyfillImports++;
  53. if (_this.numPolyfillImports > 1) {
  54. path.remove();
  55. return;
  56. }
  57. bodyPath.replaceWithMultiple(createImports(state.opts.polyfills, "require", state.opts.regenerator));
  58. }
  59. });
  60. }
  61. };
  62. return {
  63. name: "transform-polyfill-require",
  64. visitor: isPolyfillImport,
  65. pre: function pre() {
  66. this.numPolyfillImports = 0;
  67. }
  68. };
  69. };
  70. function isPolyfillSource(value) {
  71. return value === "babel-polyfill" || value === "core-js";
  72. }