use-built-ins-entry-plugin.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var _debug = require("./debug");
  7. var _utils = require("./utils");
  8. function _default({
  9. types: t
  10. }) {
  11. function replaceWithPolyfillImports(path, polyfills, regenerator) {
  12. if (regenerator) {
  13. (0, _utils.createImport)(path, "regenerator-runtime");
  14. }
  15. const items = Array.isArray(polyfills) ? new Set(polyfills) : polyfills;
  16. for (const p of Array.from(items).reverse()) {
  17. (0, _utils.createImport)(path, p);
  18. }
  19. path.remove();
  20. }
  21. const isPolyfillImport = {
  22. ImportDeclaration(path, state) {
  23. if (path.node.specifiers.length === 0 && (0, _utils.isPolyfillSource)(path.node.source.value)) {
  24. this.importPolyfillIncluded = true;
  25. replaceWithPolyfillImports(path, state.opts.polyfills, state.opts.regenerator);
  26. }
  27. },
  28. Program(path, state) {
  29. path.get("body").forEach(bodyPath => {
  30. if ((0, _utils.isRequire)(t, bodyPath)) {
  31. replaceWithPolyfillImports(bodyPath, state.opts.polyfills, state.opts.regenerator);
  32. }
  33. });
  34. }
  35. };
  36. return {
  37. name: "transform-polyfill-require",
  38. visitor: isPolyfillImport,
  39. pre() {
  40. this.numPolyfillImports = 0;
  41. this.importPolyfillIncluded = false;
  42. },
  43. post() {
  44. const {
  45. debug,
  46. onDebug,
  47. polyfills
  48. } = this.opts;
  49. if (debug) {
  50. (0, _debug.logEntryPolyfills)(this.importPolyfillIncluded, polyfills, this.file.opts.filename, onDebug);
  51. }
  52. }
  53. };
  54. }