From 0f1504dff451bd0f5c103611f43c6760b6b84479 Mon Sep 17 00:00:00 2001 From: 9aoy Date: Mon, 30 Dec 2024 17:26:39 +0800 Subject: [PATCH] fix: do not copy public dir for node target by default (#4281) --- e2e/cases/server/public-dir/publicDir.test.ts | 46 +++++++++++++++++++ packages/core/src/config.ts | 8 ++-- packages/core/src/plugins/server.ts | 8 +++- packages/core/src/types/config.ts | 2 +- website/docs/en/config/server/public-dir.mdx | 16 ++++++- website/docs/zh/config/server/public-dir.mdx | 16 ++++++- 6 files changed, 86 insertions(+), 10 deletions(-) diff --git a/e2e/cases/server/public-dir/publicDir.test.ts b/e2e/cases/server/public-dir/publicDir.test.ts index 759d66fd58..df5aeb9867 100644 --- a/e2e/cases/server/public-dir/publicDir.test.ts +++ b/e2e/cases/server/public-dir/publicDir.test.ts @@ -191,6 +191,14 @@ test('should copy publicDir to the environment distDir when multiple environment }, }, }, + node: { + output: { + target: 'node', + distPath: { + root: 'dist-build-node', + }, + }, + }, }, }, }); @@ -207,6 +215,44 @@ test('should copy publicDir to the environment distDir when multiple environment filename.includes('dist-build-web-2/test-temp-file.txt'), ), ).toBeTruthy(); + expect( + filenames.some((filename) => + filename.includes('dist-build-node/test-temp-file.txt'), + ), + ).toBeFalsy(); +}); + +test('should copy publicDir to the node distDir when copyOnBuild is specified as true', async () => { + await fse.outputFile(join(__dirname, 'public', 'test-temp-file.txt'), 'a'); + + const rsbuild = await build({ + cwd, + rsbuildConfig: { + server: { + publicDir: { + copyOnBuild: true, + }, + }, + environments: { + node: { + output: { + target: 'node', + distPath: { + root: 'dist-build-node-1', + }, + }, + }, + }, + }, + }); + const files = await rsbuild.unwrapOutputJSON(); + const filenames = Object.keys(files); + + expect( + filenames.some((filename) => + filename.includes('dist-build-node-1/test-temp-file.txt'), + ), + ).toBeTruthy(); }); test('should copy publicDir to root dist when environment dist path has a parent-child relationship', async () => { diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index fe33849230..195a8d9882 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -679,16 +679,18 @@ export function stringifyConfig(config: unknown, verbose?: boolean): string { return stringify(config, { verbose }); } +type NormalizePublicDirOptions = PublicDirOptions & + Required>; + export const normalizePublicDirs = ( publicDir?: PublicDir, -): Required[] => { +): NormalizePublicDirOptions[] => { if (publicDir === false) { return []; } - const defaultConfig: Required = { + const defaultConfig: NormalizePublicDirOptions = { name: 'public', - copyOnBuild: true, watch: false, }; diff --git a/packages/core/src/plugins/server.ts b/packages/core/src/plugins/server.ts index 38482f6a33..22118b821a 100644 --- a/packages/core/src/plugins/server.ts +++ b/packages/core/src/plugins/server.ts @@ -34,7 +34,7 @@ export const pluginServer = (): RsbuildPlugin => ({ for (const publicDir of publicDirs) { const { name, copyOnBuild } = publicDir; - if (!copyOnBuild || !name) { + if (copyOnBuild === false || !name) { continue; } @@ -47,7 +47,11 @@ export const pluginServer = (): RsbuildPlugin => ({ } const distPaths = dedupeNestedPaths( - Object.values(environments).map((e) => e.distPath), + Object.values(environments) + .filter( + (e) => copyOnBuild === true || e.config.output.target !== 'node', + ) + .map((e) => e.distPath), ); try { diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index f809326f0f..1f44d7cb5d 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -322,7 +322,7 @@ export type PublicDirOptions = { name?: string; /** * Whether to copy files from the publicDir to the distDir on production build - * @default true + * @default target !== 'node' */ copyOnBuild?: boolean; /** diff --git a/website/docs/en/config/server/public-dir.mdx b/website/docs/en/config/server/public-dir.mdx index d811dafce0..415121f361 100644 --- a/website/docs/en/config/server/public-dir.mdx +++ b/website/docs/en/config/server/public-dir.mdx @@ -16,7 +16,7 @@ type PublicDir = false | PublicDirOptions | PublicDirOptions[]; ```js const defaultValue = { name: 'public', - copyOnBuild: true, + copyOnBuild: target !== 'node', }; ``` @@ -62,7 +62,7 @@ export default { ### copyOnBuild - **Type:** `boolean` -- **Default:** `true` +- **Default:** `target !== 'node'` Whether to copy files from the publicDir to the distDir on production build. @@ -80,6 +80,18 @@ export default { Note that setting the value of `copyOnBuild` to false means that when you run `rsbuild preview` for a production preview, you will not be able to access the corresponding static resources. +By default, the public directory is not copied in the node target environment. If you need to copy files from the public directory to the distDir in the node target environment, you can manually enable `copyOnBuild`: + +```ts +export default { + server: { + publicDir: { + copyOnBuild: true, + }, + }, +}; +``` + :::tip During dev builds, if you need to copy some static assets to the output directory, you can use the [output.copy](/config/output/copy) option instead. ::: diff --git a/website/docs/zh/config/server/public-dir.mdx b/website/docs/zh/config/server/public-dir.mdx index 0feccd72bd..3115450b49 100644 --- a/website/docs/zh/config/server/public-dir.mdx +++ b/website/docs/zh/config/server/public-dir.mdx @@ -16,7 +16,7 @@ type PublicDir = false | PublicDirOptions | PublicDirOptions[]; ```js const defaultValue = { name: 'public', - copyOnBuild: true, + copyOnBuild: target !== 'node', watch: false, }; ``` @@ -63,7 +63,7 @@ export default { ### copyOnBuild - **类型:** `boolean` -- **默认值:** `true` +- **默认值:** `target !== 'node'` 在生产构建时,是否将文件从 public 目录复制到构建产物目录。 @@ -81,6 +81,18 @@ export default { 需要注意的是,将 `copyOnBuild` 的值为 false 后,如果执行 `rsbuild preview` 进行生产环境预览,将无法访问对应的静态资源文件。 +默认情况下,node 环境下不会进行复制。如果你需要在 node 环境下将文件从 public 目录复制到构建产物目录,可手动开启 `copyOnBuild`: + +```ts +export default { + server: { + publicDir: { + copyOnBuild: true, + }, + }, +}; +``` + :::tip 在 dev 构建时,如果你需要拷贝一些静态资源到构建产物目录,可以使用 [output.copy](/config/output/copy) 选项代替。 :::