index.js 12 KB

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