find-config.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findConfig = findConfig;
  6. var _findUp = _interopRequireDefault(require("next/dist/compiled/find-up"));
  7. var _fs = _interopRequireDefault(require("fs"));
  8. var _json5 = _interopRequireDefault(require("next/dist/compiled/json5"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. async function findConfig(directory, key) {
  15. // `package.json` configuration always wins. Let's check that first.
  16. const packageJsonPath = await (0, _findUp).default("package.json", {
  17. cwd: directory
  18. });
  19. if (packageJsonPath) {
  20. const packageJson = require(packageJsonPath);
  21. if (packageJson[key] != null && typeof packageJson[key] === "object") {
  22. return packageJson[key];
  23. }
  24. }
  25. // If we didn't find the configuration in `package.json`, we should look for
  26. // known filenames.
  27. const filePath = await (0, _findUp).default([
  28. `.${key}rc.json`,
  29. `${key}.config.json`,
  30. `.${key}rc.js`,
  31. `${key}.config.js`,
  32. `${key}.config.cjs`,
  33. ], {
  34. cwd: directory
  35. });
  36. if (filePath) {
  37. if (filePath.endsWith(".js") || filePath.endsWith(".cjs")) {
  38. return require(filePath);
  39. }
  40. // We load JSON contents with JSON5 to allow users to comment in their
  41. // configuration file. This pattern was popularized by TypeScript.
  42. const fileContents = _fs.default.readFileSync(filePath, "utf8");
  43. return _json5.default.parse(fileContents);
  44. }
  45. return null;
  46. }
  47. //# sourceMappingURL=find-config.js.map