index.js 11 KB

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