Skip to content

Commit 1a4a0a8

Browse files
authored
fix: validate entry type (#685)
1 parent 860dba6 commit 1a4a0a8

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

packages/core/src/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ const composeEntryConfig = async (
890890
root: string,
891891
cssModulesAuto: CssLoaderOptionsAuto,
892892
): Promise<{ entryConfig: EnvironmentConfig; lcp: string | null }> => {
893-
let entries = rawEntry;
893+
let entries: RsbuildConfigEntry = rawEntry;
894894

895895
if (!entries) {
896896
// In bundle mode, return directly to let Rsbuild apply default entry to './src/index.ts'
@@ -904,6 +904,15 @@ const composeEntryConfig = async (
904904
};
905905
}
906906

907+
// Type check to ensure entries is of the expected type
908+
if (typeof entries !== 'object') {
909+
throw new Error(
910+
`The ${color.cyan('source.entry')} configuration should be an object, but received ${typeof entries}: ${color.cyan(
911+
entries,
912+
)}. Checkout ${color.green('https://lib.rsbuild.dev/config/rsbuild/source#sourceentry')} for more details.`,
913+
);
914+
}
915+
907916
if (bundle !== false) {
908917
const entryErrorReasons: string[] = [];
909918
traverseEntryQuery(entries, (entry) => {

tests/integration/entry/index.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,17 @@ test('validate entry and throw errors', async () => {
154154
expect(stripAnsi(errMsg)).toMatchInlineSnapshot(
155155
`"Error: Can't resolve the entry "./src/main.ts" at the location <ROOT>/tests/integration/entry/validate/src/main.ts. Please ensure that the file exists."`,
156156
);
157+
158+
try {
159+
await buildAndGetResults({
160+
fixturePath,
161+
configPath: 'bundlelessWithString.config.ts',
162+
});
163+
} catch (e) {
164+
errMsg = (e as Error).message;
165+
}
166+
167+
expect(stripAnsi(errMsg)).toMatchInlineSnapshot(
168+
`"The source.entry configuration should be an object, but received string: ./src/**. Checkout https://lib.rsbuild.dev/config/rsbuild/source#sourceentry for more details."`,
169+
);
157170
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
bundle: false,
8+
source: {
9+
// @ts-expect-error test error config
10+
entry: './src/**',
11+
},
12+
}),
13+
],
14+
});

website/docs/en/config/rsbuild/source.mdx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ Used to set the entry modules for building.
2525

2626
In Rslib, the default value is:
2727

28-
- bundle mode: `src/index.(ts|js|tsx|jsx|mjs|cjs)`
29-
- bundleless mode: `src/**`
28+
- bundle mode:
29+
30+
```ts
31+
const defaultEntry = {
32+
// default support for other suffixes such as ts, tsx, jsx, mjs, cjs
33+
index: 'src/index.js',
34+
};
35+
```
36+
37+
- bundleless mode:
38+
39+
```ts
40+
const defaultEntry = {
41+
index: 'src/**',
42+
};
43+
```
3044

3145
:::info
3246
Check out the [lib.bundle](/config/lib/bundle#set-entry) to learn more about how to set entry for bundle and bundleless project.

website/docs/zh/config/rsbuild/source.mdx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,22 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
2424

2525
在 Rslib 中,默认值为:
2626

27-
- bundle 模式:`src/index.(ts|js|tsx|jsx|mjs|cjs)`
28-
- bundleless 模式:`src/**`
27+
- bundle 模式:
28+
29+
```ts
30+
const defaultEntry = {
31+
// 默认支持其他后缀,如 ts、tsx、jsx、mjs、cjs
32+
index: 'src/index.js',
33+
};
34+
```
35+
36+
- bundleless 模式:
37+
38+
```ts
39+
const defaultEntry = {
40+
index: 'src/**',
41+
};
42+
```
2943

3044
:::info
3145
参考 [lib.bundle](/config/lib/bundle#set-entry) 进一步了解如何为 bundle 和 bundleless 项目设置入口。

0 commit comments

Comments
 (0)