index.js
4.59 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"use strict";
exports.__esModule = true;
exports.definitions = undefined;
exports.default = function (_ref) {
var t = _ref.types;
function getRuntimeModuleName(opts) {
return opts.moduleName || "babel-runtime";
}
function has(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
var HELPER_BLACKLIST = ["interopRequireWildcard", "interopRequireDefault"];
return {
pre: function pre(file) {
var moduleName = getRuntimeModuleName(this.opts);
if (this.opts.helpers !== false) {
file.set("helperGenerator", function (name) {
if (HELPER_BLACKLIST.indexOf(name) < 0) {
return file.addImport(moduleName + "/helpers/" + name, "default", name);
}
});
}
this.setDynamic("regeneratorIdentifier", function () {
return file.addImport(moduleName + "/regenerator", "default", "regeneratorRuntime");
});
},
visitor: {
ReferencedIdentifier: function ReferencedIdentifier(path, state) {
var node = path.node,
parent = path.parent,
scope = path.scope;
if (node.name === "regeneratorRuntime" && state.opts.regenerator !== false) {
path.replaceWith(state.get("regeneratorIdentifier"));
return;
}
if (state.opts.polyfill === false) return;
if (t.isMemberExpression(parent)) return;
if (!has(_definitions2.default.builtins, node.name)) return;
if (scope.getBindingIdentifier(node.name)) return;
var moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(state.addImport(moduleName + "/core-js/" + _definitions2.default.builtins[node.name], "default", node.name));
},
CallExpression: function CallExpression(path, state) {
if (state.opts.polyfill === false) return;
if (path.node.arguments.length) return;
var callee = path.node.callee;
if (!t.isMemberExpression(callee)) return;
if (!callee.computed) return;
if (!path.get("callee.property").matchesPattern("Symbol.iterator")) return;
var moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(t.callExpression(state.addImport(moduleName + "/core-js/get-iterator", "default", "getIterator"), [callee.object]));
},
BinaryExpression: function BinaryExpression(path, state) {
if (state.opts.polyfill === false) return;
if (path.node.operator !== "in") return;
if (!path.get("left").matchesPattern("Symbol.iterator")) return;
var moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(t.callExpression(state.addImport(moduleName + "/core-js/is-iterable", "default", "isIterable"), [path.node.right]));
},
MemberExpression: {
enter: function enter(path, state) {
if (state.opts.polyfill === false) return;
if (!path.isReferenced()) return;
var node = path.node;
var obj = node.object;
var prop = node.property;
if (!t.isReferenced(obj, node)) return;
if (node.computed) return;
if (!has(_definitions2.default.methods, obj.name)) return;
var methods = _definitions2.default.methods[obj.name];
if (!has(methods, prop.name)) return;
if (path.scope.getBindingIdentifier(obj.name)) return;
if (obj.name === "Object" && prop.name === "defineProperty" && path.parentPath.isCallExpression()) {
var call = path.parentPath.node;
if (call.arguments.length === 3 && t.isLiteral(call.arguments[1])) return;
}
var moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(state.addImport(moduleName + "/core-js/" + methods[prop.name], "default", obj.name + "$" + prop.name));
},
exit: function exit(path, state) {
if (state.opts.polyfill === false) return;
if (!path.isReferenced()) return;
var node = path.node;
var obj = node.object;
if (!has(_definitions2.default.builtins, obj.name)) return;
if (path.scope.getBindingIdentifier(obj.name)) return;
var moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(t.memberExpression(state.addImport(moduleName + "/core-js/" + _definitions2.default.builtins[obj.name], "default", obj.name), node.property, node.computed));
}
}
}
};
};
var _definitions = require("./definitions");
var _definitions2 = _interopRequireDefault(_definitions);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.definitions = _definitions2.default;