From e558edc15bd0eb36531eddbddc42cc5b62663a2e Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 29 Jul 2024 18:52:11 +0800 Subject: [PATCH 01/16] feat: support only build specified environment --- .../cli/specified-environment/index.test.ts | 20 +++++ .../specified-environment/rsbuild.config.mjs | 20 +++++ .../cli/specified-environment/src/index.js | 1 + packages/core/src/cli/commands.ts | 6 ++ packages/core/src/cli/init.ts | 1 + packages/core/src/createContext.ts | 7 +- packages/core/src/createRsbuild.ts | 8 +- packages/core/src/provider/initConfigs.ts | 78 ++++++++++++------- packages/core/src/types/context.ts | 2 + packages/core/src/types/rsbuild.ts | 13 +++- packages/core/tests/environments.test.ts | 36 +++++++++ .../docs/en/guide/advanced/environments.mdx | 15 ++++ website/docs/en/guide/basic/cli.mdx | 6 +- .../docs/zh/guide/advanced/environments.mdx | 15 ++++ website/docs/zh/guide/basic/cli.mdx | 6 +- 15 files changed, 199 insertions(+), 35 deletions(-) create mode 100644 e2e/cases/cli/specified-environment/index.test.ts create mode 100644 e2e/cases/cli/specified-environment/rsbuild.config.mjs create mode 100644 e2e/cases/cli/specified-environment/src/index.js diff --git a/e2e/cases/cli/specified-environment/index.test.ts b/e2e/cases/cli/specified-environment/index.test.ts new file mode 100644 index 0000000000..7a302ea46f --- /dev/null +++ b/e2e/cases/cli/specified-environment/index.test.ts @@ -0,0 +1,20 @@ +import { execSync } from 'node:child_process'; +import path from 'node:path'; +import { globContentJSON } from '@e2e/helper'; +import { expect, test } from '@playwright/test'; + +test('should only build specified environment when using --environment option', async () => { + execSync('npx rsbuild build --environment web2', { + cwd: __dirname, + }); + + const files = await globContentJSON(path.join(__dirname, 'dist')); + const outputFiles = Object.keys(files); + + expect( + outputFiles.find((item) => item.includes('web1/index.html')), + ).toBeFalsy(); + expect( + outputFiles.find((item) => item.includes('web2/index.html')), + ).toBeTruthy(); +}); diff --git a/e2e/cases/cli/specified-environment/rsbuild.config.mjs b/e2e/cases/cli/specified-environment/rsbuild.config.mjs new file mode 100644 index 0000000000..8cb3a3eda0 --- /dev/null +++ b/e2e/cases/cli/specified-environment/rsbuild.config.mjs @@ -0,0 +1,20 @@ +import { defineConfig } from '@rsbuild/core'; + +export default defineConfig({ + environments: { + web1: { + output: { + distPath: { + root: 'dist/web1', + }, + }, + }, + web2: { + output: { + distPath: { + root: 'dist/web2', + }, + }, + }, + }, +}); diff --git a/e2e/cases/cli/specified-environment/src/index.js b/e2e/cases/cli/specified-environment/src/index.js new file mode 100644 index 0000000000..ddc67c9b01 --- /dev/null +++ b/e2e/cases/cli/specified-environment/src/index.js @@ -0,0 +1 @@ +console.log('hello!'); diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index b4222aea2e..edeb153e7c 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -13,6 +13,7 @@ export type CommonOptions = { open?: boolean | string; host?: string; port?: number; + environment?: string[]; }; export type BuildOptions = CommonOptions & { @@ -39,6 +40,11 @@ const applyCommonOptions = (command: Command) => { '--env-mode ', 'specify the env mode to load the `.env.[mode]` file', ) + .option( + '--environment ', + 'specify the environment during multi-environment', + (str, prev) => (prev ? prev.concat(str.split(',')) : str.split(',')), + ) .option('--env-dir ', 'specify the directory to load `.env` files'); }; diff --git a/packages/core/src/cli/init.ts b/packages/core/src/cli/init.ts index 7ea975e33b..a4551b6589 100644 --- a/packages/core/src/cli/init.ts +++ b/packages/core/src/cli/init.ts @@ -80,6 +80,7 @@ export async function init({ return createRsbuild({ cwd: root, rsbuildConfig: config, + environment: commonOpts.environment, }); } catch (err) { if (isRestart) { diff --git a/packages/core/src/createContext.ts b/packages/core/src/createContext.ts index a99c406ed1..0d9b579614 100644 --- a/packages/core/src/createContext.ts +++ b/packages/core/src/createContext.ts @@ -8,11 +8,11 @@ import { getHTMLPathByEntry } from './initPlugins'; import { logger } from './logger'; import type { BundlerType, - CreateRsbuildOptions, EnvironmentContext, InternalContext, NormalizedConfig, NormalizedEnvironmentConfig, + ResolvedCreateRsbuildOptions, RsbuildConfig, RsbuildContext, RsbuildEntry, @@ -35,7 +35,7 @@ function getAbsoluteDistPath( * Create context by config. */ async function createContextByConfig( - options: Required, + options: ResolvedCreateRsbuildOptions, bundlerType: BundlerType, ): Promise { const { cwd } = options; @@ -210,7 +210,7 @@ export function createPublicContext( * which can have a lot of overhead and take some side effects. */ export async function createContext( - options: Required, + options: ResolvedCreateRsbuildOptions, userRsbuildConfig: RsbuildConfig, bundlerType: BundlerType, ): Promise { @@ -223,5 +223,6 @@ export async function createContext( hooks: initHooks(), config: { ...rsbuildConfig }, originalConfig: userRsbuildConfig, + specifiedEnvironments: options.environment, }; } diff --git a/packages/core/src/createRsbuild.ts b/packages/core/src/createRsbuild.ts index dd948f2e3c..fee5e5541d 100644 --- a/packages/core/src/createRsbuild.ts +++ b/packages/core/src/createRsbuild.ts @@ -10,6 +10,7 @@ import type { InternalContext, PluginManager, PreviewServerOptions, + ResolvedCreateRsbuildOptions, RsbuildInstance, RsbuildProvider, } from './types'; @@ -99,7 +100,7 @@ export async function createRsbuild( ): Promise { const { rsbuildConfig = {} } = options; - const rsbuildOptions: Required = { + const rsbuildOptions: ResolvedCreateRsbuildOptions = { cwd: process.cwd(), rsbuildConfig, ...options, @@ -180,7 +181,10 @@ export async function createRsbuild( if (rsbuildConfig.environments) { await Promise.all( Object.entries(rsbuildConfig.environments).map(async ([name, config]) => { - if (config.plugins) { + const isEnvironmentEnabled = + !rsbuildOptions.environment || + rsbuildOptions.environment.includes(name); + if (config.plugins && isEnvironmentEnabled) { const plugins = await Promise.all(config.plugins); rsbuild.addPlugins(plugins, { environment: name, diff --git a/packages/core/src/provider/initConfigs.ts b/packages/core/src/provider/initConfigs.ts index 617f9d16f4..9d5a2a62f5 100644 --- a/packages/core/src/provider/initConfigs.ts +++ b/packages/core/src/provider/initConfigs.ts @@ -1,3 +1,4 @@ +import color from 'picocolors'; import { getDefaultEntry, normalizeConfig } from '../config'; import { JS_DIST_DIR } from '../constants'; import { @@ -9,7 +10,6 @@ import { isDebug, logger } from '../logger'; import { mergeRsbuildConfig } from '../mergeConfig'; import { initPlugins } from '../pluginManager'; import type { - CreateRsbuildOptions, InspectConfigOptions, InternalContext, MergedEnvironmentConfig, @@ -17,6 +17,7 @@ import type { NormalizedConfig, NormalizedEnvironmentConfig, PluginManager, + ResolvedCreateRsbuildOptions, RsbuildEntry, RspackConfig, } from '../types'; @@ -62,12 +63,13 @@ async function modifyEnvironmentConfig( export type InitConfigsOptions = { context: InternalContext; pluginManager: PluginManager; - rsbuildOptions: Required; + rsbuildOptions: ResolvedCreateRsbuildOptions; }; const initEnvironmentConfigs = ( normalizedConfig: NormalizedConfig, rootPath: string, + specifiedEnvironments?: string[], ): Record => { let defaultEntry: RsbuildEntry; const getDefaultEntryWithMemo = () => { @@ -80,6 +82,9 @@ const initEnvironmentConfigs = ( normalizedConfig; const { assetPrefix, lazyCompilation } = dev; + const isEnvironmentEnabled = (name: string) => + !specifiedEnvironments || specifiedEnvironments.includes(name); + const applyEnvironmentDefaultConfig = (config: MergedEnvironmentConfig) => { if (!config.source.entry) { config.source.entry = getDefaultEntryWithMemo(); @@ -94,35 +99,53 @@ const initEnvironmentConfigs = ( }; if (environments && Object.keys(environments).length) { - return Object.fromEntries( - Object.entries(environments).map(([name, config]) => { - const environmentConfig: MergedEnvironmentConfig = { - ...(mergeRsbuildConfig( - { - ...rsbuildSharedConfig, - dev: { - assetPrefix, - lazyCompilation, - }, - } as unknown as MergedEnvironmentConfig, - config as unknown as MergedEnvironmentConfig, - ) as unknown as MergedEnvironmentConfig), - }; - - return [name, applyEnvironmentDefaultConfig(environmentConfig)]; - }), + const resolvedEnvironments = Object.fromEntries( + Object.entries(environments) + .filter(([name]) => isEnvironmentEnabled(name)) + .map(([name, config]) => { + const environmentConfig: MergedEnvironmentConfig = { + ...(mergeRsbuildConfig( + { + ...rsbuildSharedConfig, + dev: { + assetPrefix, + lazyCompilation, + }, + } as unknown as MergedEnvironmentConfig, + config as unknown as MergedEnvironmentConfig, + ) as unknown as MergedEnvironmentConfig), + }; + + return [name, applyEnvironmentDefaultConfig(environmentConfig)]; + }), + ); + + if (!Object.keys(resolvedEnvironments).length) { + logger.error( + `The current project is specified to run only in the ${color.yellow(specifiedEnvironments?.join(','))} environment, but the configuration of the corresponding environment was not found.`, + ); + process.exit(1); + } + return resolvedEnvironments; + } + + const defaultEnvironmentName = camelCase(rsbuildSharedConfig.output.target); + + if (!isEnvironmentEnabled(defaultEnvironmentName)) { + logger.error( + `The current project is specified to run only in the ${color.yellow(specifiedEnvironments?.join(','))} environment, but the configuration of the corresponding environment was not found.`, ); + process.exit(1); } return { - [camelCase(rsbuildSharedConfig.output.target)]: - applyEnvironmentDefaultConfig({ - ...rsbuildSharedConfig, - dev: { - assetPrefix, - lazyCompilation, - }, - } as MergedEnvironmentConfig), + [defaultEnvironmentName]: applyEnvironmentDefaultConfig({ + ...rsbuildSharedConfig, + dev: { + assetPrefix, + lazyCompilation, + }, + } as MergedEnvironmentConfig), }; }; @@ -151,6 +174,7 @@ export async function initRsbuildConfig({ const mergedEnvironments = initEnvironmentConfigs( normalizeBaseConfig, context.rootPath, + context.specifiedEnvironments, ); const { diff --git a/packages/core/src/types/context.ts b/packages/core/src/types/context.ts index b2da229d07..05752ad3df 100644 --- a/packages/core/src/types/context.ts +++ b/packages/core/src/types/context.ts @@ -42,4 +42,6 @@ export type InternalContext = RsbuildContext & { getPluginAPI?: (environment?: string) => RsbuildPluginAPI; /** The environment context. */ environments: Record; + /** Only build specified environment. */ + specifiedEnvironments?: string[]; }; diff --git a/packages/core/src/types/rsbuild.ts b/packages/core/src/types/rsbuild.ts index a97f68a669..1fa4d22c29 100644 --- a/packages/core/src/types/rsbuild.ts +++ b/packages/core/src/types/rsbuild.ts @@ -71,6 +71,17 @@ export type CreateRsbuildOptions = { cwd?: string; /** Configurations of Rsbuild. */ rsbuildConfig?: RsbuildConfig; + /** Only build specified environment. */ + environment?: string[]; +}; + +export type ResolvedCreateRsbuildOptions = { + /** The root path of current project. */ + cwd: string; + /** Configurations of Rsbuild. */ + rsbuildConfig: RsbuildConfig; + /** Only build specified environment. */ + environment?: string[]; }; export type ProviderInstance = { @@ -104,7 +115,7 @@ export type RsbuildProvider = (options: { context: InternalContext; pluginManager: PluginManager; - rsbuildOptions: Required; + rsbuildOptions: ResolvedCreateRsbuildOptions; setCssExtractPlugin: (plugin: unknown) => void; }) => Promise>; diff --git a/packages/core/tests/environments.test.ts b/packages/core/tests/environments.test.ts index cbbb407f87..7107bafc32 100644 --- a/packages/core/tests/environments.test.ts +++ b/packages/core/tests/environments.test.ts @@ -201,6 +201,42 @@ describe('environment config', () => { ).toMatchSnapshot(); }); + it('should support run specified environment', async () => { + process.env.NODE_ENV = 'development'; + + const pluginLogs: string[] = []; + + const plugin: (pluginId: string) => RsbuildPlugin = (pluginId) => ({ + name: 'test-environment', + setup: () => { + pluginLogs.push(`run plugin in ${pluginId}`); + }, + }); + + const rsbuild = await createRsbuild({ + rsbuildConfig: { + environments: { + web: { + plugins: [plugin('web')], + }, + ssr: { + plugins: [plugin('ssr')], + }, + }, + }, + environment: ['ssr'], + }); + + rsbuild.addPlugins([plugin('global')]); + + const { + origin: { environmentConfigs }, + } = await rsbuild.inspectConfig(); + + expect(Object.keys(environmentConfigs)).toEqual(['ssr']); + expect(pluginLogs).toEqual(['run plugin in ssr', 'run plugin in global']); + }); + it('should normalize environment config correctly', async () => { process.env.NODE_ENV = 'development'; const rsbuild = await createRsbuild({ diff --git a/website/docs/en/guide/advanced/environments.mdx b/website/docs/en/guide/advanced/environments.mdx index 242abbc21b..8f71178a12 100644 --- a/website/docs/en/guide/advanced/environments.mdx +++ b/website/docs/en/guide/advanced/environments.mdx @@ -100,6 +100,21 @@ export default { }; ``` +## Specify environment build + +By default, Rsbuild will build all environments in the Rsbuild configuration when you execute `rsbuild dev` or `rsbuild build`. You can build only the specified environment via `--environment `. + +```bash +# Enable build for all environments by default +rsbuild dev + +# Only enable web environment build +rsbuild dev --environment web + +# Enable web and ssr environment build +rsbuild dev --environment web --environment ssr +``` + ## Add plugins for specified environment Plugins configured through the [plugins](/config/plugins) field support running in all environments. If you want a plugin to run only in a specified environment, you can configure the plugin in the specified `environment`. diff --git a/website/docs/en/guide/basic/cli.mdx b/website/docs/en/guide/basic/cli.mdx index 40c4896b5e..f0ee4377c7 100644 --- a/website/docs/en/guide/basic/cli.mdx +++ b/website/docs/en/guide/basic/cli.mdx @@ -39,6 +39,7 @@ Options: --port specify a port number for Rsbuild Server to listen --host specify the host that the Rsbuild Server listens to -c --config specify the configuration file, can be a relative or absolute path + --environment specify the environment during multi-environment --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help display help for command @@ -74,6 +75,7 @@ Usage: rsbuild build [options] Options: -w --watch turn on watch mode, watch for changes and rebuild -c --config specify the configuration file, can be a relative or absolute path + --environment specify the environment during multi-environment --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help display help for command @@ -91,6 +93,7 @@ Options: --port specify a port number for Rsbuild Server to listen --host specify the host that the Rsbuild Server listens to -c --config specify the configuration file, can be a relative or absolute path + --environment specify the environment during multi-environment --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help display help for command @@ -112,6 +115,7 @@ Options: --output Specify the path to output in the dist (default: "/") --verbose Show the full function in the result -c --config specify the configuration file, can be a relative or absolute path + --environment specify the environment during multi-environment --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help show command help @@ -131,7 +135,7 @@ Inspect config succeed, open following files to view the content: - Rspack Config (web): /project/dist/rspack.config.web.mjs ``` -### Specifying Environment +### Specifying Environment Mode By default, the inspect command outputs the configuration for the development environment. You can add the `--env production` option to output the configuration for the production build: diff --git a/website/docs/zh/guide/advanced/environments.mdx b/website/docs/zh/guide/advanced/environments.mdx index f77eec3e3f..a05eb137c4 100644 --- a/website/docs/zh/guide/advanced/environments.mdx +++ b/website/docs/zh/guide/advanced/environments.mdx @@ -100,6 +100,21 @@ export default { }; ``` +## 仅构建指定环境 + +默认情况下,当你执行 `rsbuild dev` 或 `rsbuild build` 时,Rsbuild 会构建所有 Rsbuild 配置中的环境。你可以通过 `--environment ` 仅构建指定环境。 + +```bash +# 默认为所有环境开启构建 +rsbuild dev + +# 仅开启 web 环境构建 +rsbuild dev --environment web + +# 开启 web 和 ssr 环境构建 +rsbuild dev --environment web --environment ssr +``` + ## 为指定环境添加插件 通过 [plugins](/config/plugins) 字段配置的插件支持在所有环境下运行,如果你希望某个插件仅在指定环境下运行时,将该插件配置在特定 `environment` 下即可。 diff --git a/website/docs/zh/guide/basic/cli.mdx b/website/docs/zh/guide/basic/cli.mdx index aef61ee341..d95e5da656 100644 --- a/website/docs/zh/guide/basic/cli.mdx +++ b/website/docs/zh/guide/basic/cli.mdx @@ -39,6 +39,7 @@ Options: --port 设置 Rsbuild Server 监听的端口号 --host 指定 Rsbuild Server 启动时监听的 host -c --config 指定配置文件路径,可以为相对路径或绝对路径 + --environment 当多环境构建时仅构建指定环境 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 @@ -74,6 +75,7 @@ Usage: rsbuild build [options] Options: -w --watch 开启 watch 模式, 监听文件变更并重新构建 -c --config 指定配置文件路径,可以为相对路径或绝对路径 + --environment 当多环境构建时仅构建指定环境 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 @@ -91,6 +93,7 @@ Options: --port 设置 Rsbuild Server 监听的端口号 --host 指定 Rsbuild Server 启动时监听的 host -c --config 指定配置文件路径,可以为相对路径或绝对路径 + --environment 当多环境时仅预览指定环境 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 @@ -112,6 +115,7 @@ Options: --output 指定在 dist 目录下输出的路径 (default: "/") --verbose 在结果中展示函数的完整内容 -c --config 指定配置文件路径,可以为相对路径或绝对路径 + --environment 当多环境构建时仅输出指定环境的配置 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 @@ -131,7 +135,7 @@ Inspect config succeed, open following files to view the content: - Rspack Config (web): /project/dist/rspack.config.web.mjs ``` -### 指定环境 +### 指定环境模式 默认情况下,inspect 命令会输出开发环境的配置,你可以添加 `--env production` 选项来输出生产环境的配置: From 058c3d2a9a073dac3528126abc14f31a319a879b Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 29 Jul 2024 19:09:32 +0800 Subject: [PATCH 02/16] fix: type --- packages/compat/webpack/src/initConfigs.ts | 4 ++-- packages/core/src/index.ts | 1 + scripts/test-helper/src/rsbuild.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/compat/webpack/src/initConfigs.ts b/packages/compat/webpack/src/initConfigs.ts index 2cfaaef2d7..b4a32c4062 100644 --- a/packages/compat/webpack/src/initConfigs.ts +++ b/packages/compat/webpack/src/initConfigs.ts @@ -1,7 +1,7 @@ import { - type CreateRsbuildOptions, type InspectConfigOptions, type PluginManager, + type ResolvedCreateRsbuildOptions, logger, } from '@rsbuild/core'; import { inspectConfig } from './inspectConfig'; @@ -12,7 +12,7 @@ import { generateWebpackConfig } from './webpackConfig'; export type InitConfigsOptions = { context: InternalContext; pluginManager: PluginManager; - rsbuildOptions: Required; + rsbuildOptions: ResolvedCreateRsbuildOptions; }; export async function initConfigs({ diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 4d429d55b3..3f5b0532a3 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -40,6 +40,7 @@ export type { CreateCompiler, CreateCompilerOptions, CreateRsbuildOptions, + ResolvedCreateRsbuildOptions, CrossOrigin, CSSLoaderOptions, CSSModules, diff --git a/scripts/test-helper/src/rsbuild.ts b/scripts/test-helper/src/rsbuild.ts index cc55e96c22..6331fb33c7 100644 --- a/scripts/test-helper/src/rsbuild.ts +++ b/scripts/test-helper/src/rsbuild.ts @@ -37,7 +37,7 @@ export async function createStubRsbuild({ } > { const { createRsbuild } = await import('@rsbuild/core'); - const rsbuildOptions: Required = { + const rsbuildOptions = { cwd: process.env.REBUILD_TEST_SUITE_CWD || process.cwd(), rsbuildConfig, ...options, From 9045831640dd2882c9b17f982663e3228af4d436 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:44:50 +0800 Subject: [PATCH 03/16] Update packages/core/src/provider/initConfigs.ts Co-authored-by: neverland --- packages/core/src/provider/initConfigs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/provider/initConfigs.ts b/packages/core/src/provider/initConfigs.ts index 9d5a2a62f5..ea759c4623 100644 --- a/packages/core/src/provider/initConfigs.ts +++ b/packages/core/src/provider/initConfigs.ts @@ -122,7 +122,7 @@ const initEnvironmentConfigs = ( if (!Object.keys(resolvedEnvironments).length) { logger.error( - `The current project is specified to run only in the ${color.yellow(specifiedEnvironments?.join(','))} environment, but the configuration of the corresponding environment was not found.`, + `The current build is specified to run only in the ${color.yellow(specifiedEnvironments?.join(','))} environment, but the configuration of the specified environment was not found.`, ); process.exit(1); } From 998fbf47663519cced81827b570d772896ace227 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:47:27 +0800 Subject: [PATCH 04/16] Update website/docs/en/guide/advanced/environments.mdx Co-authored-by: neverland --- website/docs/en/guide/advanced/environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/en/guide/advanced/environments.mdx b/website/docs/en/guide/advanced/environments.mdx index 8f71178a12..d9aa633bab 100644 --- a/website/docs/en/guide/advanced/environments.mdx +++ b/website/docs/en/guide/advanced/environments.mdx @@ -108,7 +108,7 @@ By default, Rsbuild will build all environments in the Rsbuild configuration whe # Enable build for all environments by default rsbuild dev -# Only enable web environment build +# Build for the web environment rsbuild dev --environment web # Enable web and ssr environment build From 9683317adc6bfa9981e3c183982f4f21f4e29b0e Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:47:34 +0800 Subject: [PATCH 05/16] Update website/docs/en/guide/advanced/environments.mdx Co-authored-by: neverland --- website/docs/en/guide/advanced/environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/en/guide/advanced/environments.mdx b/website/docs/en/guide/advanced/environments.mdx index d9aa633bab..51cb8b9d3d 100644 --- a/website/docs/en/guide/advanced/environments.mdx +++ b/website/docs/en/guide/advanced/environments.mdx @@ -111,7 +111,7 @@ rsbuild dev # Build for the web environment rsbuild dev --environment web -# Enable web and ssr environment build +# Build for the web and ssr environments rsbuild dev --environment web --environment ssr ``` From 359b76beeca0dada30f47a3a0c4b38521c898525 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:47:39 +0800 Subject: [PATCH 06/16] Update website/docs/en/guide/advanced/environments.mdx Co-authored-by: neverland --- website/docs/en/guide/advanced/environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/en/guide/advanced/environments.mdx b/website/docs/en/guide/advanced/environments.mdx index 51cb8b9d3d..60a386cd7c 100644 --- a/website/docs/en/guide/advanced/environments.mdx +++ b/website/docs/en/guide/advanced/environments.mdx @@ -105,7 +105,7 @@ export default { By default, Rsbuild will build all environments in the Rsbuild configuration when you execute `rsbuild dev` or `rsbuild build`. You can build only the specified environment via `--environment `. ```bash -# Enable build for all environments by default +# Build for all environments by default rsbuild dev # Build for the web environment From 973226e5c0876f6e483b2c5560f09b6f303f67b5 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:47:48 +0800 Subject: [PATCH 07/16] Update website/docs/en/guide/basic/cli.mdx Co-authored-by: neverland --- website/docs/en/guide/basic/cli.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/en/guide/basic/cli.mdx b/website/docs/en/guide/basic/cli.mdx index f0ee4377c7..b731d0f24a 100644 --- a/website/docs/en/guide/basic/cli.mdx +++ b/website/docs/en/guide/basic/cli.mdx @@ -39,7 +39,7 @@ Options: --port specify a port number for Rsbuild Server to listen --host specify the host that the Rsbuild Server listens to -c --config specify the configuration file, can be a relative or absolute path - --environment specify the environment during multi-environment + --environment specify the name of environment to build --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help display help for command From 1ea3523f453a73c987bcc0a7951bb31091b5586b Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:47:54 +0800 Subject: [PATCH 08/16] Update packages/core/src/cli/commands.ts Co-authored-by: neverland --- packages/core/src/cli/commands.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index edeb153e7c..3f5932fac8 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -42,7 +42,7 @@ const applyCommonOptions = (command: Command) => { ) .option( '--environment ', - 'specify the environment during multi-environment', + 'specify the name of environment to build', (str, prev) => (prev ? prev.concat(str.split(',')) : str.split(',')), ) .option('--env-dir ', 'specify the directory to load `.env` files'); From 9e29c50b1914b5141060e39c6b02eb7bf9416c95 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:48:06 +0800 Subject: [PATCH 09/16] Update website/docs/zh/guide/advanced/environments.mdx Co-authored-by: neverland --- website/docs/zh/guide/advanced/environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/zh/guide/advanced/environments.mdx b/website/docs/zh/guide/advanced/environments.mdx index a05eb137c4..f78846aec7 100644 --- a/website/docs/zh/guide/advanced/environments.mdx +++ b/website/docs/zh/guide/advanced/environments.mdx @@ -105,7 +105,7 @@ export default { 默认情况下,当你执行 `rsbuild dev` 或 `rsbuild build` 时,Rsbuild 会构建所有 Rsbuild 配置中的环境。你可以通过 `--environment ` 仅构建指定环境。 ```bash -# 默认为所有环境开启构建 +# 构建所有环境 rsbuild dev # 仅开启 web 环境构建 From 62f2af32723d862878015d8ac977a8ef685c50f1 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:48:14 +0800 Subject: [PATCH 10/16] Update website/docs/zh/guide/basic/cli.mdx Co-authored-by: neverland --- website/docs/zh/guide/basic/cli.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/zh/guide/basic/cli.mdx b/website/docs/zh/guide/basic/cli.mdx index d95e5da656..c267eb7c8e 100644 --- a/website/docs/zh/guide/basic/cli.mdx +++ b/website/docs/zh/guide/basic/cli.mdx @@ -39,7 +39,7 @@ Options: --port 设置 Rsbuild Server 监听的端口号 --host 指定 Rsbuild Server 启动时监听的 host -c --config 指定配置文件路径,可以为相对路径或绝对路径 - --environment 当多环境构建时仅构建指定环境 + --environment 指定需要构建的 environment 名称 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 From 80d6a0e5d57fe1f78e984c7609a854e4f78da644 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:48:23 +0800 Subject: [PATCH 11/16] Update website/docs/zh/guide/advanced/environments.mdx Co-authored-by: neverland --- website/docs/zh/guide/advanced/environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/zh/guide/advanced/environments.mdx b/website/docs/zh/guide/advanced/environments.mdx index f78846aec7..234c933d78 100644 --- a/website/docs/zh/guide/advanced/environments.mdx +++ b/website/docs/zh/guide/advanced/environments.mdx @@ -108,7 +108,7 @@ export default { # 构建所有环境 rsbuild dev -# 仅开启 web 环境构建 +# 仅构建 web 环境 rsbuild dev --environment web # 开启 web 和 ssr 环境构建 From 414cf383cca0110a3325dee7d9b7940513fae29a Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 30 Jul 2024 10:48:32 +0800 Subject: [PATCH 12/16] Update website/docs/zh/guide/advanced/environments.mdx Co-authored-by: neverland --- website/docs/zh/guide/advanced/environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/zh/guide/advanced/environments.mdx b/website/docs/zh/guide/advanced/environments.mdx index 234c933d78..6786149067 100644 --- a/website/docs/zh/guide/advanced/environments.mdx +++ b/website/docs/zh/guide/advanced/environments.mdx @@ -111,7 +111,7 @@ rsbuild dev # 仅构建 web 环境 rsbuild dev --environment web -# 开启 web 和 ssr 环境构建 +# 构建 web 和 ssr 环境 rsbuild dev --environment web --environment ssr ``` From 1487ce260e1c7cfcd24310b2460d73ea8354082a Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 30 Jul 2024 10:49:04 +0800 Subject: [PATCH 13/16] fix: format --- packages/core/src/provider/initConfigs.ts | 8 +++----- packages/core/src/types/rsbuild.ts | 10 ++-------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/packages/core/src/provider/initConfigs.ts b/packages/core/src/provider/initConfigs.ts index ea759c4623..5e87623813 100644 --- a/packages/core/src/provider/initConfigs.ts +++ b/packages/core/src/provider/initConfigs.ts @@ -121,10 +121,9 @@ const initEnvironmentConfigs = ( ); if (!Object.keys(resolvedEnvironments).length) { - logger.error( + throw new Error( `The current build is specified to run only in the ${color.yellow(specifiedEnvironments?.join(','))} environment, but the configuration of the specified environment was not found.`, ); - process.exit(1); } return resolvedEnvironments; } @@ -132,10 +131,9 @@ const initEnvironmentConfigs = ( const defaultEnvironmentName = camelCase(rsbuildSharedConfig.output.target); if (!isEnvironmentEnabled(defaultEnvironmentName)) { - logger.error( - `The current project is specified to run only in the ${color.yellow(specifiedEnvironments?.join(','))} environment, but the configuration of the corresponding environment was not found.`, + throw new Error( + `The current build is specified to run only in the ${color.yellow(specifiedEnvironments?.join(','))} environment, but the configuration of the specified environment was not found.`, ); - process.exit(1); } return { diff --git a/packages/core/src/types/rsbuild.ts b/packages/core/src/types/rsbuild.ts index 1fa4d22c29..5b18ada386 100644 --- a/packages/core/src/types/rsbuild.ts +++ b/packages/core/src/types/rsbuild.ts @@ -75,14 +75,8 @@ export type CreateRsbuildOptions = { environment?: string[]; }; -export type ResolvedCreateRsbuildOptions = { - /** The root path of current project. */ - cwd: string; - /** Configurations of Rsbuild. */ - rsbuildConfig: RsbuildConfig; - /** Only build specified environment. */ - environment?: string[]; -}; +export type ResolvedCreateRsbuildOptions = CreateRsbuildOptions & + Required>; export type ProviderInstance = { readonly bundler: Bundler; From 6c077e73c0c15e7e74e0db2958f05232f30acf99 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 30 Jul 2024 10:50:19 +0800 Subject: [PATCH 14/16] fix: update --- website/docs/en/guide/advanced/environments.mdx | 2 +- website/docs/en/guide/basic/cli.mdx | 6 +++--- website/docs/zh/guide/advanced/environments.mdx | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/website/docs/en/guide/advanced/environments.mdx b/website/docs/en/guide/advanced/environments.mdx index 60a386cd7c..22b8612f63 100644 --- a/website/docs/en/guide/advanced/environments.mdx +++ b/website/docs/en/guide/advanced/environments.mdx @@ -112,7 +112,7 @@ rsbuild dev rsbuild dev --environment web # Build for the web and ssr environments -rsbuild dev --environment web --environment ssr +rsbuild dev --environment web --environment node ``` ## Add plugins for specified environment diff --git a/website/docs/en/guide/basic/cli.mdx b/website/docs/en/guide/basic/cli.mdx index b731d0f24a..a9ea529d40 100644 --- a/website/docs/en/guide/basic/cli.mdx +++ b/website/docs/en/guide/basic/cli.mdx @@ -75,7 +75,7 @@ Usage: rsbuild build [options] Options: -w --watch turn on watch mode, watch for changes and rebuild -c --config specify the configuration file, can be a relative or absolute path - --environment specify the environment during multi-environment + --environment specify the name of environment to build --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help display help for command @@ -93,7 +93,7 @@ Options: --port specify a port number for Rsbuild Server to listen --host specify the host that the Rsbuild Server listens to -c --config specify the configuration file, can be a relative or absolute path - --environment specify the environment during multi-environment + --environment specify the name of environment to build --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help display help for command @@ -115,7 +115,7 @@ Options: --output Specify the path to output in the dist (default: "/") --verbose Show the full function in the result -c --config specify the configuration file, can be a relative or absolute path - --environment specify the environment during multi-environment + --environment specify the name of environment to build --env-mode specify the env mode to load the `.env.[mode]` file --env-dir specify the directory to load `.env` files -h, --help show command help diff --git a/website/docs/zh/guide/advanced/environments.mdx b/website/docs/zh/guide/advanced/environments.mdx index 6786149067..9a8f1ba3b0 100644 --- a/website/docs/zh/guide/advanced/environments.mdx +++ b/website/docs/zh/guide/advanced/environments.mdx @@ -112,7 +112,7 @@ rsbuild dev rsbuild dev --environment web # 构建 web 和 ssr 环境 -rsbuild dev --environment web --environment ssr +rsbuild dev --environment web --environment node ``` ## 为指定环境添加插件 From 052e8b9a1649f291e8f8133b5751757fa3a159a9 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 30 Jul 2024 16:55:03 +0800 Subject: [PATCH 15/16] docs: update --- website/docs/zh/guide/basic/cli.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/docs/zh/guide/basic/cli.mdx b/website/docs/zh/guide/basic/cli.mdx index c267eb7c8e..7c3624569b 100644 --- a/website/docs/zh/guide/basic/cli.mdx +++ b/website/docs/zh/guide/basic/cli.mdx @@ -75,7 +75,7 @@ Usage: rsbuild build [options] Options: -w --watch 开启 watch 模式, 监听文件变更并重新构建 -c --config 指定配置文件路径,可以为相对路径或绝对路径 - --environment 当多环境构建时仅构建指定环境 + --environment 指定需要构建的 environment 名称 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 @@ -93,7 +93,7 @@ Options: --port 设置 Rsbuild Server 监听的端口号 --host 指定 Rsbuild Server 启动时监听的 host -c --config 指定配置文件路径,可以为相对路径或绝对路径 - --environment 当多环境时仅预览指定环境 + --environment 指定需要构建的 environment 名称 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 @@ -115,7 +115,7 @@ Options: --output 指定在 dist 目录下输出的路径 (default: "/") --verbose 在结果中展示函数的完整内容 -c --config 指定配置文件路径,可以为相对路径或绝对路径 - --environment 当多环境构建时仅输出指定环境的配置 + --environment 指定需要构建的 environment 名称 --env-mode 指定 env 模式来加载 `.env.[mode]` 文件 --env-dir 指定目录来加载 `.env` 文件 -h, --help 显示命令帮助 From ac49a499636f3b000a8a2ccbb9b40013a32489bc Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 30 Jul 2024 19:33:07 +0800 Subject: [PATCH 16/16] docs: add shorter usage --- website/docs/en/guide/advanced/environments.mdx | 3 +++ website/docs/zh/guide/advanced/environments.mdx | 3 +++ 2 files changed, 6 insertions(+) diff --git a/website/docs/en/guide/advanced/environments.mdx b/website/docs/en/guide/advanced/environments.mdx index 22b8612f63..338bb8e4db 100644 --- a/website/docs/en/guide/advanced/environments.mdx +++ b/website/docs/en/guide/advanced/environments.mdx @@ -113,6 +113,9 @@ rsbuild dev --environment web # Build for the web and ssr environments rsbuild dev --environment web --environment node + +# Build multiple environments can be shortened to: +rsbuild dev --environment web,node ``` ## Add plugins for specified environment diff --git a/website/docs/zh/guide/advanced/environments.mdx b/website/docs/zh/guide/advanced/environments.mdx index 9a8f1ba3b0..61b70a1477 100644 --- a/website/docs/zh/guide/advanced/environments.mdx +++ b/website/docs/zh/guide/advanced/environments.mdx @@ -113,6 +113,9 @@ rsbuild dev --environment web # 构建 web 和 ssr 环境 rsbuild dev --environment web --environment node + +# 构建多个环境可以简写为: +rsbuild dev --environment web,node ``` ## 为指定环境添加插件