config.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.resolveConfig = resolveConfig;
  4. exports.resolveConfigFile = resolveConfigFile;
  5. exports.loadConfig = loadConfig;
  6. exports.DEFAULT_CONFIG = void 0;
  7. var _cosmiconfig = require("cosmiconfig");
  8. const DEFAULT_CONFIG = {
  9. dimensions: true,
  10. expandProps: 'end',
  11. icon: false,
  12. native: false,
  13. typescript: false,
  14. prettier: true,
  15. prettierConfig: null,
  16. memo: false,
  17. ref: false,
  18. replaceAttrValues: null,
  19. svgProps: null,
  20. svgo: true,
  21. svgoConfig: null,
  22. template: null,
  23. titleProp: false,
  24. runtimeConfig: true,
  25. plugins: null,
  26. namedExport: 'ReactComponent'
  27. };
  28. exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
  29. const explorer = (0, _cosmiconfig.cosmiconfig)('svgr', {
  30. sync: true,
  31. cache: true,
  32. rcExtensions: true
  33. });
  34. const explorerSync = (0, _cosmiconfig.cosmiconfigSync)('svgr', {
  35. sync: true,
  36. cache: true,
  37. rcExtensions: true
  38. });
  39. async function resolveConfig(searchFrom, configFile) {
  40. if (configFile == null) {
  41. const result = await explorer.search(searchFrom);
  42. return result ? result.config : null;
  43. }
  44. const result = await explorer.load(configFile);
  45. return result ? result.config : null;
  46. }
  47. resolveConfig.sync = (searchFrom, configFile) => {
  48. if (configFile == null) {
  49. const result = explorerSync.search(searchFrom);
  50. return result ? result.config : null;
  51. }
  52. const result = explorerSync.load(configFile);
  53. return result ? result.config : null;
  54. };
  55. async function resolveConfigFile(filePath) {
  56. const result = await explorer.search(filePath);
  57. return result ? result.filepath : null;
  58. }
  59. resolveConfigFile.sync = filePath => {
  60. const result = explorerSync.search(filePath);
  61. return result ? result.filepath : null;
  62. };
  63. async function loadConfig({
  64. configFile,
  65. ...baseConfig
  66. }, state = {}) {
  67. const rcConfig = state.filePath && baseConfig.runtimeConfig !== false ? await resolveConfig(state.filePath, configFile) : {};
  68. return { ...DEFAULT_CONFIG,
  69. ...rcConfig,
  70. ...baseConfig
  71. };
  72. }
  73. loadConfig.sync = ({
  74. configFile,
  75. ...baseConfig
  76. }, state = {}) => {
  77. const rcConfig = state.filePath && baseConfig.runtimeConfig !== false ? resolveConfig.sync(state.filePath, configFile) : {};
  78. return { ...DEFAULT_CONFIG,
  79. ...rcConfig,
  80. ...baseConfig
  81. };
  82. };