Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-zoos-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/rspeedy": patch
---

Support cli option `--environment` to specify the name of environment to build
3 changes: 2 additions & 1 deletion packages/rspeedy/core/etc/rspeedy.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ export interface Config {
export type ConsoleType = 'log' | 'warn' | 'error' | 'info' | 'debug' | 'profile' | 'profileEnd' | (string & Record<never, never>);

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

// @public
export interface CreateRspeedyOptions {
cwd?: string;
environment?: CreateRsbuildOptions['environment'];
loadEnv?: CreateRsbuildOptions['loadEnv'];
rspeedyConfig?: Config;
}
Expand Down
22 changes: 16 additions & 6 deletions packages/rspeedy/core/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { logger } from '@rsbuild/core'
import type { Command } from 'commander'

import type { CommonOptions } from './commands.js'
import type { CreateRspeedyOptions } from '../create-rspeedy.js'
import { exit } from './exit.js'
import { loadConfig } from '../config/loadConfig.js'
import { createRspeedy } from '../create-rspeedy.js'
import { isCI } from '../utils/is-ci.js'

export type BuildOptions = CommonOptions
export type BuildOptions = CommonOptions & {
environment?: string[] | undefined
}

export async function build(
this: Command,
Expand All @@ -28,13 +31,20 @@ export async function build(
configPath: buildOptions.config,
})

const rspeedy = await createRspeedy({
const options: CreateRspeedyOptions = {
cwd,
rspeedyConfig,
...(buildOptions.envMode
? { loadEnv: { mode: buildOptions.envMode } }
: {}),
})
}

if (buildOptions.envMode) {
options.loadEnv = { mode: buildOptions.envMode }
}

if (buildOptions.environment) {
options.environment = buildOptions.environment
}

const rspeedy = await createRspeedy(options)

await rspeedy.build()
} catch (error) {
Expand Down
8 changes: 8 additions & 0 deletions packages/rspeedy/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function apply(program: Command): Command {
const buildCommand = program.command('build')
buildCommand
.description('Build the project in production mode')
.option(
'--environment <name...>',
'specify the name of environment to build',
)
.action(
(buildOptions: BuildOptions) =>
import('./build.js').then(({ build }) =>
Expand All @@ -59,6 +63,10 @@ export function apply(program: Command): Command {
'Run the dev server and watch for source file changes while serving.',
)
.option('--base <base>', 'specify the base path of the server')
.option(
'--environment <name...>',
'specify the name of environment to build',
)
.action(
(devOptions: DevOptions) =>
import('./dev.js').then(({ dev }) =>
Expand Down
19 changes: 14 additions & 5 deletions packages/rspeedy/core/src/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import color from 'picocolors'
import type { CommonOptions } from './commands.js'
import { loadConfig, resolveConfigPath } from '../config/loadConfig.js'
import { createRspeedy } from '../create-rspeedy.js'
import type { CreateRspeedyOptions } from '../create-rspeedy.js'
import { exit } from './exit.js'

export interface DevOptions extends CommonOptions {
base?: string | undefined
environment?: string[] | undefined
}

export async function dev(
Expand Down Expand Up @@ -63,13 +65,20 @@ export async function dev(
},
)

const rspeedy = await createRspeedy({
const options: CreateRspeedyOptions = {
cwd,
rspeedyConfig,
...(devOptions.envMode
? { loadEnv: { mode: devOptions.envMode } }
: {}),
})
}

if (devOptions.envMode) {
options.loadEnv = { mode: devOptions.envMode }
}

if (devOptions.environment) {
options.environment = devOptions.environment
}

const rspeedy = await createRspeedy(options)

const server = await rspeedy.createDevServer()

Expand Down
11 changes: 10 additions & 1 deletion packages/rspeedy/core/src/create-rspeedy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export interface CreateRspeedyOptions {
* @defaultValue true
*/
loadEnv?: CreateRsbuildOptions['loadEnv']
/**
* Only build specified environments.
* For example, passing `['lynx']` will only build the `lynx` environment.
* If not specified or passing an empty array, all environments will be built.
* @see https://rsbuild.dev/guide/advanced/environments#build-specified-environment
* @defaultValue []
*/
environment?: CreateRsbuildOptions['environment']
}
/**
* The `createRspeedy` method can let you create a Rspeedy instance and you can customize the build or development process in Node.js Runtime.
Expand All @@ -68,7 +76,7 @@ export interface CreateRspeedyOptions {
* @public
*/
export async function createRspeedy(
{ cwd = process.cwd(), rspeedyConfig = {}, loadEnv = true }:
{ cwd = process.cwd(), rspeedyConfig = {}, loadEnv = true, environment = [] }:
CreateRspeedyOptions,
): Promise<RspeedyInstance> {
const config = applyDefaultRspeedyConfig(rspeedyConfig)
Expand All @@ -78,6 +86,7 @@ export async function createRspeedy(
cwd,
loadEnv,
rsbuildConfig: toRsbuildConfig(config) as RsbuildConfig,
environment,
}),
import('./plugins/index.js'),
])
Expand Down
36 changes: 36 additions & 0 deletions packages/rspeedy/core/test/cli/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,39 @@ describe('rspeedy config test', () => {
)
})
})

describe('rspeedy environment test', () => {
const fixturesRoot = join(
dirname(fileURLToPath(import.meta.url)),
'fixtures',
)

test.each([
{ name: 'web only', environment: ['web'], expected: ['web'] },
{ name: 'lynx only', environment: ['lynx'], expected: ['lynx'] },
{
name: 'web + lynx',
environment: ['web', 'lynx'],
expected: ['web', 'lynx'],
},
{ name: 'empty array', environment: [], expected: ['web', 'lynx'] },
])(
'test environment combinations - $name',
async ({ environment, expected }) => {
const root = join(fixturesRoot, 'environment')
const rsbuild = await createRspeedy({
cwd: root,
rspeedyConfig: {
environments: {
web: {},
lynx: {},
},
},
environment,
})
const configs = await rsbuild.initConfigs()
expect(configs).toHaveLength(expected.length)
expect(configs.map(c => c.name)).toEqual(expected)
},
)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 9 additions & 7 deletions website/docs/en/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ The `rspeedy dev` command is used to start a local dev server and compile the so
Usage: rspeedy dev [options]

Options:
-b --base <base> specify the base path of the server
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
-h, --help display help for command
-b --base <base> specify the base path of the server
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
--environment <name...> specify the name of environment to build
-h, --help display help for command
```

The dev server will restart automatically when the content of the configuration file is modified.
Expand All @@ -82,9 +83,10 @@ The `rspeedy build` command will build the outputs for production in the `dist/`
Usage: rspeedy build [options]

Options:
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
-h, --help display help for command
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
--environment <name...> specify the name of environment to build
-h, --help display help for command
```

## rspeedy preview
Expand Down
16 changes: 9 additions & 7 deletions website/docs/zh/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ Commands:
Usage: rspeedy dev [options]

Options:
-b --base <base> specify the base path of the server
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
-h, --help display help for command
-b --base <base> specify the base path of the server
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
--environment <name...> specify the name of environment to build
-h, --help display help for command
```

当配置文件内容发生修改时,开发服务器会自动重启。
Expand All @@ -82,9 +83,10 @@ Options:
Usage: rspeedy build [options]

Options:
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
-h, --help display help for command
-c --config <config> specify the configuration file, can be a relative or absolute path
--env-mode <mode> specify the env mode to load the .env.[mode] file
--environment <name...> specify the name of environment to build
-h, --help display help for command
```

## rspeedy preview
Expand Down
Loading