diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index b7a990eb9..2fe97babd 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -890,7 +890,7 @@ const composeEntryConfig = async ( root: string, cssModulesAuto: CssLoaderOptionsAuto, ): Promise<{ entryConfig: EnvironmentConfig; lcp: string | null }> => { - let entries = rawEntry; + let entries: RsbuildConfigEntry = rawEntry; if (!entries) { // In bundle mode, return directly to let Rsbuild apply default entry to './src/index.ts' @@ -904,6 +904,15 @@ const composeEntryConfig = async ( }; } + // Type check to ensure entries is of the expected type + if (typeof entries !== 'object') { + throw new Error( + `The ${color.cyan('source.entry')} configuration should be an object, but received ${typeof entries}: ${color.cyan( + entries, + )}. Checkout ${color.green('https://lib.rsbuild.dev/config/rsbuild/source#sourceentry')} for more details.`, + ); + } + if (bundle !== false) { const entryErrorReasons: string[] = []; traverseEntryQuery(entries, (entry) => { diff --git a/tests/integration/entry/index.test.ts b/tests/integration/entry/index.test.ts index 499b3aad9..62e429246 100644 --- a/tests/integration/entry/index.test.ts +++ b/tests/integration/entry/index.test.ts @@ -154,4 +154,17 @@ test('validate entry and throw errors', async () => { expect(stripAnsi(errMsg)).toMatchInlineSnapshot( `"Error: Can't resolve the entry "./src/main.ts" at the location /tests/integration/entry/validate/src/main.ts. Please ensure that the file exists."`, ); + + try { + await buildAndGetResults({ + fixturePath, + configPath: 'bundlelessWithString.config.ts', + }); + } catch (e) { + errMsg = (e as Error).message; + } + + expect(stripAnsi(errMsg)).toMatchInlineSnapshot( + `"The source.entry configuration should be an object, but received string: ./src/**. Checkout https://lib.rsbuild.dev/config/rsbuild/source#sourceentry for more details."`, + ); }); diff --git a/tests/integration/entry/validate/bundlelessWithString.config.ts b/tests/integration/entry/validate/bundlelessWithString.config.ts new file mode 100644 index 000000000..265d36787 --- /dev/null +++ b/tests/integration/entry/validate/bundlelessWithString.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + source: { + // @ts-expect-error test error config + entry: './src/**', + }, + }), + ], +}); diff --git a/website/docs/en/config/rsbuild/source.mdx b/website/docs/en/config/rsbuild/source.mdx index 35e7e215c..d5ee21ca7 100644 --- a/website/docs/en/config/rsbuild/source.mdx +++ b/website/docs/en/config/rsbuild/source.mdx @@ -25,8 +25,22 @@ Used to set the entry modules for building. In Rslib, the default value is: -- bundle mode: `src/index.(ts|js|tsx|jsx|mjs|cjs)` -- bundleless mode: `src/**` +- bundle mode: + +```ts +const defaultEntry = { + // default support for other suffixes such as ts, tsx, jsx, mjs, cjs + index: 'src/index.js', +}; +``` + +- bundleless mode: + +```ts +const defaultEntry = { + index: 'src/**', +}; +``` :::info Check out the [lib.bundle](/config/lib/bundle#set-entry) to learn more about how to set entry for bundle and bundleless project. diff --git a/website/docs/zh/config/rsbuild/source.mdx b/website/docs/zh/config/rsbuild/source.mdx index 6ec1053d1..068af35ee 100644 --- a/website/docs/zh/config/rsbuild/source.mdx +++ b/website/docs/zh/config/rsbuild/source.mdx @@ -24,8 +24,22 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge'; 在 Rslib 中,默认值为: -- bundle 模式:`src/index.(ts|js|tsx|jsx|mjs|cjs)` -- bundleless 模式:`src/**` +- bundle 模式: + +```ts +const defaultEntry = { + // 默认支持其他后缀,如 ts、tsx、jsx、mjs、cjs + index: 'src/index.js', +}; +``` + +- bundleless 模式: + +```ts +const defaultEntry = { + index: 'src/**', +}; +``` :::info 参考 [lib.bundle](/config/lib/bundle#set-entry) 进一步了解如何为 bundle 和 bundleless 项目设置入口。