diff --git a/website/docs/en/config/source/entry.mdx b/website/docs/en/config/source/entry.mdx index 3cb49a27a0..99817d61b5 100644 --- a/website/docs/en/config/source/entry.mdx +++ b/website/docs/en/config/source/entry.mdx @@ -99,7 +99,7 @@ When you build for multiple [environments](/config/environments), you can set di For example, set different entry for `web` and `node` environments: -```js +```ts title="rsbuild.config.ts" export default { environments: { web: { @@ -125,3 +125,30 @@ export default { }, }; ``` + +## Asynchronous setting + +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`: + +```ts title="rsbuild.config.ts" +import path from 'node:path'; +import { glob } from 'glob'; +import { defineConfig } from '@rsbuild/core'; + +export default defineConfig(async () => { + const entryFiles = await glob('./src/**/main.{ts,tsx,js,jsx}'); + + const entry = Object.fromEntries( + entryFiles.map((file) => { + const entryName = path.basename(path.dirname(file)); + return [entryName, `./${file}`]; + }), + ); + + return { + source: { + entry: entry, + }, + }; +}); +``` diff --git a/website/docs/zh/config/source/entry.mdx b/website/docs/zh/config/source/entry.mdx index 5df98f26cd..ed12aa7611 100644 --- a/website/docs/zh/config/source/entry.mdx +++ b/website/docs/zh/config/source/entry.mdx @@ -99,7 +99,7 @@ export default { 比如为 `web` 和 `node` 环境设置不同的 entry: -```js +```ts title="rsbuild.config.ts" export default { environments: { web: { @@ -125,3 +125,30 @@ export default { }, }; ``` + +## 异步设置 + +如果你需要异步设置 entry,例如使用 [glob](https://www.npmjs.com/package/glob) 来扫描目录,可以在 `rsbuild.config.ts` 中导出一个异步函数: + +```ts title="rsbuild.config.ts" +import path from 'node:path'; +import { glob } from 'glob'; +import { defineConfig } from '@rsbuild/core'; + +export default defineConfig(async () => { + const entryFiles = await glob('./src/**/main.{ts,tsx,js,jsx}'); + + const entry = Object.fromEntries( + entryFiles.map((file) => { + const entryName = path.basename(path.dirname(file)); + return [entryName, `./${file}`]; + }), + ); + + return { + source: { + entry: entry, + }, + }; +}); +```