index.js
10.8 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _data = _interopRequireDefault(require("../core-js-compat/data.js"));
var _shippedProposals = _interopRequireDefault(require("./shipped-proposals"));
var _getModulesListForTargetVersion = _interopRequireDefault(require("../core-js-compat/get-modules-list-for-target-version.js"));
var _builtInDefinitions = require("./built-in-definitions");
var _usageFilters = _interopRequireDefault(require("./usage-filters"));
var _babel = _interopRequireWildcard(require("@babel/core"));
var _utils = require("./utils");
var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const {
types: t
} = _babel.default || _babel;
const runtimeCompat = "#__secret_key__@babel/runtime__compatibility";
const esnextFallback = (name, cb) => {
if (cb(name)) return true;
if (!name.startsWith("es.")) return false;
const fallback = `esnext.${name.slice(3)}`;
if (!_data.default[fallback]) return false;
return cb(fallback);
};
var _default = (0, _helperDefinePolyfillProvider.default)(function ({
getUtils,
method,
shouldInjectPolyfill,
createMetaResolver,
debug,
babel
}, {
version = 3,
proposals,
shippedProposals,
[runtimeCompat]: {
useBabelRuntime,
ext = ".js"
} = {
useBabelRuntime: ""
}
}) {
const isWebpack = babel.caller(caller => (caller == null ? void 0 : caller.name) === "babel-loader");
const resolve = createMetaResolver({
global: _builtInDefinitions.BuiltIns,
static: _builtInDefinitions.StaticProperties,
instance: _builtInDefinitions.InstanceProperties
});
const available = new Set((0, _getModulesListForTargetVersion.default)(version));
function getCoreJSPureBase(useProposalBase) {
return useBabelRuntime ? useProposalBase ? `${useBabelRuntime}/core-js` : `${useBabelRuntime}/core-js-stable` : useProposalBase ? "core-js-pure/features" : "core-js-pure/stable";
}
function maybeInjectGlobalImpl(name, utils) {
if (shouldInjectPolyfill(name)) {
debug(name);
utils.injectGlobalImport((0, _utils.coreJSModule)(name));
return true;
}
return false;
}
function maybeInjectGlobal(names, utils, fallback = true) {
for (const name of names) {
if (fallback) {
esnextFallback(name, name => maybeInjectGlobalImpl(name, utils));
} else {
maybeInjectGlobalImpl(name, utils);
}
}
}
function maybeInjectPure(desc, hint, utils, object) {
if (desc.pure && !(object && desc.exclude && desc.exclude.includes(object)) && esnextFallback(desc.name, shouldInjectPolyfill)) {
const {
name
} = desc;
let useProposalBase = false;
if (proposals || shippedProposals && name.startsWith("esnext.")) {
useProposalBase = true;
} else if (name.startsWith("es.") && !available.has(name)) {
useProposalBase = true;
}
const coreJSPureBase = getCoreJSPureBase(useProposalBase);
return utils.injectDefaultImport(`${coreJSPureBase}/${desc.pure}${ext}`, hint);
}
}
function isFeatureStable(name) {
if (name.startsWith("esnext.")) {
const esName = `es.${name.slice(7)}`; // If its imaginative esName is not in latest compat data, it means
// the proposal is not stage 4
return esName in _data.default;
}
return true;
}
return {
name: "corejs3",
polyfills: _data.default,
filterPolyfills(name) {
if (!available.has(name)) return false;
if (proposals || method === "entry-global") return true;
if (shippedProposals && _shippedProposals.default.has(name)) {
return true;
}
return isFeatureStable(name);
},
entryGlobal(meta, utils, path) {
if (meta.kind !== "import") return;
const modules = (0, _utils.isCoreJSSource)(meta.source);
if (!modules) return;
if (modules.length === 1 && meta.source === (0, _utils.coreJSModule)(modules[0]) && shouldInjectPolyfill(modules[0])) {
// Avoid infinite loop: do not replace imports with a new copy of
// themselves.
debug(null);
return;
}
const modulesSet = new Set(modules);
const filteredModules = modules.filter(module => {
if (!module.startsWith("esnext.")) return true;
const stable = module.replace("esnext.", "es.");
if (modulesSet.has(stable) && shouldInjectPolyfill(stable)) {
return false;
}
return true;
});
maybeInjectGlobal(filteredModules, utils, false);
path.remove();
},
usageGlobal(meta, utils, path) {
const resolved = resolve(meta);
if (!resolved) return;
if ((0, _usageFilters.default)(resolved.desc, path)) return;
let deps = resolved.desc.global;
if (resolved.kind !== "global" && "object" in meta && meta.object && meta.placement === "prototype") {
const low = meta.object.toLowerCase();
deps = deps.filter(m => m.includes(low) || _builtInDefinitions.CommonInstanceDependencies.has(m));
}
maybeInjectGlobal(deps, utils);
},
usagePure(meta, utils, path) {
if (meta.kind === "in") {
if (meta.key === "Symbol.iterator") {
path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("is-iterable", useBabelRuntime, ext), "isIterable"), [path.node.right] // meta.kind === "in" narrows this
));
}
return;
}
if (path.parentPath.isUnaryExpression({
operator: "delete"
})) return;
if (meta.kind === "property") {
// We can't compile destructuring and updateExpression.
if (!path.isMemberExpression()) return;
if (!path.isReferenced()) return;
if (path.parentPath.isUpdateExpression()) return;
if (t.isSuper(path.node.object)) {
return;
}
if (meta.key === "Symbol.iterator") {
if (!shouldInjectPolyfill("es.symbol.iterator")) return;
const {
parent,
node
} = path;
if (t.isCallExpression(parent, {
callee: node
})) {
if (parent.arguments.length === 0) {
path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator", useBabelRuntime, ext), "getIterator"), [node.object]));
path.skip();
} else {
(0, _utils.callMethod)(path, utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"));
}
} else {
path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"), [path.node.object]));
}
return;
}
}
let resolved = resolve(meta);
if (!resolved) return;
if ((0, _usageFilters.default)(resolved.desc, path)) return;
if (useBabelRuntime && resolved.desc.pure && resolved.desc.pure.slice(-6) === "/index") {
// Remove /index, since it doesn't exist in @babel/runtime-corejs3s
resolved = _extends({}, resolved, {
desc: _extends({}, resolved.desc, {
pure: resolved.desc.pure.slice(0, -6)
})
});
}
if (resolved.kind === "global") {
const id = maybeInjectPure(resolved.desc, resolved.name, utils);
if (id) path.replaceWith(id);
} else if (resolved.kind === "static") {
const id = maybeInjectPure(resolved.desc, resolved.name, utils, // @ts-expect-error
meta.object);
if (id) path.replaceWith(id);
} else if (resolved.kind === "instance") {
const id = maybeInjectPure(resolved.desc, `${resolved.name}InstanceProperty`, utils, // @ts-expect-error
meta.object);
if (!id) return;
const {
node
} = path;
if (t.isCallExpression(path.parent, {
callee: node
})) {
(0, _utils.callMethod)(path, id);
} else {
path.replaceWith(t.callExpression(id, [node.object]));
}
}
},
visitor: method === "usage-global" && {
// import("foo")
CallExpression(path) {
if (path.get("callee").isImport()) {
const utils = getUtils(path);
if (isWebpack) {
// Webpack uses Promise.all to handle dynamic import.
maybeInjectGlobal(_builtInDefinitions.PromiseDependenciesWithIterators, utils);
} else {
maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, utils);
}
}
},
// (async function () { }).finally(...)
Function(path) {
if (path.node.async) {
maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, getUtils(path));
}
},
// for-of, [a, b] = c
"ForOfStatement|ArrayPattern"(path) {
maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
},
// [...spread]
SpreadElement(path) {
if (!path.parentPath.isObjectExpression()) {
maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
}
},
// yield*
YieldExpression(path) {
if (path.node.delegate) {
maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
}
}
}
};
});
exports.default = _default;