UNPKG

12.6 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 _runtimeCorejs2Definitions = _interopRequireDefault(require("./runtime-corejs2-definitions"));
59
60var _runtimeCorejs3Definitions = _interopRequireDefault(require("./runtime-corejs3-definitions"));
61
62var _helpers = require("./helpers");
63
64function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
65
66function resolveAbsoluteRuntime(moduleName, dirname) {
67 try {
68 return _path().default.dirname(_resolve().default.sync(`${moduleName}/package.json`, {
69 basedir: dirname
70 }));
71 } catch (err) {
72 if (err.code !== "MODULE_NOT_FOUND") throw err;
73 throw Object.assign(new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`), {
74 code: "BABEL_RUNTIME_NOT_FOUND",
75 runtime: moduleName,
76 dirname
77 });
78 }
79}
80
81function supportsStaticESM(caller) {
82 return !!(caller && caller.supportsStaticESM);
83}
84
85var _default = (0, _helperPluginUtils().declare)((api, options, dirname) => {
86 api.assertVersion(7);
87 const {
88 corejs,
89 helpers: useRuntimeHelpers = true,
90 regenerator: useRuntimeRegenerator = true,
91 useESModules = false,
92 version: runtimeVersion = "7.0.0-beta.0",
93 absoluteRuntime = false
94 } = options;
95 let proposals = false;
96 let rawVersion;
97
98 if (typeof corejs === "object" && corejs !== null) {
99 rawVersion = corejs.version;
100 proposals = Boolean(corejs.proposals);
101 } else {
102 rawVersion = corejs;
103 }
104
105 const corejsVersion = rawVersion ? Number(rawVersion) : false;
106
107 if (![false, 2, 3].includes(corejsVersion)) {
108 throw new Error(`The \`core-js\` version must be false, 2 or 3, but got ${JSON.stringify(rawVersion)}.`);
109 }
110
111 if (proposals && (!corejsVersion || corejsVersion < 3)) {
112 throw new Error("The 'proposals' option is only supported when using 'corejs: 3'");
113 }
114
115 if (typeof useRuntimeRegenerator !== "boolean") {
116 throw new Error("The 'regenerator' option must be undefined, or a boolean.");
117 }
118
119 if (typeof useRuntimeHelpers !== "boolean") {
120 throw new Error("The 'helpers' option must be undefined, or a boolean.");
121 }
122
123 if (typeof useESModules !== "boolean" && useESModules !== "auto") {
124 throw new Error("The 'useESModules' option must be undefined, or a boolean, or 'auto'.");
125 }
126
127 if (typeof absoluteRuntime !== "boolean" && typeof absoluteRuntime !== "string") {
128 throw new Error("The 'absoluteRuntime' option must be undefined, a boolean, or a string.");
129 }
130
131 if (typeof runtimeVersion !== "string") {
132 throw new Error(`The 'version' option must be a version string.`);
133 }
134
135 function has(obj, key) {
136 return Object.prototype.hasOwnProperty.call(obj, key);
137 }
138
139 function hasMapping(methods, name) {
140 return has(methods, name) && (proposals || methods[name].stable);
141 }
142
143 function hasStaticMapping(object, method) {
144 return has(StaticProperties, object) && hasMapping(StaticProperties[object], method);
145 }
146
147 function isNamespaced(path) {
148 const binding = path.scope.getBinding(path.node.name);
149 if (!binding) return false;
150 return binding.path.isImportNamespaceSpecifier();
151 }
152
153 function maybeNeedsPolyfill(path, methods, name) {
154 if (isNamespaced(path.get("object"))) return false;
155 if (!methods[name].types) return true;
156 const typeAnnotation = path.get("object").getTypeAnnotation();
157 const type = (0, _helpers.typeAnnotationToString)(typeAnnotation);
158 if (!type) return true;
159 return methods[name].types.some(name => name === type);
160 }
161
162 function resolvePropertyName(path, computed) {
163 const {
164 node
165 } = path;
166 if (!computed) return node.name;
167 if (path.isStringLiteral()) return node.value;
168 const result = path.evaluate();
169 return result.value;
170 }
171
172 if (has(options, "useBuiltIns")) {
173 if (options.useBuiltIns) {
174 throw new Error("The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.");
175 } else {
176 throw new Error("The 'useBuiltIns' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
177 }
178 }
179
180 if (has(options, "polyfill")) {
181 if (options.polyfill === false) {
182 throw new Error("The 'polyfill' option has been removed. The @babel/runtime " + "module now skips polyfilling by default.");
183 } else {
184 throw new Error("The 'polyfill' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
185 }
186 }
187
188 if (has(options, "moduleName")) {
189 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.");
190 }
191
192 const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
193 const injectCoreJS2 = corejsVersion === 2;
194 const injectCoreJS3 = corejsVersion === 3;
195 const injectCoreJS = corejsVersion !== false;
196 const moduleName = injectCoreJS3 ? "@babel/runtime-corejs3" : injectCoreJS2 ? "@babel/runtime-corejs2" : "@babel/runtime";
197 const corejsRoot = injectCoreJS3 && !proposals ? "core-js-stable" : "core-js";
198 const {
199 BuiltIns,
200 StaticProperties,
201 InstanceProperties
202 } = (injectCoreJS2 ? _runtimeCorejs2Definitions.default : _runtimeCorejs3Definitions.default)(runtimeVersion);
203 const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
204 let modulePath = moduleName;
205
206 if (absoluteRuntime !== false) {
207 modulePath = resolveAbsoluteRuntime(moduleName, _path().default.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime));
208 }
209
210 return {
211 name: "transform-runtime",
212
213 pre(file) {
214 if (useRuntimeHelpers) {
215 file.set("helperGenerator", name => {
216 if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) {
217 return;
218 }
219
220 const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
221 const blockHoist = isInteropHelper && !(0, _helperModuleImports().isModule)(file.path) ? 4 : undefined;
222 const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
223 return this.addDefaultImport(`${modulePath}/${helpersDir}/${name}`, name, blockHoist);
224 });
225 }
226
227 const cache = new Map();
228
229 this.addDefaultImport = (source, nameHint, blockHoist) => {
230 const cacheKey = (0, _helperModuleImports().isModule)(file.path);
231 const key = `${source}:${nameHint}:${cacheKey || ""}`;
232 let cached = cache.get(key);
233
234 if (cached) {
235 cached = _core().types.cloneNode(cached);
236 } else {
237 cached = (0, _helperModuleImports().addDefault)(file.path, source, {
238 importedInterop: "uncompiled",
239 nameHint,
240 blockHoist
241 });
242 cache.set(key, cached);
243 }
244
245 return cached;
246 };
247 },
248
249 visitor: {
250 ReferencedIdentifier(path) {
251 const {
252 node,
253 parent,
254 scope
255 } = path;
256 const {
257 name
258 } = node;
259
260 if (name === "regeneratorRuntime" && useRuntimeRegenerator) {
261 path.replaceWith(this.addDefaultImport(`${modulePath}/regenerator`, "regeneratorRuntime"));
262 return;
263 }
264
265 if (!injectCoreJS) return;
266 if (_core().types.isMemberExpression(parent)) return;
267 if (!hasMapping(BuiltIns, name)) return;
268 if (scope.getBindingIdentifier(name)) return;
269 path.replaceWith(this.addDefaultImport(`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`, name));
270 },
271
272 CallExpression(path) {
273 if (!injectCoreJS) return;
274 const {
275 node
276 } = path;
277 const {
278 callee
279 } = node;
280 if (!_core().types.isMemberExpression(callee)) return;
281 const {
282 object
283 } = callee;
284 const propertyName = resolvePropertyName(path.get("callee.property"), callee.computed);
285
286 if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) {
287 if (hasMapping(InstanceProperties, propertyName) && maybeNeedsPolyfill(path.get("callee"), InstanceProperties, propertyName)) {
288 let context1, context2;
289
290 if (_core().types.isIdentifier(object)) {
291 context1 = object;
292 context2 = _core().types.cloneNode(object);
293 } else {
294 context1 = path.scope.generateDeclaredUidIdentifier("context");
295 context2 = _core().types.assignmentExpression("=", context1, object);
296 }
297
298 node.callee = _core().types.memberExpression(_core().types.callExpression(this.addDefaultImport(`${moduleName}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`, `${propertyName}InstanceProperty`), [context2]), _core().types.identifier("call"));
299 node.arguments.unshift(context1);
300 return;
301 }
302 }
303
304 if (node.arguments.length) return;
305 if (!callee.computed) return;
306
307 if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
308 return;
309 }
310
311 path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${modulePath}/core-js/get-iterator`, "getIterator"), [object]));
312 },
313
314 BinaryExpression(path) {
315 if (!injectCoreJS) return;
316 if (path.node.operator !== "in") return;
317 if (!path.get("left").matchesPattern("Symbol.iterator")) return;
318 path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${modulePath}/core-js/is-iterable`, "isIterable"), [path.node.right]));
319 },
320
321 MemberExpression: {
322 enter(path) {
323 if (!injectCoreJS) return;
324 if (!path.isReferenced()) return;
325 const {
326 node
327 } = path;
328 const {
329 object
330 } = node;
331 if (!_core().types.isReferenced(object, node)) return;
332
333 if (!injectCoreJS2 && node.computed && path.get("property").matchesPattern("Symbol.iterator")) {
334 path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${moduleName}/core-js/get-iterator-method`, "getIteratorMethod"), [object]));
335 return;
336 }
337
338 const objectName = object.name;
339 const propertyName = resolvePropertyName(path.get("property"), node.computed);
340
341 if (path.scope.getBindingIdentifier(objectName) || !hasStaticMapping(objectName, propertyName)) {
342 if (injectCoreJS3 && hasMapping(InstanceProperties, propertyName) && maybeNeedsPolyfill(path, InstanceProperties, propertyName)) {
343 path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${moduleName}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`, `${propertyName}InstanceProperty`), [object]));
344 }
345
346 return;
347 }
348
349 path.replaceWith(this.addDefaultImport(`${modulePath}/${corejsRoot}/${StaticProperties[objectName][propertyName].path}`, `${objectName}$${propertyName}`));
350 },
351
352 exit(path) {
353 if (!injectCoreJS) return;
354 if (!path.isReferenced()) return;
355 if (path.node.computed) return;
356 const {
357 node
358 } = path;
359 const {
360 object
361 } = node;
362 const {
363 name
364 } = object;
365 if (!hasMapping(BuiltIns, name)) return;
366 if (path.scope.getBindingIdentifier(name)) return;
367 path.replaceWith(_core().types.memberExpression(this.addDefaultImport(`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`, name), node.property));
368 }
369
370 }
371 }
372 };
373});
374
375exports.default = _default;
\No newline at end of file