You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
12 KiB

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