UNPKG

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