Skip to content

Commit af89fa0

Browse files
authored
docs: add asynchronous entry configuration example (#4370)
1 parent a48cf00 commit af89fa0

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ When you build for multiple [environments](/config/environments), you can set di
9999

100100
For example, set different entry for `web` and `node` environments:
101101

102-
```js
102+
```ts title="rsbuild.config.ts"
103103
export default {
104104
environments: {
105105
web: {
@@ -125,3 +125,30 @@ export default {
125125
},
126126
};
127127
```
128+
129+
## Asynchronous setting
130+
131+
If you need to set entry asynchronously, for example, use [glob](https://www.npmjs.com/package/glob) to scan the directory, you can export an async function in `rsbuild.config.ts`:
132+
133+
```ts title="rsbuild.config.ts"
134+
import path from 'node:path';
135+
import { glob } from 'glob';
136+
import { defineConfig } from '@rsbuild/core';
137+
138+
export default defineConfig(async () => {
139+
const entryFiles = await glob('./src/**/main.{ts,tsx,js,jsx}');
140+
141+
const entry = Object.fromEntries(
142+
entryFiles.map((file) => {
143+
const entryName = path.basename(path.dirname(file));
144+
return [entryName, `./${file}`];
145+
}),
146+
);
147+
148+
return {
149+
source: {
150+
entry: entry,
151+
},
152+
};
153+
});
154+
```

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default {
9999

100100
比如为 `web``node` 环境设置不同的 entry:
101101

102-
```js
102+
```ts title="rsbuild.config.ts"
103103
export default {
104104
environments: {
105105
web: {
@@ -125,3 +125,30 @@ export default {
125125
},
126126
};
127127
```
128+
129+
## 异步设置
130+
131+
如果你需要异步设置 entry,例如使用 [glob](https://www.npmjs.com/package/glob) 来扫描目录,可以在 `rsbuild.config.ts` 中导出一个异步函数:
132+
133+
```ts title="rsbuild.config.ts"
134+
import path from 'node:path';
135+
import { glob } from 'glob';
136+
import { defineConfig } from '@rsbuild/core';
137+
138+
export default defineConfig(async () => {
139+
const entryFiles = await glob('./src/**/main.{ts,tsx,js,jsx}');
140+
141+
const entry = Object.fromEntries(
142+
entryFiles.map((file) => {
143+
const entryName = path.basename(path.dirname(file));
144+
return [entryName, `./${file}`];
145+
}),
146+
);
147+
148+
return {
149+
source: {
150+
entry: entry,
151+
},
152+
};
153+
});
154+
```

0 commit comments

Comments
 (0)