Dashboard sipadu mbip
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.

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 isNamespaced(path) {
  112. const binding = path.scope.getBinding(path.node.name);
  113. if (!binding) return false;
  114. return binding.path.isImportNamespaceSpecifier();
  115. }
  116. function maybeNeedsPolyfill(path, methods, name) {
  117. if (isNamespaced(path.get("object"))) return false;
  118. if (!methods[name].types) return true;
  119. const typeAnnotation = path.get("object").getTypeAnnotation();
  120. const type = (0, _helpers.typeAnnotationToString)(typeAnnotation);
  121. if (!type) return true;
  122. return methods[name].types.some(name => name === type);
  123. }
  124. function resolvePropertyName(path, computed) {
  125. const {
  126. node
  127. } = path;
  128. if (!computed) return node.name;
  129. if (path.isStringLiteral()) return node.value;
  130. const result = path.evaluate();
  131. return result.value;
  132. }
  133. if (has(options, "useBuiltIns")) {
  134. if (options.useBuiltIns) {
  135. throw new Error("The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.");
  136. } else {
  137. throw new Error("The 'useBuiltIns' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
  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. if (has(options, "moduleName")) {
  148. 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.");
  149. }
  150. const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
  151. const injectCoreJS2 = corejsVersion === 2;
  152. const injectCoreJS3 = corejsVersion === 3;
  153. const injectCoreJS = corejsVersion !== false;
  154. const moduleName = injectCoreJS3 ? "@babel/runtime-corejs3" : injectCoreJS2 ? "@babel/runtime-corejs2" : "@babel/runtime";
  155. const corejsRoot = injectCoreJS3 && !proposals ? "core-js-stable" : "core-js";
  156. const {
  157. BuiltIns,
  158. StaticProperties,
  159. InstanceProperties
  160. } = (injectCoreJS2 ? _runtimeCorejs2Definitions.default : _runtimeCorejs3Definitions.default)(runtimeVersion);
  161. const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
  162. let modulePath = moduleName;
  163. if (absoluteRuntime !== false) {
  164. modulePath = resolveAbsoluteRuntime(moduleName, _path().default.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime));
  165. }
  166. return {
  167. name: "transform-runtime",
  168. pre(file) {
  169. if (useRuntimeHelpers) {
  170. file.set("helperGenerator", name => {
  171. if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) {
  172. return;
  173. }
  174. const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
  175. const blockHoist = isInteropHelper && !(0, _helperModuleImports().isModule)(file.path) ? 4 : undefined;
  176. const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
  177. return this.addDefaultImport(`${modulePath}/${helpersDir}/${name}`, name, blockHoist);
  178. });
  179. }
  180. const cache = new Map();
  181. this.addDefaultImport = (source, nameHint, blockHoist) => {
  182. const cacheKey = (0, _helperModuleImports().isModule)(file.path);
  183. const key = `${source}:${nameHint}:${cacheKey || ""}`;
  184. let cached = cache.get(key);
  185. if (cached) {
  186. cached = _core().types.cloneNode(cached);
  187. } else {
  188. cached = (0, _helperModuleImports().addDefault)(file.path, source, {
  189. importedInterop: "uncompiled",
  190. nameHint,
  191. blockHoist
  192. });
  193. cache.set(key, cached);
  194. }
  195. return cached;
  196. };
  197. },
  198. visitor: {
  199. ReferencedIdentifier(path) {
  200. const {
  201. node,
  202. parent,
  203. scope
  204. } = path;
  205. const {
  206. name
  207. } = node;
  208. if (name === "regeneratorRuntime" && useRuntimeRegenerator) {
  209. path.replaceWith(this.addDefaultImport(`${modulePath}/regenerator`, "regeneratorRuntime"));
  210. return;
  211. }
  212. if (!injectCoreJS) return;
  213. if (_core().types.isMemberExpression(parent)) return;
  214. if (!hasMapping(BuiltIns, name)) return;
  215. if (scope.getBindingIdentifier(name)) return;
  216. path.replaceWith(this.addDefaultImport(`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`, name));
  217. },
  218. CallExpression(path) {
  219. if (!injectCoreJS) return;
  220. const {
  221. node
  222. } = path;
  223. const {
  224. callee
  225. } = node;
  226. if (!_core().types.isMemberExpression(callee)) return;
  227. const {
  228. object
  229. } = callee;
  230. const propertyName = resolvePropertyName(path.get("callee.property"), callee.computed);
  231. if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) {
  232. if (hasMapping(InstanceProperties, propertyName) && maybeNeedsPolyfill(path.get("callee"), InstanceProperties, propertyName)) {
  233. let context1, context2;
  234. if (_core().types.isIdentifier(object)) {
  235. context1 = object;
  236. context2 = _core().types.cloneNode(object);
  237. } else {
  238. context1 = path.scope.generateDeclaredUidIdentifier("context");
  239. context2 = _core().types.assignmentExpression("=", context1, object);
  240. }
  241. node.callee = _core().types.memberExpression(_core().types.callExpression(this.addDefaultImport(`${moduleName}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`, `${propertyName}InstanceProperty`), [context2]), _core().types.identifier("call"));
  242. node.arguments.unshift(context1);
  243. return;
  244. }
  245. }
  246. if (node.arguments.length) return;
  247. if (!callee.computed) return;
  248. if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
  249. return;
  250. }
  251. path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${modulePath}/core-js/get-iterator`, "getIterator"), [object]));
  252. },
  253. BinaryExpression(path) {
  254. if (!injectCoreJS) return;
  255. if (path.node.operator !== "in") return;
  256. if (!path.get("left").matchesPattern("Symbol.iterator")) return;
  257. path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${modulePath}/core-js/is-iterable`, "isIterable"), [path.node.right]));
  258. },
  259. MemberExpression: {
  260. enter(path) {
  261. if (!injectCoreJS) return;
  262. if (!path.isReferenced()) return;
  263. const {
  264. node
  265. } = path;
  266. const {
  267. object
  268. } = node;
  269. if (!_core().types.isReferenced(object, node)) return;
  270. if (!injectCoreJS2 && node.computed && path.get("property").matchesPattern("Symbol.iterator")) {
  271. path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${moduleName}/core-js/get-iterator-method`, "getIteratorMethod"), [object]));
  272. return;
  273. }
  274. const objectName = object.name;
  275. const propertyName = resolvePropertyName(path.get("property"), node.computed);
  276. if (path.scope.getBindingIdentifier(objectName) || !hasStaticMapping(objectName, propertyName)) {
  277. if (injectCoreJS3 && hasMapping(InstanceProperties, propertyName) && maybeNeedsPolyfill(path, InstanceProperties, propertyName)) {
  278. path.replaceWith(_core().types.callExpression(this.addDefaultImport(`${moduleName}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`, `${propertyName}InstanceProperty`), [object]));
  279. }
  280. return;
  281. }
  282. path.replaceWith(this.addDefaultImport(`${modulePath}/${corejsRoot}/${StaticProperties[objectName][propertyName].path}`, `${objectName}$${propertyName}`));
  283. },
  284. exit(path) {
  285. if (!injectCoreJS) return;
  286. if (!path.isReferenced()) return;
  287. if (path.node.computed) return;
  288. const {
  289. node
  290. } = path;
  291. const {
  292. object
  293. } = node;
  294. const {
  295. name
  296. } = object;
  297. if (!hasMapping(BuiltIns, name)) return;
  298. if (path.scope.getBindingIdentifier(name)) return;
  299. path.replaceWith(_core().types.memberExpression(this.addDefaultImport(`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`, name), node.property));
  300. }
  301. }
  302. }
  303. };
  304. });
  305. exports.default = _default;