diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 2c722104..8644e0e5 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -48,7 +48,7 @@ const findConfig = (basePath: string): string | undefined => { return DEFAULT_EXTENSIONS.map((ext) => basePath + ext).find(fs.existsSync); }; -const resolveConfigPath = (root: string, customConfig?: string) => { +const resolveConfigPath = (root: string, customConfig?: string): string => { if (customConfig) { const customConfigPath = isAbsolute(customConfig) ? customConfig @@ -65,7 +65,7 @@ const resolveConfigPath = (root: string, customConfig?: string) => { return configFilePath; } - return undefined; + throw new Error(`${DEFAULT_CONFIG_NAME} not found`); }; export async function loadConfig({ @@ -77,7 +77,7 @@ export async function loadConfig({ path?: string; envMode?: string; }): Promise { - const configFilePath = resolveConfigPath(cwd, path)!; + const configFilePath = resolveConfigPath(cwd, path); const { content } = await loadRsbuildConfig({ cwd: dirname(configFilePath), path: configFilePath, diff --git a/packages/core/tests/config.test.ts b/packages/core/tests/config.test.ts index 0a3c873e..adf02713 100644 --- a/packages/core/tests/config.test.ts +++ b/packages/core/tests/config.test.ts @@ -8,8 +8,8 @@ vi.mock('rslog'); describe('Should load config file correctly', () => { test('Load config.js in cjs project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/cjs'); - const configDir = join(fixtureDir, 'rslib.config.js'); - const config = await loadConfig({ cwd: configDir }); + const configFilePath = join(fixtureDir, 'rslib.config.js'); + const config = await loadConfig({ path: configFilePath }); expect(config).toEqual({ lib: [], source: { @@ -18,15 +18,15 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); }); test('Load config.mjs in cjs project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/cjs'); - const configDir = join(fixtureDir, 'rslib.config.mjs'); - const config = await loadConfig({ cwd: configDir }); + const configFilePath = join(fixtureDir, 'rslib.config.mjs'); + const config = await loadConfig({ path: configFilePath }); expect(config).toEqual({ lib: [], source: { @@ -35,15 +35,15 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); }); test('Load config.ts in cjs project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/cjs'); - const configDir = join(fixtureDir, 'rslib.config.ts'); - const config = await loadConfig({ cwd: configDir }); + const configFilePath = join(fixtureDir, 'rslib.config.ts'); + const config = await loadConfig({ path: configFilePath }); expect(config).toEqual({ lib: [], source: { @@ -52,15 +52,15 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); }); test('Load config.cjs with defineConfig in cjs project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/cjs'); - const configDir = join(fixtureDir, 'rslib.config.cjs'); - const config = await loadConfig({ cwd: configDir }); + const configFilePath = join(fixtureDir, 'rslib.config.cjs'); + const config = await loadConfig({ path: configFilePath }); expect(config).toEqual({ lib: [], source: { @@ -69,15 +69,15 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); }); test('Load config.js in esm project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/esm'); - const configDir = join(fixtureDir, 'rslib.config.js'); - const config = await loadConfig({ cwd: configDir }); + const configFilePath = join(fixtureDir, 'rslib.config.js'); + const config = await loadConfig({ path: configFilePath }); expect(config).toEqual({ lib: [], source: { @@ -86,15 +86,15 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); }); test('Load config.cjs in esm project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/esm'); - const configDir = join(fixtureDir, 'rslib.config.cjs'); - const config = await loadConfig({ cwd: configDir }); + const configFilePath = join(fixtureDir, 'rslib.config.cjs'); + const config = await loadConfig({ path: configFilePath }); expect(config).toEqual({ lib: [], source: { @@ -103,15 +103,15 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); }); test('Load config.ts in esm project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/esm'); - const configDir = join(fixtureDir, 'rslib.config.ts'); - const config = await loadConfig({ cwd: configDir }); + const configFilePath = join(fixtureDir, 'rslib.config.ts'); + const config = await loadConfig({ path: configFilePath }); expect(config).toEqual({ lib: [], source: { @@ -120,16 +120,16 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); }); test('Load config.mjs with defineConfig in esm project', async () => { const fixtureDir = join(__dirname, 'fixtures/config/esm'); - const configDir = join(fixtureDir, 'rslib.config.mjs'); - const config = await loadConfig({ cwd: configDir }); - expect(config).toEqual({ + const configFilePath = join(fixtureDir, 'rslib.config.mjs'); + const config = await loadConfig({ path: configFilePath }); + expect(config).toMatchObject({ lib: [], source: { entry: { @@ -137,7 +137,7 @@ describe('Should load config file correctly', () => { }, }, _privateMeta: { - configFilePath: configDir, + configFilePath, }, }); });