UNPKG

9.1 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function _path() {
9 const data = _interopRequireDefault(require("path"));
10
11 _path = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _resolve() {
19 const data = _interopRequireDefault(require("resolve"));
20
21 _resolve = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _helperPluginUtils() {
29 const data = require("@babel/helper-plugin-utils");
30
31 _helperPluginUtils = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _helperModuleImports() {
39 const data = require("@babel/helper-module-imports");
40
41 _helperModuleImports = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _core() {
49 const data = require("@babel/core");
50
51 _core = function () {
52 return data;
53 };
54
55 return data;
56}
57
58var _definitions = _interopRequireDefault(require("./definitions"));
59
60function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
61
62function resolveAbsoluteRuntime(moduleName, dirname) {
63 try {
64 return _path().default.dirname(_resolve().default.sync(`${moduleName}/package.json`, {
65 basedir: dirname
66 }));
67 } catch (err) {
68 if (err.code !== "MODULE_NOT_FOUND") throw err;
69 throw Object.assign(new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`), {
70 code: "BABEL_RUNTIME_NOT_FOUND",
71 runtime: moduleName,
72 dirname
73 });
74 }
75}
76
77function supportsStaticESM(caller) {
78 return !!(caller && caller.supportsStaticESM);
79}
80
81var _default = (0, _helperPluginUtils().declare)((api, options, dirname) => {
82 api.assertVersion(7);
83 const {
84 corejs: corejsVersion = false,
85 helpers: useRuntimeHelpers = true,
86 regenerator: useRuntimeRegenerator = true,
87 useESModules = false,
88 version: runtimeVersion = "7.0.0-beta.0",
89 absoluteRuntime = false
90 } = options;
91 const definitions = (0, _definitions.default)(runtimeVersion);
92
93 if (typeof useRuntimeRegenerator !== "boolean") {
94 throw new Error("The 'regenerator' option must be undefined, or a boolean.");
95 }
96
97 if (typeof useRuntimeHelpers !== "boolean") {
98 throw new Error("The 'helpers' option must be undefined, or a boolean.");
99 }
100
101 if (typeof useESModules !== "boolean" && useESModules !== "auto") {
102 throw new Error("The 'useESModules' option must be undefined, or a boolean, or 'auto'.");
103 }
104
105 if (typeof absoluteRuntime !== "boolean" && typeof absoluteRuntime !== "string") {
106 throw new Error("The 'absoluteRuntime' option must be undefined, a boolean, or a string.");
107 }
108
109 if (corejsVersion !== false && (typeof corejsVersion !== "number" || corejsVersion !== 2) && (typeof corejsVersion !== "string" || corejsVersion !== "2")) {
110 throw new Error(`The 'corejs' option must be undefined, false, 2 or '2', ` + `but got ${JSON.stringify(corejsVersion)}.`);
111 }
112
113 if (typeof runtimeVersion !== "string") {
114 throw new Error(`The 'version' option must be a version string.`);
115 }
116
117 function has(obj, key) {
118 return Object.prototype.hasOwnProperty.call(obj, key);
119 }
120
121 if (has(options, "useBuiltIns")) {
122 if (options.useBuiltIns) {
123 throw new Error("The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.");
124 } else {
125 throw new Error("The 'useBuiltIns' option has been removed. Use the 'corejs'" + "option with value '2' to polyfill with CoreJS 2.x via @babel/runtime.");
126 }
127 }
128
129 if (has(options, "polyfill")) {
130 if (options.polyfill === false) {
131 throw new Error("The 'polyfill' option has been removed. The @babel/runtime " + "module now skips polyfilling by default.");
132 } else {
133 throw new Error("The 'polyfill' option has been removed. Use the 'corejs'" + "option with value '2' to polyfill with CoreJS 2.x via @babel/runtime.");
134 }
135 }
136
137 if (has(options, "moduleName")) {
138 throw new Error("The 'moduleName' option has been removed. @babel/transform-runtime " + "no longer supports arbitrary runtimes. If you were using this to " + "set an absolute path for Babel's standard runtimes, please use the " + "'absoluteRuntime' option.");
139 }
140
141 const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
142 const injectCoreJS2 = `${corejsVersion}` === "2";
143 const moduleName = injectCoreJS2 ? "@babel/runtime-corejs2" : "@babel/runtime";
144 const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
145 let modulePath = moduleName;
146
147 if (absoluteRuntime !== false) {
148 modulePath = resolveAbsoluteRuntime(moduleName, _path().default.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime));
149 }
150
151 return {
152 pre(file) {
153 if (useRuntimeHelpers) {
154 file.set("helperGenerator", name => {
155 if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) {
156 return;
157 }
158
159 const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
160 const blockHoist = isInteropHelper && !(0, _helperModuleImports().isModule)(file.path) ? 4 : undefined;
161 const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
162 return this.addDefaultImport(`${modulePath}/${helpersDir}/${name}`, name, blockHoist);
163 });
164 }
165
166 const cache = new Map();
167
168 this.addDefaultImport = (source, nameHint, blockHoist) => {
169 const cacheKey = (0, _helperModuleImports().isModule)(file.path);
170 const key = `${source}:${nameHint}:${cacheKey || ""}`;
171 let cached = cache.get(key);
172
173 if (cached) {
174 cached = _core().types.cloneNode(cached);
175 } else {
176 cached = (0, _helperModuleImports().addDefault)(file.path, source, {
177 importedInterop: "uncompiled",
178 nameHint,
179 blockHoist
180 });
181 cache.set(key, cached);
182 }
183
184 return cached;
185 };
186 },
187
188 visitor: {
189 ReferencedIdentifier(path) {
190 const {
191 node,
192 parent,
193 scope
194 } = path;
195
196 if (node.name === "regeneratorRuntime" && useRuntimeRegenerator) {
197 path.replaceWith(this.addDefaultImport(`${modulePath}/regenerator`, "regeneratorRuntime"));
198 return;
199 }
200
201 if (!injectCoreJS2) return;
202 if (_core().types.isMemberExpression(parent)) return;
203 if (!has(definitions.builtins, node.name)) return;
204 if (scope.getBindingIdentifier(node.name)) return;
205 path.replaceWith(this.addDefaultImport(`${modulePath}/core-js/${definitions.builtins[node.name]}`, node.name));
206 },
207
208 CallExpression(path) {
209 if (!injectCoreJS2) return;
210 if (path.node.arguments.length) return;
211 const callee = path.node.callee;
212 if (!_core().types.isMemberExpression(callee)) return;
213 if (!callee.computed) return;
214
215 if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
216 return;
217 }
218
219 path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${modulePath}/core-js/get-iterator`, "getIterator"), [callee.object]));
220 },
221
222 BinaryExpression(path) {
223 if (!injectCoreJS2) return;
224 if (path.node.operator !== "in") return;
225 if (!path.get("left").matchesPattern("Symbol.iterator")) return;
226 path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${modulePath}/core-js/is-iterable`, "isIterable"), [path.node.right]));
227 },
228
229 MemberExpression: {
230 enter(path) {
231 if (!injectCoreJS2) return;
232 if (!path.isReferenced()) return;
233 const {
234 node
235 } = path;
236 const obj = node.object;
237 const prop = node.property;
238 if (!_core().types.isReferenced(obj, node)) return;
239 if (node.computed) return;
240 if (!has(definitions.methods, obj.name)) return;
241 const methods = definitions.methods[obj.name];
242 if (!has(methods, prop.name)) return;
243 if (path.scope.getBindingIdentifier(obj.name)) return;
244
245 if (obj.name === "Object" && prop.name === "defineProperty" && path.parentPath.isCallExpression()) {
246 const call = path.parentPath.node;
247
248 if (call.arguments.length === 3 && _core().types.isLiteral(call.arguments[1])) {
249 return;
250 }
251 }
252
253 path.replaceWith(this.addDefaultImport(`${modulePath}/core-js/${methods[prop.name]}`, `${obj.name}$${prop.name}`));
254 },
255
256 exit(path) {
257 if (!injectCoreJS2) return;
258 if (!path.isReferenced()) return;
259 const {
260 node
261 } = path;
262 const obj = node.object;
263 if (!has(definitions.builtins, obj.name)) return;
264 if (path.scope.getBindingIdentifier(obj.name)) return;
265 path.replaceWith(_core().types.memberExpression(this.addDefaultImport(`${modulePath}/core-js/${definitions.builtins[obj.name]}`, obj.name), node.property, node.computed));
266 }
267
268 }
269 }
270 };
271});
272
273exports.default = _default;
\No newline at end of file