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.

76 lines
2.0 KiB

4 years ago
  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. if (!api.assertVersion) {
  9. api = Object.assign(copyApiObject(api), {
  10. assertVersion(range) {
  11. throwVersionError(range, api.version);
  12. }
  13. });
  14. }
  15. return builder(api, options || {}, dirname);
  16. };
  17. }
  18. function copyApiObject(api) {
  19. let proto = null;
  20. if (typeof api.version === "string" && /^7\./.test(api.version)) {
  21. proto = Object.getPrototypeOf(api);
  22. if (proto && (!has(proto, "version") || !has(proto, "transform") || !has(proto, "template") || !has(proto, "types"))) {
  23. proto = null;
  24. }
  25. }
  26. return Object.assign({}, proto, api);
  27. }
  28. function has(obj, key) {
  29. return Object.prototype.hasOwnProperty.call(obj, key);
  30. }
  31. function throwVersionError(range, version) {
  32. if (typeof range === "number") {
  33. if (!Number.isInteger(range)) {
  34. throw new Error("Expected string or integer value.");
  35. }
  36. range = `^${range}.0.0-0`;
  37. }
  38. if (typeof range !== "string") {
  39. throw new Error("Expected string or integer value.");
  40. }
  41. const limit = Error.stackTraceLimit;
  42. if (typeof limit === "number" && limit < 25) {
  43. Error.stackTraceLimit = 25;
  44. }
  45. let err;
  46. if (version.slice(0, 2) === "7.") {
  47. err = new Error(`Requires Babel "^7.0.0-beta.41", but was loaded with "${version}". ` + `You'll need to update your @babel/core version.`);
  48. } else {
  49. 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.`);
  50. }
  51. if (typeof limit === "number") {
  52. Error.stackTraceLimit = limit;
  53. }
  54. throw Object.assign(err, {
  55. code: "BABEL_VERSION_UNSUPPORTED",
  56. version,
  57. range
  58. });
  59. }