Skip to content

Commit

Permalink
feat(CLI): add --base flag (#4492)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Feb 6, 2025
1 parent 51d8562 commit d8bd19f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 4 deletions.
19 changes: 19 additions & 0 deletions e2e/cases/cli/base/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { execSync } from 'node:child_process';
import path from 'node:path';
import { globContentJSON, rspackOnlyTest } from '@e2e/helper';
import { expect } from '@playwright/test';

rspackOnlyTest('should run allow to specify base path', async () => {
execSync('npx rsbuild build --base /test', {
cwd: __dirname,
});

const outputs = await globContentJSON(path.join(__dirname, 'dist'));
const outputFiles = Object.keys(outputs);

expect(
outputFiles.find((item) =>
outputs[item].includes('/test/static/js/index.'),
),
).toBeTruthy();
});
1 change: 1 addition & 0 deletions e2e/cases/cli/base/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello!');
11 changes: 11 additions & 0 deletions e2e/cases/cli/base/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@rsbuild/config/tsconfig",
"compilerOptions": {
"jsx": "react-jsx",
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"]
}
2 changes: 2 additions & 0 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { RsbuildMode } from '../types';
import { init } from './init';

export type CommonOptions = {
base?: string;
root?: string;
mode?: RsbuildMode;
config?: string;
Expand Down Expand Up @@ -34,6 +35,7 @@ export type PreviewOptions = CommonOptions;

const applyCommonOptions = (cli: CAC) => {
cli
.option('--base <base>', 'specify the base path of the server')
.option(
'-c, --config <config>',
'specify the configuration file, can be a relative or absolute path',
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export async function init({
...config.source.define,
};

if (commonOpts.base) {
config.server ||= {};
config.server.base = commonOpts.base;
}

if (commonOpts.root) {
config.root = root;
}
Expand Down
5 changes: 3 additions & 2 deletions website/docs/en/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ Rsbuild CLI provides several common flags that can be used with all commands:

| Flag | Description |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--base <base>` | Specify the base path of the server, see [server.base](/config/server/base) |
| `-c, --config <config>` | Specify the configuration file, can be a relative or absolute path, see [Specify config file](/guide/basic/configure-rsbuild#specify-config-file) |
| `--config-loader <loader>` | Specify the config loader, can be `jiti` or `native`, see [Specify config loader](/guide/basic/configure-rsbuild#specify-config-loader) |
| `-r, --root <root>` | Specify the project root directory |
| `-m, --mode <mode>` | Specify the build mode (`development`, `production` or `none`), see [mode](/config/mode) |
| `--env-mode <mode>` | Specify the env mode to load the `.env.[mode]` file, see [Env mode](/guide/advanced/env-vars#env-mode) |
| `--env-dir <dir>` | Specify the directory to load `.env` files, see [Env directory](/guide/advanced/env-vars#env-directory) |
| `--environment <name>` | Specify the name of environment to build, see [Build specified environment](/guide/advanced/environments#build-specified-environment) |
| `-h, --help` | Display help for command |
| `-m, --mode <mode>` | Specify the build mode (`development`, `production` or `none`), see [mode](/config/mode) |
| `-r, --root <root>` | Specify the project root directory |

## rsbuild dev

Expand Down
5 changes: 3 additions & 2 deletions website/docs/zh/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ Rsbuild CLI 提供了一些公共选项,可以用于所有命令:

| 选项 | 描述 |
| -------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `--base <base>` | 指定服务端的基础路径,详见 [server.base](/config/server/base) |
| `-c, --config <config>` | 指定配置文件路径,可以为相对路径或绝对路径,详见 [指定配置文件](/guide/basic/configure-rsbuild#指定配置文件) |
| `--config-loader <loader>` | 指定配置文件加载方式,可以为 `jiti``native`,详见 [指定加载方式](/guide/basic/configure-rsbuild#指定加载方式) |
| `-r, --root <root>` | 指定项目根目录,可以是绝对路径或者相对于 cwd 的路径 |
| `-m, --mode <mode>` | 指定构建模式,可以是 `development``production``none`,详见 [mode](/config/mode) |
| `--env-mode <mode>` | 指定 env 模式来加载 `.env.[mode]` 文件,详见 [Env 模式](/guide/advanced/env-vars#env-模式) |
| `--env-dir <dir>` | 指定目录来加载 `.env` 文件,详见 [Env 目录](/guide/advanced/env-vars#env-目录) |
| `--environment <name>` | 指定需要构建的 environment 名称,详见 [构建指定环境](/guide/advanced/environments#构建指定环境) |
| `-h, --help` | 显示命令帮助 |
| `-m, --mode <mode>` | 指定构建模式,可以是 `development``production``none`,详见 [mode](/config/mode) |
| `-r, --root <root>` | 指定项目根目录,可以是绝对路径或者相对于 cwd 的路径 |

## rsbuild dev

Expand Down

1 comment on commit d8bd19f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
plugins ✅ success
rspress ✅ success
rslib ✅ success
examples ✅ success

Please sign in to comment.