Skip to content

Commit

Permalink
perf: optimize the resolveConfigPath
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Oct 15, 2024
1 parent 3419b1a commit 20353b1
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,43 @@ export function defineConfig(config: RslibConfigExport) {
return config;
}

const findConfig = (basePath: string): string | undefined => {
return DEFAULT_CONFIG_EXTENSIONS.map((ext) => basePath + ext).find(
fs.existsSync,
async function isFileExist(filePath: string) {
try {
await fs.promises.access(filePath, fs.constants.R_OK);
return true;
} catch (e) {
return false;
}
}

const findConfig = async (basePath: string): Promise<string | undefined> => {
const promises: Promise<false | string>[] = DEFAULT_CONFIG_EXTENSIONS.map(
async (ext) => {
const configPath = basePath + ext;
const isExist = await isFileExist(configPath);
return isExist ? configPath : false;
},
);
const configPaths = await Promise.all(promises);

return configPaths.find((i) => i !== false);
};

const resolveConfigPath = (root: string, customConfig?: string): string => {
const resolveConfigPath = async (
root: string,
customConfig?: string,
): Promise<string> => {
if (customConfig) {
const customConfigPath = isAbsolute(customConfig)
? customConfig
: join(root, customConfig);
if (fs.existsSync(customConfigPath)) {
if (await isFileExist(customConfigPath)) {
return customConfigPath;
}
logger.warn(`Cannot find config file: ${color.dim(customConfigPath)}\n`);
}

const configFilePath = findConfig(join(root, DEFAULT_CONFIG_NAME));
const configFilePath = await findConfig(join(root, DEFAULT_CONFIG_NAME));

if (configFilePath) {
return configFilePath;
Expand All @@ -105,7 +124,7 @@ export async function loadConfig({
path?: string;
envMode?: string;
}): Promise<RslibConfig> {
const configFilePath = resolveConfigPath(cwd, path);
const configFilePath = await resolveConfigPath(cwd, path);
const { content } = await loadRsbuildConfig({
cwd: dirname(configFilePath),
path: configFilePath,
Expand Down

0 comments on commit 20353b1

Please sign in to comment.