1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.default = void 0;
|
---|
5 | var _corejs2BuiltIns = _interopRequireDefault(require("@babel/compat-data/corejs2-built-ins"));
|
---|
6 | var _builtInDefinitions = require("./built-in-definitions");
|
---|
7 | var _addPlatformSpecificPolyfills = _interopRequireDefault(require("./add-platform-specific-polyfills"));
|
---|
8 | var _helpers = require("./helpers");
|
---|
9 | var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
|
---|
10 | var _babel = _interopRequireWildcard(require("@babel/core"));
|
---|
11 | 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); }
|
---|
12 | 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; }
|
---|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
14 | const {
|
---|
15 | types: t
|
---|
16 | } = _babel.default || _babel;
|
---|
17 | const BABEL_RUNTIME = "@babel/runtime-corejs2";
|
---|
18 | const presetEnvCompat = "#__secret_key__@babel/preset-env__compatibility";
|
---|
19 | const runtimeCompat = "#__secret_key__@babel/runtime__compatibility";
|
---|
20 | const has = Function.call.bind(Object.hasOwnProperty);
|
---|
21 | var _default = (0, _helperDefinePolyfillProvider.default)(function (api, {
|
---|
22 | [presetEnvCompat]: {
|
---|
23 | entryInjectRegenerator = false,
|
---|
24 | noRuntimeName = false
|
---|
25 | } = {},
|
---|
26 | [runtimeCompat]: {
|
---|
27 | useBabelRuntime = false,
|
---|
28 | runtimeVersion = "",
|
---|
29 | ext = ".js"
|
---|
30 | } = {}
|
---|
31 | }) {
|
---|
32 | const resolve = api.createMetaResolver({
|
---|
33 | global: _builtInDefinitions.BuiltIns,
|
---|
34 | static: _builtInDefinitions.StaticProperties,
|
---|
35 | instance: _builtInDefinitions.InstanceProperties
|
---|
36 | });
|
---|
37 | const {
|
---|
38 | debug,
|
---|
39 | shouldInjectPolyfill,
|
---|
40 | method
|
---|
41 | } = api;
|
---|
42 | const polyfills = (0, _addPlatformSpecificPolyfills.default)(api.targets, method, _corejs2BuiltIns.default);
|
---|
43 | const coreJSBase = useBabelRuntime ? `${BABEL_RUNTIME}/core-js` : method === "usage-pure" ? "core-js/library/fn" : "core-js/modules";
|
---|
44 | function inject(name, utils) {
|
---|
45 | if (typeof name === "string") {
|
---|
46 | // Some polyfills aren't always available, for example
|
---|
47 | // web.dom.iterable when targeting node
|
---|
48 | if (has(polyfills, name) && shouldInjectPolyfill(name)) {
|
---|
49 | debug(name);
|
---|
50 | utils.injectGlobalImport(`${coreJSBase}/${name}.js`);
|
---|
51 | }
|
---|
52 | return;
|
---|
53 | }
|
---|
54 | name.forEach(name => inject(name, utils));
|
---|
55 | }
|
---|
56 | function maybeInjectPure(desc, hint, utils) {
|
---|
57 | let {
|
---|
58 | pure,
|
---|
59 | meta,
|
---|
60 | name
|
---|
61 | } = desc;
|
---|
62 | if (!pure || !shouldInjectPolyfill(name)) return;
|
---|
63 | if (runtimeVersion && meta && meta.minRuntimeVersion && !(0, _helpers.hasMinVersion)(meta && meta.minRuntimeVersion, runtimeVersion)) {
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Unfortunately core-js and @babel/runtime-corejs2 don't have the same
|
---|
68 | // directory structure, so we need to special case this.
|
---|
69 | if (useBabelRuntime && pure === "symbol/index") pure = "symbol";
|
---|
70 | return utils.injectDefaultImport(`${coreJSBase}/${pure}${ext}`, hint);
|
---|
71 | }
|
---|
72 | return {
|
---|
73 | name: "corejs2",
|
---|
74 | runtimeName: noRuntimeName ? null : BABEL_RUNTIME,
|
---|
75 | polyfills,
|
---|
76 | entryGlobal(meta, utils, path) {
|
---|
77 | if (meta.kind === "import" && meta.source === "core-js") {
|
---|
78 | debug(null);
|
---|
79 | inject(Object.keys(polyfills), utils);
|
---|
80 | if (entryInjectRegenerator) {
|
---|
81 | utils.injectGlobalImport("regenerator-runtime/runtime.js");
|
---|
82 | }
|
---|
83 | path.remove();
|
---|
84 | }
|
---|
85 | },
|
---|
86 | usageGlobal(meta, utils) {
|
---|
87 | const resolved = resolve(meta);
|
---|
88 | if (!resolved) return;
|
---|
89 | let deps = resolved.desc.global;
|
---|
90 | if (resolved.kind !== "global" && "object" in meta && meta.object && meta.placement === "prototype") {
|
---|
91 | const low = meta.object.toLowerCase();
|
---|
92 | deps = deps.filter(m => m.includes(low));
|
---|
93 | }
|
---|
94 | inject(deps, utils);
|
---|
95 | },
|
---|
96 | usagePure(meta, utils, path) {
|
---|
97 | if (meta.kind === "in") {
|
---|
98 | if (meta.key === "Symbol.iterator") {
|
---|
99 | path.replaceWith(t.callExpression(utils.injectDefaultImport(`${coreJSBase}/is-iterable${ext}`, "isIterable"), [path.node.right] // meta.kind === "in" narrows this
|
---|
100 | ));
|
---|
101 | }
|
---|
102 |
|
---|
103 | return;
|
---|
104 | }
|
---|
105 | if (path.parentPath.isUnaryExpression({
|
---|
106 | operator: "delete"
|
---|
107 | })) return;
|
---|
108 | if (meta.kind === "property") {
|
---|
109 | // We can't compile destructuring.
|
---|
110 | if (!path.isMemberExpression()) return;
|
---|
111 | if (!path.isReferenced()) return;
|
---|
112 | if (meta.key === "Symbol.iterator" && shouldInjectPolyfill("es6.symbol") && path.parentPath.isCallExpression({
|
---|
113 | callee: path.node
|
---|
114 | }) && path.parentPath.node.arguments.length === 0) {
|
---|
115 | path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport(`${coreJSBase}/get-iterator${ext}`, "getIterator"), [path.node.object]));
|
---|
116 | path.skip();
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | const resolved = resolve(meta);
|
---|
121 | if (!resolved) return;
|
---|
122 | const id = maybeInjectPure(resolved.desc, resolved.name, utils);
|
---|
123 | if (id) path.replaceWith(id);
|
---|
124 | },
|
---|
125 | visitor: method === "usage-global" && {
|
---|
126 | // yield*
|
---|
127 | YieldExpression(path) {
|
---|
128 | if (path.node.delegate) {
|
---|
129 | inject("web.dom.iterable", api.getUtils(path));
|
---|
130 | }
|
---|
131 | },
|
---|
132 | // for-of, [a, b] = c
|
---|
133 | "ForOfStatement|ArrayPattern"(path) {
|
---|
134 | _builtInDefinitions.CommonIterators.forEach(name => inject(name, api.getUtils(path)));
|
---|
135 | }
|
---|
136 | }
|
---|
137 | };
|
---|
138 | });
|
---|
139 | exports.default = _default; |
---|