1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.default = void 0;
|
---|
5 |
|
---|
6 | var _data = _interopRequireDefault(require("../core-js-compat/data.js"));
|
---|
7 |
|
---|
8 | var _shippedProposals = _interopRequireDefault(require("./shipped-proposals"));
|
---|
9 |
|
---|
10 | var _getModulesListForTargetVersion = _interopRequireDefault(require("../core-js-compat/get-modules-list-for-target-version.js"));
|
---|
11 |
|
---|
12 | var _builtInDefinitions = require("./built-in-definitions");
|
---|
13 |
|
---|
14 | var babel = _interopRequireWildcard(require("@babel/core"));
|
---|
15 |
|
---|
16 | var _utils = require("./utils");
|
---|
17 |
|
---|
18 | var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
|
---|
19 |
|
---|
20 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
---|
21 |
|
---|
22 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
|
---|
23 |
|
---|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
25 |
|
---|
26 | function _extends() { _extends = Object.assign || 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); }
|
---|
27 |
|
---|
28 | const {
|
---|
29 | types: t
|
---|
30 | } = babel.default || babel;
|
---|
31 | const runtimeCompat = "#__secret_key__@babel/runtime__compatibility";
|
---|
32 |
|
---|
33 | const esnextFallback = (name, cb) => {
|
---|
34 | if (cb(name)) return true;
|
---|
35 | if (!name.startsWith("es.")) return false;
|
---|
36 | const fallback = `esnext.${name.slice(3)}`;
|
---|
37 | if (!_data.default[fallback]) return false;
|
---|
38 | return cb(fallback);
|
---|
39 | };
|
---|
40 |
|
---|
41 | var _default = (0, _helperDefinePolyfillProvider.default)(function ({
|
---|
42 | getUtils,
|
---|
43 | method,
|
---|
44 | shouldInjectPolyfill,
|
---|
45 | createMetaResolver,
|
---|
46 | debug,
|
---|
47 | babel
|
---|
48 | }, {
|
---|
49 | version = 3,
|
---|
50 | proposals,
|
---|
51 | shippedProposals,
|
---|
52 | [runtimeCompat]: {
|
---|
53 | useBabelRuntime,
|
---|
54 | ext = ".js"
|
---|
55 | } = {}
|
---|
56 | }) {
|
---|
57 | const isWebpack = babel.caller(caller => (caller == null ? void 0 : caller.name) === "babel-loader");
|
---|
58 | const resolve = createMetaResolver({
|
---|
59 | global: _builtInDefinitions.BuiltIns,
|
---|
60 | static: _builtInDefinitions.StaticProperties,
|
---|
61 | instance: _builtInDefinitions.InstanceProperties
|
---|
62 | });
|
---|
63 | const available = new Set((0, _getModulesListForTargetVersion.default)(version));
|
---|
64 |
|
---|
65 | function getCoreJSPureBase(useProposalBase) {
|
---|
66 | return useBabelRuntime ? useProposalBase ? `${useBabelRuntime}/core-js` : `${useBabelRuntime}/core-js-stable` : useProposalBase ? "core-js-pure/features" : "core-js-pure/stable";
|
---|
67 | }
|
---|
68 |
|
---|
69 | function maybeInjectGlobalImpl(name, utils) {
|
---|
70 | if (shouldInjectPolyfill(name)) {
|
---|
71 | debug(name);
|
---|
72 | utils.injectGlobalImport((0, _utils.coreJSModule)(name));
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | function maybeInjectGlobal(names, utils, fallback = true) {
|
---|
80 | for (const name of names) {
|
---|
81 | if (fallback) {
|
---|
82 | esnextFallback(name, name => maybeInjectGlobalImpl(name, utils));
|
---|
83 | } else {
|
---|
84 | maybeInjectGlobalImpl(name, utils);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | function maybeInjectPure(desc, hint, utils, object) {
|
---|
90 | if (desc.pure && !(object && desc.exclude && desc.exclude.includes(object)) && esnextFallback(desc.name, shouldInjectPolyfill)) {
|
---|
91 | const {
|
---|
92 | name
|
---|
93 | } = desc;
|
---|
94 | let useProposalBase = false;
|
---|
95 |
|
---|
96 | if (proposals || shippedProposals && name.startsWith("esnext.")) {
|
---|
97 | useProposalBase = true;
|
---|
98 | } else if (name.startsWith("es.") && !available.has(name)) {
|
---|
99 | useProposalBase = true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | const coreJSPureBase = getCoreJSPureBase(useProposalBase);
|
---|
103 | return utils.injectDefaultImport( // $FlowIgnore, we already guard desc.pure
|
---|
104 | `${coreJSPureBase}/${desc.pure}${ext}`, hint);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | function isFeatureStable(name) {
|
---|
109 | if (name.startsWith("esnext.")) {
|
---|
110 | const esName = `es.${name.slice(7)}`; // If its imaginative esName is not in latest compat data, it means
|
---|
111 | // the proposal is not stage 4
|
---|
112 |
|
---|
113 | return esName in _data.default;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | return {
|
---|
120 | name: "corejs3",
|
---|
121 | polyfills: _data.default,
|
---|
122 |
|
---|
123 | filterPolyfills(name) {
|
---|
124 | if (!available.has(name)) return false;
|
---|
125 | if (proposals || method === "entry-global") return true;
|
---|
126 |
|
---|
127 | if (shippedProposals && _shippedProposals.default.has(name)) {
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return isFeatureStable(name);
|
---|
132 | },
|
---|
133 |
|
---|
134 | entryGlobal(meta, utils, path) {
|
---|
135 | if (meta.kind !== "import") return;
|
---|
136 | const modules = (0, _utils.isCoreJSSource)(meta.source);
|
---|
137 | if (!modules) return;
|
---|
138 |
|
---|
139 | if (modules.length === 1 && meta.source === (0, _utils.coreJSModule)(modules[0]) && shouldInjectPolyfill(modules[0])) {
|
---|
140 | // Avoid infinite loop: do not replace imports with a new copy of
|
---|
141 | // themselves.
|
---|
142 | debug(null);
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | maybeInjectGlobal(modules, utils, false);
|
---|
147 | path.remove();
|
---|
148 | },
|
---|
149 |
|
---|
150 | usageGlobal(meta, utils) {
|
---|
151 | const resolved = resolve(meta);
|
---|
152 | if (!resolved) return;
|
---|
153 | let deps = resolved.desc.global;
|
---|
154 |
|
---|
155 | if (resolved.kind !== "global" && meta.object && meta.placement === "prototype") {
|
---|
156 | const low = meta.object.toLowerCase();
|
---|
157 | deps = deps.filter(m => m.includes(low) || _builtInDefinitions.CommonInstanceDependencies.has(m));
|
---|
158 | }
|
---|
159 |
|
---|
160 | maybeInjectGlobal(deps, utils);
|
---|
161 | },
|
---|
162 |
|
---|
163 | usagePure(meta, utils, path) {
|
---|
164 | if (meta.kind === "in") {
|
---|
165 | if (meta.key === "Symbol.iterator") {
|
---|
166 | path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("is-iterable", useBabelRuntime, ext), "isIterable"), [path.node.right]));
|
---|
167 | }
|
---|
168 |
|
---|
169 | return;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (path.parentPath.isUnaryExpression({
|
---|
173 | operator: "delete"
|
---|
174 | })) return;
|
---|
175 | let isCall;
|
---|
176 |
|
---|
177 | if (meta.kind === "property") {
|
---|
178 | // We can't compile destructuring.
|
---|
179 | if (!path.isMemberExpression()) return;
|
---|
180 | if (!path.isReferenced()) return;
|
---|
181 | isCall = path.parentPath.isCallExpression({
|
---|
182 | callee: path.node
|
---|
183 | });
|
---|
184 |
|
---|
185 | if (meta.key === "Symbol.iterator") {
|
---|
186 | if (!shouldInjectPolyfill("es.symbol.iterator")) return;
|
---|
187 |
|
---|
188 | if (isCall) {
|
---|
189 | if (path.parent.arguments.length === 0) {
|
---|
190 | path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator", useBabelRuntime, ext), "getIterator"), [path.node.object]));
|
---|
191 | path.skip();
|
---|
192 | } else {
|
---|
193 | (0, _utils.callMethod)(path, utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"));
|
---|
194 | }
|
---|
195 | } else {
|
---|
196 | path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"), [path.node.object]));
|
---|
197 | }
|
---|
198 |
|
---|
199 | return;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | let resolved = resolve(meta);
|
---|
204 | if (!resolved) return;
|
---|
205 |
|
---|
206 | if (useBabelRuntime && resolved.desc.pure && resolved.desc.pure.slice(-6) === "/index") {
|
---|
207 | // Remove /index, since it doesn't exist in @babel/runtime-corejs3s
|
---|
208 | resolved = _extends({}, resolved, {
|
---|
209 | desc: _extends({}, resolved.desc, {
|
---|
210 | pure: resolved.desc.pure.slice(0, -6)
|
---|
211 | })
|
---|
212 | });
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (resolved.kind === "global") {
|
---|
216 | const id = maybeInjectPure(resolved.desc, resolved.name, utils);
|
---|
217 | if (id) path.replaceWith(id);
|
---|
218 | } else if (resolved.kind === "static") {
|
---|
219 | const id = maybeInjectPure(resolved.desc, resolved.name, utils, // $FlowIgnore
|
---|
220 | meta.object);
|
---|
221 | if (id) path.replaceWith(id);
|
---|
222 | } else if (resolved.kind === "instance") {
|
---|
223 | const id = maybeInjectPure(resolved.desc, `${resolved.name}InstanceProperty`, utils, // $FlowIgnore
|
---|
224 | meta.object);
|
---|
225 | if (!id) return;
|
---|
226 |
|
---|
227 | if (isCall) {
|
---|
228 | (0, _utils.callMethod)(path, id);
|
---|
229 | } else {
|
---|
230 | path.replaceWith(t.callExpression(id, [path.node.object]));
|
---|
231 | }
|
---|
232 | }
|
---|
233 | },
|
---|
234 |
|
---|
235 | visitor: method === "usage-global" && {
|
---|
236 | // import("foo")
|
---|
237 | CallExpression(path) {
|
---|
238 | if (path.get("callee").isImport()) {
|
---|
239 | const utils = getUtils(path);
|
---|
240 |
|
---|
241 | if (isWebpack) {
|
---|
242 | // Webpack uses Promise.all to handle dynamic import.
|
---|
243 | maybeInjectGlobal(_builtInDefinitions.PromiseDependenciesWithIterators, utils);
|
---|
244 | } else {
|
---|
245 | maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, utils);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | },
|
---|
249 |
|
---|
250 | // (async function () { }).finally(...)
|
---|
251 | Function(path) {
|
---|
252 | if (path.node.async) {
|
---|
253 | maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, getUtils(path));
|
---|
254 | }
|
---|
255 | },
|
---|
256 |
|
---|
257 | // for-of, [a, b] = c
|
---|
258 | "ForOfStatement|ArrayPattern"(path) {
|
---|
259 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
---|
260 | },
|
---|
261 |
|
---|
262 | // [...spread]
|
---|
263 | SpreadElement(path) {
|
---|
264 | if (!path.parentPath.isObjectExpression()) {
|
---|
265 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
---|
266 | }
|
---|
267 | },
|
---|
268 |
|
---|
269 | // yield*
|
---|
270 | YieldExpression(path) {
|
---|
271 | if (path.node.delegate) {
|
---|
272 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | }
|
---|
277 | };
|
---|
278 | });
|
---|
279 |
|
---|
280 | exports.default = _default; |
---|