Skip to content

Commit 20353b1

Browse files
committed
perf: optimize the resolveConfigPath
1 parent 3419b1a commit 20353b1

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

packages/core/src/config.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,43 @@ export function defineConfig(config: RslibConfigExport) {
7070
return config;
7171
}
7272

73-
const findConfig = (basePath: string): string | undefined => {
74-
return DEFAULT_CONFIG_EXTENSIONS.map((ext) => basePath + ext).find(
75-
fs.existsSync,
73+
async function isFileExist(filePath: string) {
74+
try {
75+
await fs.promises.access(filePath, fs.constants.R_OK);
76+
return true;
77+
} catch (e) {
78+
return false;
79+
}
80+
}
81+
82+
const findConfig = async (basePath: string): Promise<string | undefined> => {
83+
const promises: Promise<false | string>[] = DEFAULT_CONFIG_EXTENSIONS.map(
84+
async (ext) => {
85+
const configPath = basePath + ext;
86+
const isExist = await isFileExist(configPath);
87+
return isExist ? configPath : false;
88+
},
7689
);
90+
const configPaths = await Promise.all(promises);
91+
92+
return configPaths.find((i) => i !== false);
7793
};
7894

79-
const resolveConfigPath = (root: string, customConfig?: string): string => {
95+
const resolveConfigPath = async (
96+
root: string,
97+
customConfig?: string,
98+
): Promise<string> => {
8099
if (customConfig) {
81100
const customConfigPath = isAbsolute(customConfig)
82101
? customConfig
83102
: join(root, customConfig);
84-
if (fs.existsSync(customConfigPath)) {
103+
if (await isFileExist(customConfigPath)) {
85104
return customConfigPath;
86105
}
87106
logger.warn(`Cannot find config file: ${color.dim(customConfigPath)}\n`);
88107
}
89108

90-
const configFilePath = findConfig(join(root, DEFAULT_CONFIG_NAME));
109+
const configFilePath = await findConfig(join(root, DEFAULT_CONFIG_NAME));
91110

92111
if (configFilePath) {
93112
return configFilePath;
@@ -105,7 +124,7 @@ export async function loadConfig({
105124
path?: string;
106125
envMode?: string;
107126
}): Promise<RslibConfig> {
108-
const configFilePath = resolveConfigPath(cwd, path);
127+
const configFilePath = await resolveConfigPath(cwd, path);
109128
const { content } = await loadRsbuildConfig({
110129
cwd: dirname(configFilePath),
111130
path: configFilePath,

0 commit comments

Comments
 (0)