Skip to content

Commit 021bcdb

Browse files
authored
feat(cli): add dist-path CLI option (#8005)
1 parent b2c7966 commit 021bcdb

6 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { expect, expectFile, readDirContents, test } from '@e2e/helper';
4+
5+
test('should set output dist path from CLI', async ({ execCliSync }) => {
6+
execCliSync('build --dist-path dist-custom');
7+
8+
const distCustom = path.join(import.meta.dirname, 'dist-custom');
9+
await expectFile(path.join(distCustom, 'index.html'));
10+
11+
const outputs = await readDirContents(distCustom);
12+
const outputFiles = Object.keys(outputs);
13+
14+
expect(outputFiles.find((item) => item.includes('static/js/index.'))).toBeTruthy();
15+
expect(fs.existsSync(path.join(import.meta.dirname, 'dist'))).toBeFalsy();
16+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello!');

packages/core/src/cli/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { init } from './init';
99

1010
export type CommonOptions = {
1111
base?: string;
12+
distPath?: string;
1213
root?: string;
1314
mode?: RsbuildMode;
1415
config?: string;
@@ -44,6 +45,7 @@ const applyCommonOptions = (cli: CAC) => {
4445
.option('--config-loader <loader>', 'Set the config file loader (auto | jiti | native)', {
4546
default: 'auto',
4647
})
48+
.option('--dist-path <dir>', 'Set the root directory of output files')
4749
.option('--env-dir <dir>', 'Set the directory for loading `.env` files')
4850
.option('--env-mode <mode>', 'Set the env mode to load the `.env.[mode]` file')
4951
.option('--environment <name>', 'Set the environment name(s) to build', {

packages/core/src/cli/init.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ const loadConfig = async (root: string) => {
5757
config.server.port = commonOpts.port;
5858
}
5959

60+
if (commonOpts.distPath !== undefined) {
61+
config.output ||= {};
62+
63+
const { distPath } = config.output;
64+
config.output.distPath =
65+
distPath && typeof distPath === 'object'
66+
? { ...distPath, root: commonOpts.distPath }
67+
: { root: commonOpts.distPath };
68+
}
69+
6070
// enable CLI shortcuts by default when using Rsbuild CLI
6171
if (config.dev.cliShortcuts === undefined) {
6272
config.dev.cliShortcuts = true;

website/docs/en/guide/basic/cli.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The Rsbuild CLI includes several common flags that work with all commands:
3636
| `--base <base>` | Set the base path of the server, see [server.base](/config/server/base) |
3737
| `-c, --config <config>` | Set the configuration file (relative or absolute path), see [Specify config file](/guide/configuration/rsbuild#specify-config-file) |
3838
| `--config-loader <loader>` | Set the config file loader (`auto` \| `jiti` \| `native`), see [Specify config loader](/guide/configuration/rsbuild#specify-config-loader) |
39+
| `--dist-path <dir>` | Set the root directory of output files, see [output.distPath](/config/output/dist-path) |
3940
| `--env-mode <mode>` | Set the env mode to load the `.env.[mode]` file, see [Env mode](/guide/advanced/env-vars#env-mode) |
4041
| `--env-dir <dir>` | Set the directory for loading `.env` files, see [Env directory](/guide/advanced/env-vars#env-directory) |
4142
| `--environment <name>` | Set the environment name(s) to build, see [Build specified environment](/guide/advanced/environments#build-a-specific-environment) |

website/docs/zh/guide/basic/cli.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Rsbuild CLI 提供了一些公共选项,可以用于所有命令:
3636
| `--base <base>` | 指定服务端的基础路径,详见 [server.base](/config/server/base) |
3737
| `-c, --config <config>` | 指定配置文件路径(相对路径或绝对路径),详见 [指定配置文件](/guide/configuration/rsbuild#specify-config-file) |
3838
| `--config-loader <loader>` | 指定配置文件加载方式(`auto` \| `jiti` \| `native`),详见 [指定加载方式](/guide/configuration/rsbuild#specify-config-loader) |
39+
| `--dist-path <dir>` | 指定构建产物的根目录,详见 [output.distPath](/config/output/dist-path) |
3940
| `--env-mode <mode>` | 指定 env 模式来加载 `.env.[mode]` 文件,详见 [Env 模式](/guide/advanced/env-vars#env-mode) |
4041
| `--env-dir <dir>` | 指定目录来加载 `.env` 文件,详见 [Env 目录](/guide/advanced/env-vars#env-directory) |
4142
| `--environment <name>` | 指定需要构建的 environment 名称,详见 [构建指定环境](/guide/advanced/environments#build-a-specific-environment) |

0 commit comments

Comments
 (0)