From 6a1ca2a087e937ec3546c15af34aa7abd8778a3d Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 12 Aug 2024 16:12:18 +0800 Subject: [PATCH 1/4] feat!: output inspect config to .rsbuild dir --- e2e/cases/inspect-config/debug.test.ts | 4 ++-- e2e/cases/inspect-config/index.test.ts | 11 +++++---- packages/core/src/constants.ts | 1 + packages/core/src/plugins/cleanOutput.ts | 24 +++++++++++++++++-- packages/core/src/provider/inspectConfig.ts | 3 ++- .../docs/en/guide/advanced/environments.mdx | 8 +++---- website/docs/en/guide/basic/cli.mdx | 14 +++++------ .../docs/en/guide/basic/configure-rsbuild.mdx | 6 ++--- website/docs/en/guide/debug/debug-mode.mdx | 8 +++---- website/docs/en/guide/faq/features.mdx | 4 ++-- .../docs/zh/guide/advanced/environments.mdx | 8 +++---- website/docs/zh/guide/basic/cli.mdx | 14 +++++------ .../docs/zh/guide/basic/configure-rsbuild.mdx | 6 ++--- website/docs/zh/guide/debug/debug-mode.mdx | 8 +++---- website/docs/zh/guide/faq/features.mdx | 4 ++-- 15 files changed, 74 insertions(+), 49 deletions(-) diff --git a/e2e/cases/inspect-config/debug.test.ts b/e2e/cases/inspect-config/debug.test.ts index a86111a57a..cfac096a73 100644 --- a/e2e/cases/inspect-config/debug.test.ts +++ b/e2e/cases/inspect-config/debug.test.ts @@ -5,12 +5,12 @@ import { expect, test } from '@playwright/test'; import { logger } from '@rsbuild/core'; const getRsbuildConfig = (dist: string) => - path.resolve(__dirname, `./${dist}/rsbuild.config.mjs`); + path.resolve(__dirname, `./${dist}/.rsbuild/rsbuild.config.mjs`); const getBundlerConfig = (dist: string) => path.resolve( __dirname, - `./${dist}/${process.env.PROVIDE_TYPE || 'rspack'}.config.web.mjs`, + `./${dist}/.rsbuild/${process.env.PROVIDE_TYPE || 'rspack'}.config.web.mjs`, ); test('should generate config files when build (with DEBUG)', async () => { diff --git a/e2e/cases/inspect-config/index.test.ts b/e2e/cases/inspect-config/index.test.ts index c82aea2a83..dcd3280b41 100644 --- a/e2e/cases/inspect-config/index.test.ts +++ b/e2e/cases/inspect-config/index.test.ts @@ -3,19 +3,22 @@ import path from 'node:path'; import { createRsbuild } from '@e2e/helper'; import { expect, test } from '@playwright/test'; -const rsbuildConfig = path.resolve(__dirname, './dist/rsbuild.config.mjs'); +const rsbuildConfig = path.resolve( + __dirname, + './dist/.rsbuild/rsbuild.config.mjs', +); const rsbuildNodeConfig = path.resolve( __dirname, - './dist/rsbuild.config.node.mjs', + './dist/.rsbuild/rsbuild.config.node.mjs', ); const bundlerConfig = path.resolve( __dirname, - `./dist/${process.env.PROVIDE_TYPE || 'rspack'}.config.web.mjs`, + `./dist/.rsbuild/${process.env.PROVIDE_TYPE || 'rspack'}.config.web.mjs`, ); const bundlerNodeConfig = path.resolve( __dirname, - `./dist/${process.env.PROVIDE_TYPE || 'rspack'}.config.node.mjs`, + `./dist/.rsbuild/${process.env.PROVIDE_TYPE || 'rspack'}.config.node.mjs`, ); test('should generate config files when writeToDisk is true', async () => { diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 88d938886b..f059fa407a 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -16,6 +16,7 @@ export const STATIC_PATH: string = join(__dirname, '../static'); export const COMPILED_PATH: string = join(__dirname, '../compiled'); export const TS_CONFIG_FILE = 'tsconfig.json'; export const HMR_SOCKET_PATH = '/rsbuild-hmr'; +export const RSBUILD_OUTPUTS_PATH = '.rsbuild'; // Defaults export const DEFAULT_PORT = 3000; diff --git a/packages/core/src/plugins/cleanOutput.ts b/packages/core/src/plugins/cleanOutput.ts index 5589b87d7e..b56aadceb4 100644 --- a/packages/core/src/plugins/cleanOutput.ts +++ b/packages/core/src/plugins/cleanOutput.ts @@ -1,5 +1,6 @@ -import { sep } from 'node:path'; +import { join, sep } from 'node:path'; import color from 'picocolors'; +import { RSBUILD_OUTPUTS_PATH } from '../constants'; import { emptyDir } from '../helpers'; import { logger } from '../logger'; import type { EnvironmentContext, RsbuildPlugin } from '../types'; @@ -16,6 +17,23 @@ export const pluginCleanOutput = (): RsbuildPlugin => ({ name: 'rsbuild:clean-output', setup(api) { + // clean rsbuild outputs, such as inspect files + const cleanRsbuildOutputs = async () => { + const { rootPath, distPath } = api.context; + const config = api.getNormalizedConfig(); + const cleanPath = join(distPath, RSBUILD_OUTPUTS_PATH); + + let { cleanDistPath } = config.output; + + if (cleanDistPath === undefined) { + cleanDistPath = isStrictSubdir(rootPath, cleanPath); + } + + if (cleanDistPath) { + await emptyDir(cleanPath); + } + }; + const clean = async (environment: EnvironmentContext) => { const { rootPath } = api.context; const { config, distPath } = environment; @@ -55,7 +73,9 @@ export const pluginCleanOutput = (): RsbuildPlugin => ({ return total; }, []); - await Promise.all(environments.map((e) => clean(e))); + await Promise.all( + environments.map((e) => clean(e)).concat(cleanRsbuildOutputs()), + ); }; api.onBeforeBuild(async ({ isFirstCompile, environments }) => { diff --git a/packages/core/src/provider/inspectConfig.ts b/packages/core/src/provider/inspectConfig.ts index 3f0f98ebee..0d3c957d72 100644 --- a/packages/core/src/provider/inspectConfig.ts +++ b/packages/core/src/provider/inspectConfig.ts @@ -4,6 +4,7 @@ import { outputInspectConfigFiles, stringifyConfig, } from '../config'; +import { RSBUILD_OUTPUTS_PATH } from '../constants'; import { getNodeEnv, setNodeEnv } from '../helpers'; import type { InspectConfigOptions, @@ -25,7 +26,7 @@ const getInspectOutputPath = ( return join(context.distPath, inspectOptions.outputPath); } - return context.distPath; + return join(context.distPath, RSBUILD_OUTPUTS_PATH); }; export async function inspectConfig({ diff --git a/website/docs/en/guide/advanced/environments.mdx b/website/docs/en/guide/advanced/environments.mdx index f6f52b33fa..1b302aac82 100644 --- a/website/docs/en/guide/advanced/environments.mdx +++ b/website/docs/en/guide/advanced/environments.mdx @@ -74,10 +74,10 @@ When you execute the command `npx rsbuild inspect` in the project root directory Inspect config succeed, open following files to view the content: - - Rsbuild Config (web): /project/dist/rsbuild.config.web.mjs - - Rsbuild Config (node): /project/dist/rsbuild.config.node.mjs - - Rspack Config (web): /project/dist/rspack.config.web.mjs - - Rspack Config (node): /project/dist/rspack.config.node.mjs + - Rsbuild Config (web): /project/dist/.rsbuild/rsbuild.config.web.mjs + - Rsbuild Config (node): /project/dist/.rsbuild/rsbuild.config.node.mjs + - Rspack Config (web): /project/dist/.rsbuild/rspack.config.web.mjs + - Rspack Config (node): /project/dist/.rsbuild/rspack.config.node.mjs ``` ## Default environment diff --git a/website/docs/en/guide/basic/cli.mdx b/website/docs/en/guide/basic/cli.mdx index aa944c322d..4680791113 100644 --- a/website/docs/en/guide/basic/cli.mdx +++ b/website/docs/en/guide/basic/cli.mdx @@ -128,7 +128,7 @@ Options: -h, --help show command help ``` -When you run the command `npx rsbuild inspect` in the project root directory, the following files will be generated in the `dist` directory of the project: +When you run the command `npx rsbuild inspect` in the project root directory, the following files will be generated in the `dist/.rsbuild` directory of the project: - `rsbuild.config.mjs`: Represents the Rsbuild configuration used during the build. - `rspack.config.web.mjs`: Represents the Rspack configuration used during the build. @@ -138,8 +138,8 @@ When you run the command `npx rsbuild inspect` in the project root directory, th Inspect config succeed, open following files to view the content: - - Rsbuild Config: /project/dist/rsbuild.config.mjs - - Rspack Config (web): /project/dist/rspack.config.web.mjs + - Rsbuild Config: /project/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /project/dist/.rsbuild/rspack.config.web.mjs ``` ### Specifying Mode @@ -160,14 +160,14 @@ rsbuild inspect --verbose ### Multiple Targets -If the current project has multiple build targets, such as building browser artifact and Node.js artifact simultaneously, multiple Rspack configuration files will be generated in the `dist` directory. +If the current project has multiple build targets, such as building browser artifact and Node.js artifact simultaneously, multiple Rspack configuration files will be generated in the `dist/.rsbuild` directory. ```bash ➜ npx rsbuild inspect Inspect config succeed, open following files to view the content: - - Rsbuild Config: /project/dist/rsbuild.config.mjs - - Rspack Config (web): /project/dist/rspack.config.web.mjs - - Rspack Config (node): /project/dist/rspack.config.node.mjs + - Rsbuild Config: /project/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /project/dist/.rsbuild/rspack.config.web.mjs + - Rspack Config (node): /project/dist/.rsbuild/rspack.config.node.mjs ``` diff --git a/website/docs/en/guide/basic/configure-rsbuild.mdx b/website/docs/en/guide/basic/configure-rsbuild.mdx index a847c92d6b..6ec4592b6e 100644 --- a/website/docs/en/guide/basic/configure-rsbuild.mdx +++ b/website/docs/en/guide/basic/configure-rsbuild.mdx @@ -229,10 +229,10 @@ In debug mode, Rsbuild will write the Rsbuild config to the dist directory, whic ``` Inspect config succeed, open the following files to view the content: - - Rsbuild Config: /Project/demo/dist/rsbuild.config.mjs - - Rspack Config (web): /Project/demo/dist/rspack.config.web.mjs + - Rsbuild Config: /Project/demo/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /Project/demo/dist/.rsbuild/rspack.config.web.mjs ``` -Open the generated `/dist/rsbuild.config.mjs` file to see the complete content of the Rsbuild config. +Open the generated `/dist/.rsbuild/rsbuild.config.mjs` file to see the complete content of the Rsbuild config. For a complete introduction to debug mode, see the [Debug Mode](/guide/debug/debug-mode) chapter. diff --git a/website/docs/en/guide/debug/debug-mode.mdx b/website/docs/en/guide/debug/debug-mode.mdx index 36bcef2b84..99feec0f47 100644 --- a/website/docs/en/guide/debug/debug-mode.mdx +++ b/website/docs/en/guide/debug/debug-mode.mdx @@ -32,13 +32,13 @@ In addition, the following logs will be output in the terminal, indicating that ```bash Inspect config succeeds, open following files to view the content: - - Rsbuild Config: /Project/demo/dist/rsbuild.config.mjs - - Rspack Config (web): /Project/demo/dist/rspack.config.web.mjs + - Rsbuild Config: /Project/demo/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /Project/demo/dist/.rsbuild/rspack.config.web.mjs ``` ## Rsbuild Config File -In debug mode, Rsbuild will automatically generate `dist/rsbuild.config.mjs` file, which contains the final generated Rsbuild config. In this file, you can know the final result of the Rsbuild config you passed in after being processed by the framework and Rsbuild. +In debug mode, Rsbuild will automatically generate `dist/.rsbuild/rsbuild.config.mjs` file, which contains the final generated Rsbuild config. In this file, you can know the final result of the Rsbuild config you passed in after being processed by the framework and Rsbuild. The structure of the file is as follows: @@ -58,7 +58,7 @@ For a complete introduction to Rsbuild config, please see the [Configure Rsbuild ## Rspack Config File -Rsbuild will also automatically generate `dist/rspack.config.web.mjs` file, which contains the final generated Rspack config. In this file, you can see what is included in the config that Rsbuild finally passes to Rspack. +Rsbuild will also automatically generate `dist/.rsbuild/rspack.config.web.mjs` file, which contains the final generated Rspack config. In this file, you can see what is included in the config that Rsbuild finally passes to Rspack. The structure of the file is as follows: diff --git a/website/docs/en/guide/faq/features.mdx b/website/docs/en/guide/faq/features.mdx index 746d95a604..536920e2db 100644 --- a/website/docs/en/guide/faq/features.mdx +++ b/website/docs/en/guide/faq/features.mdx @@ -64,8 +64,8 @@ debug add default plugins done [1938.57 ms] Inspect config succeed, open following files to view the content: - - Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs - - Rspack Config (web): /root/my-project/dist/rspack.config.web.mjs + - Rsbuild Config: /root/my-project/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /root/my-project/dist/.rsbuild/rspack.config.web.mjs ``` --- diff --git a/website/docs/zh/guide/advanced/environments.mdx b/website/docs/zh/guide/advanced/environments.mdx index 32e0a5fe0f..cf2ef0859e 100644 --- a/website/docs/zh/guide/advanced/environments.mdx +++ b/website/docs/zh/guide/advanced/environments.mdx @@ -74,10 +74,10 @@ export default { Inspect config succeed, open following files to view the content: - - Rsbuild Config (web): /project/dist/rsbuild.config.web.mjs - - Rsbuild Config (node): /project/dist/rsbuild.config.node.mjs - - Rspack Config (web): /project/dist/rspack.config.web.mjs - - Rspack Config (node): /project/dist/rspack.config.node.mjs + - Rsbuild Config (web): /project/dist/.rsbuild/rsbuild.config.web.mjs + - Rsbuild Config (node): /project/dist/.rsbuild/rsbuild.config.node.mjs + - Rspack Config (web): /project/dist/.rsbuild/rspack.config.web.mjs + - Rspack Config (node): /project/dist/.rsbuild/rspack.config.node.mjs ``` ## 默认 environment diff --git a/website/docs/zh/guide/basic/cli.mdx b/website/docs/zh/guide/basic/cli.mdx index 8e904bcab6..1c6517fb40 100644 --- a/website/docs/zh/guide/basic/cli.mdx +++ b/website/docs/zh/guide/basic/cli.mdx @@ -128,7 +128,7 @@ Options: -h, --help 显示命令帮助 ``` -当你在项目根目录下执行命令 `npx rsbuild inspect` 后,会在项目的 `dist` 目录生成以下文件: +当你在项目根目录下执行命令 `npx rsbuild inspect` 后,会在项目的 `dist/.rsbuild` 目录生成以下文件: - `rsbuild.config.mjs`: 表示在构建时使用的 Rsbuild 配置。 - `rspack.config.web.mjs`: 表示在构建时使用的 Rspack 配置。 @@ -138,8 +138,8 @@ Options: Inspect config succeed, open following files to view the content: - - Rsbuild Config: /project/dist/rsbuild.config.mjs - - Rspack Config (web): /project/dist/rspack.config.web.mjs + - Rsbuild Config: /project/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /project/dist/.rsbuild/rspack.config.web.mjs ``` ### 指定模式 @@ -160,14 +160,14 @@ rsbuild inspect --verbose ### 多种产物类型 -如果当前项目有多种产物类型,比如同时构建了浏览器产物和 Node.js 产物,那么会在 `dist` 目录生成多份 Rspack 配置文件。 +如果当前项目有多种产物类型,比如同时构建了浏览器产物和 Node.js 产物,那么会在 `dist/.rsbuild` 目录生成多份 Rspack 配置文件。 ```bash ➜ npx rsbuild inspect Inspect config succeed, open following files to view the content: - - Rsbuild Config: /project/dist/rsbuild.config.mjs - - Rspack Config (web): /project/dist/rspack.config.web.mjs - - Rspack Config (node): /project/dist/rspack.config.node.mjs + - Rsbuild Config: /project/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /project/dist/.rsbuild/rspack.config.web.mjs + - Rspack Config (node): /project/dist/.rsbuild/rspack.config.node.mjs ``` diff --git a/website/docs/zh/guide/basic/configure-rsbuild.mdx b/website/docs/zh/guide/basic/configure-rsbuild.mdx index 7724ccbfb4..7c2ac77a62 100644 --- a/website/docs/zh/guide/basic/configure-rsbuild.mdx +++ b/website/docs/zh/guide/basic/configure-rsbuild.mdx @@ -229,10 +229,10 @@ DEBUG=rsbuild pnpm dev ``` Inspect config succeed, open following files to view the content: - - Rsbuild Config: /Project/demo/dist/rsbuild.config.mjs - - Rspack Config (web): /Project/demo/dist/rspack.config.web.mjs + - Rsbuild Config: /Project/demo/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /Project/demo/dist/.rsbuild/rspack.config.web.mjs ``` -打开生成的 `/dist/rsbuild.config.mjs` 文件,即可查看 Rsbuild 配置的完整内容。 +打开生成的 `/dist/.rsbuild/rsbuild.config.mjs` 文件,即可查看 Rsbuild 配置的完整内容。 关于调试模式的完整介绍,请查看 [开启调试模式](/guide/debug/debug-mode) 章节。 diff --git a/website/docs/zh/guide/debug/debug-mode.mdx b/website/docs/zh/guide/debug/debug-mode.mdx index a260e163d7..88e4a8636e 100644 --- a/website/docs/zh/guide/debug/debug-mode.mdx +++ b/website/docs/zh/guide/debug/debug-mode.mdx @@ -32,13 +32,13 @@ debug init plugins done [2389.48 ms] ```bash Inspect config succeed, open following files to view the content: - - Rsbuild Config: /Project/demo/dist/rsbuild.config.mjs - - Rspack Config (web): /Project/demo/dist/rspack.config.web.mjs + - Rsbuild Config: /Project/demo/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /Project/demo/dist/.rsbuild/rspack.config.web.mjs ``` ## Rsbuild 配置文件 -在调试模式下,Rsbuild 会自动生成 `dist/rsbuild.config.mjs` 文件,这里面包含了最终生成的 Rsbuild 配置。在这个文件里,你可以了解到你传入的 Rsbuild 配置在经过框架层和 Rsbuild 处理后的最终结果。 +在调试模式下,Rsbuild 会自动生成 `dist/.rsbuild/rsbuild.config.mjs` 文件,这里面包含了最终生成的 Rsbuild 配置。在这个文件里,你可以了解到你传入的 Rsbuild 配置在经过框架层和 Rsbuild 处理后的最终结果。 该文件的大致结构如下: @@ -58,7 +58,7 @@ export default { ## Rspack 配置文件 -在调试模式下,Rsbuild 还会自动生成 `dist/rspack.config.web.mjs` 文件,这里面包含了最终生成的 Rspack 配置。在这个文件里,你可以了解到 Rsbuild 最终传递给 Rspack 的配置里包含了哪些内容。 +在调试模式下,Rsbuild 还会自动生成 `dist/.rsbuild/rspack.config.web.mjs` 文件,这里面包含了最终生成的 Rspack 配置。在这个文件里,你可以了解到 Rsbuild 最终传递给 Rspack 的配置里包含了哪些内容。 该文件的大致结构如下: diff --git a/website/docs/zh/guide/faq/features.mdx b/website/docs/zh/guide/faq/features.mdx index 8ad995726c..8423e11960 100644 --- a/website/docs/zh/guide/faq/features.mdx +++ b/website/docs/zh/guide/faq/features.mdx @@ -64,8 +64,8 @@ debug add default plugins done [1938.57 ms] Inspect config succeed, open following files to view the content: - - Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs - - Rspack Config (web): /root/my-project/dist/rspack.config.web.mjs + - Rsbuild Config: /root/my-project/dist/.rsbuild/rsbuild.config.mjs + - Rspack Config (web): /root/my-project/dist/.rsbuild/rspack.config.web.mjs ``` --- From dd5b4c0eafab43d935516c43803b981ddcf92275 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 12 Aug 2024 16:23:17 +0800 Subject: [PATCH 2/4] fix: e2e --- e2e/cases/cli/inspect/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/cases/cli/inspect/index.test.ts b/e2e/cases/cli/inspect/index.test.ts index 7c2bab4c6a..4a638ca738 100644 --- a/e2e/cases/cli/inspect/index.test.ts +++ b/e2e/cases/cli/inspect/index.test.ts @@ -16,7 +16,7 @@ test('should run inspect command correctly', async () => { cwd: __dirname, }); - const files = await globContentJSON(path.join(__dirname, 'dist')); + const files = await globContentJSON(path.join(__dirname, 'dist/.rsbuild')); const fileNames = Object.keys(files); const rsbuildConfig = fileNames.find((item) => @@ -40,7 +40,7 @@ test('should run inspect command with mode option correctly', async () => { cwd: __dirname, }); - const files = await globContentJSON(path.join(__dirname, 'dist')); + const files = await globContentJSON(path.join(__dirname, 'dist/.rsbuild')); const fileNames = Object.keys(files); const rsbuildConfig = fileNames.find((item) => From 5781b375bba996318eb5cffe93288551a4ed5de8 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 12 Aug 2024 16:31:08 +0800 Subject: [PATCH 3/4] fix: webpack --- packages/compat/webpack/src/inspectConfig.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compat/webpack/src/inspectConfig.ts b/packages/compat/webpack/src/inspectConfig.ts index e4c90075f6..20f25f0cd9 100644 --- a/packages/compat/webpack/src/inspectConfig.ts +++ b/packages/compat/webpack/src/inspectConfig.ts @@ -21,7 +21,7 @@ const getInspectOutputPath = ( return join(context.distPath, inspectOptions.outputPath); } - return context.distPath; + return join(context.distPath, '.rsbuild'); }; export async function inspectConfig({ From 70dd49a93a8db2ed5989b31319b9315c37f8f119 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 12 Aug 2024 19:03:33 +0800 Subject: [PATCH 4/4] fix: dedupeCleanPaths --- packages/core/src/plugins/cleanOutput.ts | 42 ++++++++++++++++-------- packages/core/tests/cleanOutput.test.ts | 34 +++++++++++++++++++ 2 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 packages/core/tests/cleanOutput.test.ts diff --git a/packages/core/src/plugins/cleanOutput.ts b/packages/core/src/plugins/cleanOutput.ts index b56aadceb4..371e2f496a 100644 --- a/packages/core/src/plugins/cleanOutput.ts +++ b/packages/core/src/plugins/cleanOutput.ts @@ -13,28 +13,38 @@ const isStrictSubdir = (parent: string, child: string) => { return parentDir !== childDir && childDir.startsWith(parentDir); }; +export const dedupeCleanPaths = (paths: string[]): string[] => { + return paths + .sort((p1, p2) => (p2.length > p1.length ? -1 : 1)) + .reduce((prev, curr) => { + const isSub = prev.find((p) => curr.startsWith(p) || curr === p); + if (isSub) { + return prev; + } + + return prev.concat(curr); + }, []); +}; + export const pluginCleanOutput = (): RsbuildPlugin => ({ name: 'rsbuild:clean-output', setup(api) { - // clean rsbuild outputs, such as inspect files - const cleanRsbuildOutputs = async () => { + // should clean rsbuild outputs, such as inspect files + const getRsbuildCleanPath = () => { const { rootPath, distPath } = api.context; const config = api.getNormalizedConfig(); const cleanPath = join(distPath, RSBUILD_OUTPUTS_PATH); - let { cleanDistPath } = config.output; - - if (cleanDistPath === undefined) { - cleanDistPath = isStrictSubdir(rootPath, cleanPath); - } + const { cleanDistPath } = config.output; - if (cleanDistPath) { - await emptyDir(cleanPath); + if (cleanDistPath && isStrictSubdir(rootPath, cleanPath)) { + return cleanPath; } + return undefined; }; - const clean = async (environment: EnvironmentContext) => { + const getCleanPath = (environment: EnvironmentContext) => { const { rootPath } = api.context; const { config, distPath } = environment; @@ -57,8 +67,9 @@ export const pluginCleanOutput = (): RsbuildPlugin => ({ } if (cleanDistPath) { - await emptyDir(distPath); + return distPath; } + return undefined; }; const cleanAll = async (params: { @@ -73,9 +84,12 @@ export const pluginCleanOutput = (): RsbuildPlugin => ({ return total; }, []); - await Promise.all( - environments.map((e) => clean(e)).concat(cleanRsbuildOutputs()), - ); + const cleanPaths = environments + .map((e) => getCleanPath(e)) + .concat(getRsbuildCleanPath()) + .filter((p): p is string => !!p); + + await Promise.all(dedupeCleanPaths(cleanPaths).map((p) => emptyDir(p))); }; api.onBeforeBuild(async ({ isFirstCompile, environments }) => { diff --git a/packages/core/tests/cleanOutput.test.ts b/packages/core/tests/cleanOutput.test.ts new file mode 100644 index 0000000000..56c3be423c --- /dev/null +++ b/packages/core/tests/cleanOutput.test.ts @@ -0,0 +1,34 @@ +import { dedupeCleanPaths } from '../src/plugins/cleanOutput'; + +describe('cleanOutput', () => { + test('should dedupeCleanPaths correctly', async () => { + expect( + dedupeCleanPaths([ + 'package/to/root/dist/web1', + 'package/to/root/dist/web2', + 'package/to/root/dist', + ]), + ).toEqual(['package/to/root/dist']); + + expect( + dedupeCleanPaths([ + 'package/to/root', + 'package/to/root/dist/web2', + 'package/to/root/dist', + ]), + ).toEqual(['package/to/root']); + + expect( + dedupeCleanPaths([ + 'package/to/root/dist/web1', + 'package/to/root/dist/web2', + 'package/to/root/dist/web3', + 'package/to/root/dist/web3', + ]), + ).toEqual([ + 'package/to/root/dist/web1', + 'package/to/root/dist/web2', + 'package/to/root/dist/web3', + ]); + }); +});