index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.declare = declare;
  6. function declare(builder) {
  7. return (api, options, dirname) => {
  8. var _clonedApi2;
  9. let clonedApi;
  10. for (const name of Object.keys(apiPolyfills)) {
  11. var _clonedApi;
  12. if (api[name]) continue;
  13. clonedApi = (_clonedApi = clonedApi) != null ? _clonedApi : copyApiObject(api);
  14. clonedApi[name] = apiPolyfills[name](clonedApi);
  15. }
  16. return builder((_clonedApi2 = clonedApi) != null ? _clonedApi2 : api, options || {}, dirname);
  17. };
  18. }
  19. const apiPolyfills = {
  20. assertVersion: api => range => {
  21. throwVersionError(range, api.version);
  22. },
  23. targets: () => () => {
  24. return {};
  25. },
  26. assumption: () => () => {}
  27. };
  28. function copyApiObject(api) {
  29. let proto = null;
  30. if (typeof api.version === "string" && /^7\./.test(api.version)) {
  31. proto = Object.getPrototypeOf(api);
  32. if (proto && (!has(proto, "version") || !has(proto, "transform") || !has(proto, "template") || !has(proto, "types"))) {
  33. proto = null;
  34. }
  35. }
  36. return Object.assign({}, proto, api);
  37. }
  38. function has(obj, key) {
  39. return Object.prototype.hasOwnProperty.call(obj, key);
  40. }
  41. function throwVersionError(range, version) {
  42. if (typeof range === "number") {
  43. if (!Number.isInteger(range)) {
  44. throw new Error("Expected string or integer value.");
  45. }
  46. range = `^${range}.0.0-0`;
  47. }
  48. if (typeof range !== "string") {
  49. throw new Error("Expected string or integer value.");
  50. }
  51. const limit = Error.stackTraceLimit;
  52. if (typeof limit === "number" && limit < 25) {
  53. Error.stackTraceLimit = 25;
  54. }
  55. let err;
  56. if (version.slice(0, 2) === "7.") {
  57. err = new Error(`Requires Babel "^7.0.0-beta.41", but was loaded with "${version}". ` + `You'll need to update your @babel/core version.`);
  58. } else {
  59. err = new Error(`Requires Babel "${range}", but was loaded with "${version}". ` + `If you are sure you have a compatible version of @babel/core, ` + `it is likely that something in your build process is loading the ` + `wrong version. Inspect the stack trace of this error to look for ` + `the first entry that doesn't mention "@babel/core" or "babel-core" ` + `to see what is calling Babel.`);
  60. }
  61. if (typeof limit === "number") {
  62. Error.stackTraceLimit = limit;
  63. }
  64. throw Object.assign(err, {
  65. code: "BABEL_VERSION_UNSUPPORTED",
  66. version,
  67. range
  68. });
  69. }