schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";
 
exports.__esModule = true;
 
exports.default = function (_ref) {
  var t = _ref.types;
 
  function createImportDeclaration(polyfill) {
    var declar = t.importDeclaration([], t.stringLiteral(polyfill));
    declar._blockHoist = 3;
    return declar;
  }
 
  function createRequireStatement(polyfill) {
    return t.expressionStatement(t.callExpression(t.identifier("require"), [t.stringLiteral(polyfill)]));
  }
 
  function isRequire(path) {
    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);
  }
 
  function createImport(polyfill, requireType, core) {
    if (core) {
      polyfill = "core-js/modules/" + polyfill;
    }
 
    if (requireType === "import") {
      return createImportDeclaration(polyfill);
    } else {
      return createRequireStatement(polyfill);
    }
  }
 
  function createImports(polyfills, requireType, regenerator) {
    var imports = polyfills.filter(function (el, i, arr) {
      return arr.indexOf(el) === i;
    }).map(function (polyfill) {
      return createImport(polyfill, requireType, true);
    });
 
    return [].concat(imports, [regenerator && createImport("regenerator-runtime/runtime", requireType)]).filter(Boolean);
  }
 
  var isPolyfillImport = {
    ImportDeclaration: function ImportDeclaration(path, state) {
      if (path.node.specifiers.length === 0 && isPolyfillSource(path.node.source.value)) {
        this.numPolyfillImports++;
        if (this.numPolyfillImports > 1) {
          path.remove();
          return;
        }
 
        path.replaceWithMultiple(createImports(state.opts.polyfills, "import", state.opts.regenerator));
      }
    },
    Program: function Program(path, state) {
      var _this = this;
 
      if (!state.opts.polyfills) {
        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");
      }
      path.get("body").forEach(function (bodyPath) {
        if (isRequire(bodyPath)) {
          _this.numPolyfillImports++;
          if (_this.numPolyfillImports > 1) {
            path.remove();
            return;
          }
 
          bodyPath.replaceWithMultiple(createImports(state.opts.polyfills, "require", state.opts.regenerator));
        }
      });
    }
  };
 
  return {
    name: "transform-polyfill-require",
    visitor: isPolyfillImport,
    pre: function pre() {
      this.numPolyfillImports = 0;
    }
  };
};
 
function isPolyfillSource(value) {
  return value === "babel-polyfill" || value === "core-js";
}