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