Skip to content

Commit c5ee684

Browse files
authored
feat(rspeedy/core): support cli flag --environment (#462)
<!-- Thank you for submitting a pull request! We appreciate the time and effort you have invested in making these changes. Please ensure that you provide enough information to allow others to review your pull request. Upon submission, your pull request will be automatically assigned with reviewers. If you want to learn more about contributing to this project, please visit: https://github.com/lynx-family/lynx-stack/blob/main/CONTRIBUTING.md. --> ## Summary <!-- Can you explain the reasoning behind implementing this change? What problem or issue does this pull request resolve? --> <!-- It would be helpful if you could provide any relevant context, such as GitHub issues or related discussions. --> As one of several PRs addressing issue #304 Support cli option `--environment` to specify the name of environment to build. ## Checklist <!--- Check and mark with an "x" --> - [x] Tests updated (or not required). - [x] Documentation updated (or not required).
1 parent c151459 commit c5ee684

File tree

10 files changed

+110
-27
lines changed

10 files changed

+110
-27
lines changed

.changeset/ninety-zoos-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lynx-js/rspeedy": patch
3+
---
4+
5+
Support cli option `--environment` to specify the name of environment to build

packages/rspeedy/core/etc/rspeedy.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ export interface Config {
7171
export type ConsoleType = 'log' | 'warn' | 'error' | 'info' | 'debug' | 'profile' | 'profileEnd' | (string & Record<never, never>);
7272

7373
// @public
74-
export function createRspeedy({ cwd, rspeedyConfig, loadEnv }: CreateRspeedyOptions): Promise<RspeedyInstance>;
74+
export function createRspeedy({ cwd, rspeedyConfig, loadEnv, environment }: CreateRspeedyOptions): Promise<RspeedyInstance>;
7575

7676
// @public
7777
export interface CreateRspeedyOptions {
7878
cwd?: string;
79+
environment?: CreateRsbuildOptions['environment'];
7980
loadEnv?: CreateRsbuildOptions['loadEnv'];
8081
rspeedyConfig?: Config;
8182
}

packages/rspeedy/core/src/cli/build.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import { logger } from '@rsbuild/core'
66
import type { Command } from 'commander'
77

88
import type { CommonOptions } from './commands.js'
9+
import type { CreateRspeedyOptions } from '../create-rspeedy.js'
910
import { exit } from './exit.js'
1011
import { loadConfig } from '../config/loadConfig.js'
1112
import { createRspeedy } from '../create-rspeedy.js'
1213
import { isCI } from '../utils/is-ci.js'
1314

14-
export type BuildOptions = CommonOptions
15+
export type BuildOptions = CommonOptions & {
16+
environment?: string[] | undefined
17+
}
1518

1619
export async function build(
1720
this: Command,
@@ -28,13 +31,20 @@ export async function build(
2831
configPath: buildOptions.config,
2932
})
3033

31-
const rspeedy = await createRspeedy({
34+
const options: CreateRspeedyOptions = {
3235
cwd,
3336
rspeedyConfig,
34-
...(buildOptions.envMode
35-
? { loadEnv: { mode: buildOptions.envMode } }
36-
: {}),
37-
})
37+
}
38+
39+
if (buildOptions.envMode) {
40+
options.loadEnv = { mode: buildOptions.envMode }
41+
}
42+
43+
if (buildOptions.environment) {
44+
options.environment = buildOptions.environment
45+
}
46+
47+
const rspeedy = await createRspeedy(options)
3848

3949
await rspeedy.build()
4050
} catch (error) {

packages/rspeedy/core/src/cli/commands.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export function apply(program: Command): Command {
4646
const buildCommand = program.command('build')
4747
buildCommand
4848
.description('Build the project in production mode')
49+
.option(
50+
'--environment <name...>',
51+
'specify the name of environment to build',
52+
)
4953
.action(
5054
(buildOptions: BuildOptions) =>
5155
import('./build.js').then(({ build }) =>
@@ -59,6 +63,10 @@ export function apply(program: Command): Command {
5963
'Run the dev server and watch for source file changes while serving.',
6064
)
6165
.option('--base <base>', 'specify the base path of the server')
66+
.option(
67+
'--environment <name...>',
68+
'specify the name of environment to build',
69+
)
6270
.action(
6371
(devOptions: DevOptions) =>
6472
import('./dev.js').then(({ dev }) =>

packages/rspeedy/core/src/cli/dev.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import color from 'picocolors'
1111
import type { CommonOptions } from './commands.js'
1212
import { loadConfig, resolveConfigPath } from '../config/loadConfig.js'
1313
import { createRspeedy } from '../create-rspeedy.js'
14+
import type { CreateRspeedyOptions } from '../create-rspeedy.js'
1415
import { exit } from './exit.js'
1516

1617
export interface DevOptions extends CommonOptions {
1718
base?: string | undefined
19+
environment?: string[] | undefined
1820
}
1921

2022
export async function dev(
@@ -63,13 +65,20 @@ export async function dev(
6365
},
6466
)
6567

66-
const rspeedy = await createRspeedy({
68+
const options: CreateRspeedyOptions = {
6769
cwd,
6870
rspeedyConfig,
69-
...(devOptions.envMode
70-
? { loadEnv: { mode: devOptions.envMode } }
71-
: {}),
72-
})
71+
}
72+
73+
if (devOptions.envMode) {
74+
options.loadEnv = { mode: devOptions.envMode }
75+
}
76+
77+
if (devOptions.environment) {
78+
options.environment = devOptions.environment
79+
}
80+
81+
const rspeedy = await createRspeedy(options)
7382

7483
const server = await rspeedy.createDevServer()
7584

packages/rspeedy/core/src/create-rspeedy.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ export interface CreateRspeedyOptions {
4747
* @defaultValue true
4848
*/
4949
loadEnv?: CreateRsbuildOptions['loadEnv']
50+
/**
51+
* Only build specified environments.
52+
* For example, passing `['lynx']` will only build the `lynx` environment.
53+
* If not specified or passing an empty array, all environments will be built.
54+
* @see https://rsbuild.dev/guide/advanced/environments#build-specified-environment
55+
* @defaultValue []
56+
*/
57+
environment?: CreateRsbuildOptions['environment']
5058
}
5159
/**
5260
* The `createRspeedy` method can let you create a Rspeedy instance and you can customize the build or development process in Node.js Runtime.
@@ -68,7 +76,7 @@ export interface CreateRspeedyOptions {
6876
* @public
6977
*/
7078
export async function createRspeedy(
71-
{ cwd = process.cwd(), rspeedyConfig = {}, loadEnv = true }:
79+
{ cwd = process.cwd(), rspeedyConfig = {}, loadEnv = true, environment = [] }:
7280
CreateRspeedyOptions,
7381
): Promise<RspeedyInstance> {
7482
const config = applyDefaultRspeedyConfig(rspeedyConfig)
@@ -78,6 +86,7 @@ export async function createRspeedy(
7886
cwd,
7987
loadEnv,
8088
rsbuildConfig: toRsbuildConfig(config) as RsbuildConfig,
89+
environment,
8190
}),
8291
import('./plugins/index.js'),
8392
])

packages/rspeedy/core/test/cli/config.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,39 @@ describe('rspeedy config test', () => {
6969
)
7070
})
7171
})
72+
73+
describe('rspeedy environment test', () => {
74+
const fixturesRoot = join(
75+
dirname(fileURLToPath(import.meta.url)),
76+
'fixtures',
77+
)
78+
79+
test.each([
80+
{ name: 'web only', environment: ['web'], expected: ['web'] },
81+
{ name: 'lynx only', environment: ['lynx'], expected: ['lynx'] },
82+
{
83+
name: 'web + lynx',
84+
environment: ['web', 'lynx'],
85+
expected: ['web', 'lynx'],
86+
},
87+
{ name: 'empty array', environment: [], expected: ['web', 'lynx'] },
88+
])(
89+
'test environment combinations - $name',
90+
async ({ environment, expected }) => {
91+
const root = join(fixturesRoot, 'environment')
92+
const rsbuild = await createRspeedy({
93+
cwd: root,
94+
rspeedyConfig: {
95+
environments: {
96+
web: {},
97+
lynx: {},
98+
},
99+
},
100+
environment,
101+
})
102+
const configs = await rsbuild.initConfigs()
103+
expect(configs).toHaveLength(expected.length)
104+
expect(configs.map(c => c.name)).toEqual(expected)
105+
},
106+
)
107+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

website/docs/en/guide/cli.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ The `rspeedy dev` command is used to start a local dev server and compile the so
6464
Usage: rspeedy dev [options]
6565
6666
Options:
67-
-b --base <base> specify the base path of the server
68-
-c --config <config> specify the configuration file, can be a relative or absolute path
69-
--env-mode <mode> specify the env mode to load the .env.[mode] file
70-
-h, --help display help for command
67+
-b --base <base> specify the base path of the server
68+
-c --config <config> specify the configuration file, can be a relative or absolute path
69+
--env-mode <mode> specify the env mode to load the .env.[mode] file
70+
--environment <name...> specify the name of environment to build
71+
-h, --help display help for command
7172
```
7273

7374
The dev server will restart automatically when the content of the configuration file is modified.
@@ -82,9 +83,10 @@ The `rspeedy build` command will build the outputs for production in the `dist/`
8283
Usage: rspeedy build [options]
8384
8485
Options:
85-
-c --config <config> specify the configuration file, can be a relative or absolute path
86-
--env-mode <mode> specify the env mode to load the .env.[mode] file
87-
-h, --help display help for command
86+
-c --config <config> specify the configuration file, can be a relative or absolute path
87+
--env-mode <mode> specify the env mode to load the .env.[mode] file
88+
--environment <name...> specify the name of environment to build
89+
-h, --help display help for command
8890
```
8991

9092
## rspeedy preview

website/docs/zh/guide/cli.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ Commands:
6464
Usage: rspeedy dev [options]
6565
6666
Options:
67-
-b --base <base> specify the base path of the server
68-
-c --config <config> specify the configuration file, can be a relative or absolute path
69-
--env-mode <mode> specify the env mode to load the .env.[mode] file
70-
-h, --help display help for command
67+
-b --base <base> specify the base path of the server
68+
-c --config <config> specify the configuration file, can be a relative or absolute path
69+
--env-mode <mode> specify the env mode to load the .env.[mode] file
70+
--environment <name...> specify the name of environment to build
71+
-h, --help display help for command
7172
```
7273

7374
当配置文件内容发生修改时,开发服务器会自动重启。
@@ -82,9 +83,10 @@ Options:
8283
Usage: rspeedy build [options]
8384
8485
Options:
85-
-c --config <config> specify the configuration file, can be a relative or absolute path
86-
--env-mode <mode> specify the env mode to load the .env.[mode] file
87-
-h, --help display help for command
86+
-c --config <config> specify the configuration file, can be a relative or absolute path
87+
--env-mode <mode> specify the env mode to load the .env.[mode] file
88+
--environment <name...> specify the name of environment to build
89+
-h, --help display help for command
8890
```
8991

9092
## rspeedy preview

0 commit comments

Comments
 (0)