diff --git a/.changeset/ninety-zoos-dream.md b/.changeset/ninety-zoos-dream.md new file mode 100644 index 0000000000..b08e3b8e5b --- /dev/null +++ b/.changeset/ninety-zoos-dream.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/rspeedy": patch +--- + +Support cli option `--environment` to specify the name of environment to build diff --git a/packages/rspeedy/core/etc/rspeedy.api.md b/packages/rspeedy/core/etc/rspeedy.api.md index b40a69f2d9..87f30549e7 100644 --- a/packages/rspeedy/core/etc/rspeedy.api.md +++ b/packages/rspeedy/core/etc/rspeedy.api.md @@ -71,11 +71,12 @@ export interface Config { export type ConsoleType = 'log' | 'warn' | 'error' | 'info' | 'debug' | 'profile' | 'profileEnd' | (string & Record); // @public -export function createRspeedy({ cwd, rspeedyConfig, loadEnv }: CreateRspeedyOptions): Promise; +export function createRspeedy({ cwd, rspeedyConfig, loadEnv, environment }: CreateRspeedyOptions): Promise; // @public export interface CreateRspeedyOptions { cwd?: string; + environment?: CreateRsbuildOptions['environment']; loadEnv?: CreateRsbuildOptions['loadEnv']; rspeedyConfig?: Config; } diff --git a/packages/rspeedy/core/src/cli/build.ts b/packages/rspeedy/core/src/cli/build.ts index a1f3493a00..41e0b3b0a8 100644 --- a/packages/rspeedy/core/src/cli/build.ts +++ b/packages/rspeedy/core/src/cli/build.ts @@ -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, @@ -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) { diff --git a/packages/rspeedy/core/src/cli/commands.ts b/packages/rspeedy/core/src/cli/commands.ts index e808878e29..291989a173 100644 --- a/packages/rspeedy/core/src/cli/commands.ts +++ b/packages/rspeedy/core/src/cli/commands.ts @@ -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 ', + 'specify the name of environment to build', + ) .action( (buildOptions: BuildOptions) => import('./build.js').then(({ build }) => @@ -59,6 +63,10 @@ export function apply(program: Command): Command { 'Run the dev server and watch for source file changes while serving.', ) .option('--base ', 'specify the base path of the server') + .option( + '--environment ', + 'specify the name of environment to build', + ) .action( (devOptions: DevOptions) => import('./dev.js').then(({ dev }) => diff --git a/packages/rspeedy/core/src/cli/dev.ts b/packages/rspeedy/core/src/cli/dev.ts index 4c2a760608..a963459be4 100644 --- a/packages/rspeedy/core/src/cli/dev.ts +++ b/packages/rspeedy/core/src/cli/dev.ts @@ -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( @@ -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() diff --git a/packages/rspeedy/core/src/create-rspeedy.ts b/packages/rspeedy/core/src/create-rspeedy.ts index eda8f5c160..27a0395c53 100644 --- a/packages/rspeedy/core/src/create-rspeedy.ts +++ b/packages/rspeedy/core/src/create-rspeedy.ts @@ -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. @@ -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 { const config = applyDefaultRspeedyConfig(rspeedyConfig) @@ -78,6 +86,7 @@ export async function createRspeedy( cwd, loadEnv, rsbuildConfig: toRsbuildConfig(config) as RsbuildConfig, + environment, }), import('./plugins/index.js'), ]) diff --git a/packages/rspeedy/core/test/cli/config.test.ts b/packages/rspeedy/core/test/cli/config.test.ts index acd272147e..403c073429 100644 --- a/packages/rspeedy/core/test/cli/config.test.ts +++ b/packages/rspeedy/core/test/cli/config.test.ts @@ -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) + }, + ) +}) diff --git a/packages/rspeedy/core/test/cli/fixtures/environment/lynx.config.js b/packages/rspeedy/core/test/cli/fixtures/environment/lynx.config.js new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/packages/rspeedy/core/test/cli/fixtures/environment/lynx.config.js @@ -0,0 +1 @@ + diff --git a/website/docs/en/guide/cli.md b/website/docs/en/guide/cli.md index 84f2ebdc2e..76b19e4f63 100644 --- a/website/docs/en/guide/cli.md +++ b/website/docs/en/guide/cli.md @@ -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 specify the base path of the server - -c --config specify the configuration file, can be a relative or absolute path - --env-mode specify the env mode to load the .env.[mode] file - -h, --help display help for command + -b --base specify the base path of the server + -c --config specify the configuration file, can be a relative or absolute path + --env-mode specify the env mode to load the .env.[mode] file + --environment 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. @@ -82,9 +83,10 @@ The `rspeedy build` command will build the outputs for production in the `dist/` Usage: rspeedy build [options] Options: - -c --config specify the configuration file, can be a relative or absolute path - --env-mode specify the env mode to load the .env.[mode] file - -h, --help display help for command + -c --config specify the configuration file, can be a relative or absolute path + --env-mode specify the env mode to load the .env.[mode] file + --environment specify the name of environment to build + -h, --help display help for command ``` ## rspeedy preview diff --git a/website/docs/zh/guide/cli.md b/website/docs/zh/guide/cli.md index 1cdeb3989a..0acceb19dc 100644 --- a/website/docs/zh/guide/cli.md +++ b/website/docs/zh/guide/cli.md @@ -64,10 +64,11 @@ Commands: Usage: rspeedy dev [options] Options: - -b --base specify the base path of the server - -c --config specify the configuration file, can be a relative or absolute path - --env-mode specify the env mode to load the .env.[mode] file - -h, --help display help for command + -b --base specify the base path of the server + -c --config specify the configuration file, can be a relative or absolute path + --env-mode specify the env mode to load the .env.[mode] file + --environment specify the name of environment to build + -h, --help display help for command ``` 当配置文件内容发生修改时,开发服务器会自动重启。 @@ -82,9 +83,10 @@ Options: Usage: rspeedy build [options] Options: - -c --config specify the configuration file, can be a relative or absolute path - --env-mode specify the env mode to load the .env.[mode] file - -h, --help display help for command + -c --config specify the configuration file, can be a relative or absolute path + --env-mode specify the env mode to load the .env.[mode] file + --environment specify the name of environment to build + -h, --help display help for command ``` ## rspeedy preview