版博士V2.0程序
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.
 
 
 
 

83 lines
2.0 KiB

  1. // src/index.ts
  2. import fs from "fs";
  3. import path from "path";
  4. import _debug from "debug";
  5. import _jiti from "jiti";
  6. var jiti;
  7. var defaultConfigureFiles = [
  8. "windi.config.ts",
  9. "windi.config.js",
  10. "windi.config.mjs",
  11. "windi.config.cjs",
  12. "windicss.config.ts",
  13. "windicss.config.js",
  14. "windicss.config.mjs",
  15. "windicss.config.cjs",
  16. "tailwind.config.ts",
  17. "tailwind.config.js",
  18. "tailwind.config.mjs",
  19. "tailwind.config.cjs"
  20. ];
  21. function loadConfiguration(options) {
  22. if (!jiti)
  23. jiti = _jiti(process.cwd(), { requireCache: false, cache: false, v8cache: false });
  24. let resolved = {};
  25. let configFilePath;
  26. let error;
  27. const {
  28. name = "windicss-config",
  29. config,
  30. root = process.cwd(),
  31. configFiles: configureFiles = defaultConfigureFiles,
  32. onConfigurationError = (e) => {
  33. throw e;
  34. },
  35. onConfigurationNotFound = (path2) => {
  36. console.warn(`[${name}] config file "${path2}" not found, ignored`);
  37. }
  38. } = options;
  39. const debugConfig = _debug(`${name}:config`);
  40. if (typeof config === "string" || !config) {
  41. if (!config) {
  42. for (const name2 of configureFiles) {
  43. const tryPath = path.resolve(root, name2);
  44. if (fs.existsSync(tryPath)) {
  45. configFilePath = tryPath;
  46. break;
  47. }
  48. }
  49. } else {
  50. configFilePath = path.resolve(root, config);
  51. if (!fs.existsSync(configFilePath)) {
  52. onConfigurationNotFound(config);
  53. configFilePath = void 0;
  54. }
  55. }
  56. if (configFilePath) {
  57. try {
  58. debugConfig("loading from ", configFilePath);
  59. resolved = jiti(configFilePath);
  60. if (resolved.default)
  61. resolved = resolved.default;
  62. } catch (e) {
  63. error = e;
  64. configFilePath = void 0;
  65. resolved = {};
  66. onConfigurationError?.(e);
  67. }
  68. }
  69. } else {
  70. resolved = config;
  71. }
  72. debugConfig(resolved);
  73. return {
  74. error,
  75. config: resolved,
  76. filepath: configFilePath
  77. };
  78. }
  79. export {
  80. defaultConfigureFiles,
  81. loadConfiguration
  82. };