diff --git a/packages/core/src/createRsbuild.ts b/packages/core/src/createRsbuild.ts index b9f849c72d..a028365bc8 100644 --- a/packages/core/src/createRsbuild.ts +++ b/packages/core/src/createRsbuild.ts @@ -117,6 +117,7 @@ export const pickRsbuildConfig = ( 'security', 'performance', 'moduleFederation', + 'environments', '_privateMeta', ]; return pick(rsbuildConfig, keys); diff --git a/packages/core/tests/__snapshots__/environments.test.ts.snap b/packages/core/tests/__snapshots__/environments.test.ts.snap new file mode 100644 index 0000000000..71412f61ce --- /dev/null +++ b/packages/core/tests/__snapshots__/environments.test.ts.snap @@ -0,0 +1,49 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`environment config > should print environment config when inspect config 1`] = ` +{ + "ssr": { + "source": { + "entry": { + "index": "src/index.server.js", + }, + }, + }, + "web": { + "source": { + "entry": { + "index": "src/index.client.js", + }, + }, + }, +} +`; + +exports[`environment config > should support modify environment config by api.modifyRsbuildConfig 1`] = ` +{ + "ssr": { + "source": { + "entry": { + "index": "src/index.server.js", + }, + }, + }, + "web": { + "source": { + "alias": { + "@common1": "src/common1", + }, + "entry": { + "index": "src/index.client.js", + }, + }, + }, + "web1": { + "source": { + "alias": { + "@common1": "src/common1", + }, + }, + }, +} +`; diff --git a/packages/core/tests/environments.test.ts b/packages/core/tests/environments.test.ts new file mode 100644 index 0000000000..0e5c969034 --- /dev/null +++ b/packages/core/tests/environments.test.ts @@ -0,0 +1,101 @@ +import { createRsbuild } from '../src'; + +describe('environment config', () => { + it('should support modify environment config by api.modifyRsbuildConfig', async () => { + process.env.NODE_ENV = 'development'; + const rsbuild = await createRsbuild({ + rsbuildConfig: { + source: { + alias: { + '@common': './src/common', + }, + }, + environments: { + web: { + source: { + entry: { + index: './src/index.client.js', + }, + }, + }, + ssr: { + source: { + entry: { + index: './src/index.server.js', + }, + }, + }, + }, + }, + }); + + rsbuild.addPlugins([ + { + name: 'test-environment', + setup: (api) => { + api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => { + return mergeRsbuildConfig(config, { + environments: { + web: { + source: { + alias: { + '@common1': './src/common1', + }, + }, + }, + web1: { + source: { + alias: { + '@common1': './src/common1', + }, + }, + }, + }, + }); + }); + }, + }, + ]); + + const { + origin: { rsbuildConfig }, + } = await rsbuild.inspectConfig(); + + expect(rsbuildConfig.environments).toMatchSnapshot(); + }); + + it('should print environment config when inspect config', async () => { + process.env.NODE_ENV = 'development'; + const rsbuild = await createRsbuild({ + rsbuildConfig: { + source: { + alias: { + '@common': './src/common', + }, + }, + environments: { + web: { + source: { + entry: { + index: './src/index.client.js', + }, + }, + }, + ssr: { + source: { + entry: { + index: './src/index.server.js', + }, + }, + }, + }, + }, + }); + + const { + origin: { rsbuildConfig }, + } = await rsbuild.inspectConfig(); + + expect(rsbuildConfig.environments).toMatchSnapshot(); + }); +}); diff --git a/packages/shared/src/types/config/index.ts b/packages/shared/src/types/config/index.ts index 2fdd157a4e..3d77f79b4e 100644 --- a/packages/shared/src/types/config/index.ts +++ b/packages/shared/src/types/config/index.ts @@ -27,14 +27,9 @@ export type RsbuildConfigMeta = { }; /** - * The shared Rsbuild config. - * Can be used with both Rspack and Webpack. + * The Rsbuild config to run in the specified environment. * */ -export interface RsbuildConfig { - /** - * Options for local development. - */ - dev?: DevConfig; +export interface EnvironmentConfig { /** * Options for HTML generation. */ @@ -47,19 +42,10 @@ export interface RsbuildConfig { * Options for source code parsing and compilation. */ source?: SourceConfig; - /** - * Options for the Rsbuild Server, - * will take effect during local development and preview. - */ - server?: ServerConfig; /** * Options for build outputs. */ output?: OutputConfig; - /** - * Configure Rsbuild plugins. - */ - plugins?: RsbuildPlugins; /** * Options for Web security. */ @@ -72,6 +58,31 @@ export interface RsbuildConfig { * Options for module federation. */ moduleFederation?: ModuleFederationConfig; +} + +/** + * The Rsbuild config. + * */ +export interface RsbuildConfig extends EnvironmentConfig { + /** + * Options for local development. + */ + dev?: DevConfig; + /** + * Options for the Rsbuild Server, + * will take effect during local development and preview. + */ + server?: ServerConfig; + /** + * Configure Rsbuild plugins. + */ + plugins?: RsbuildPlugins; + /** + * Configure rsbuild config by environment. + */ + environments?: { + [name: string]: EnvironmentConfig; + }; /** * Used to switch the bundler type. */ @@ -95,6 +106,9 @@ export type NormalizedConfig = DeepReadonly<{ moduleFederation?: ModuleFederationConfig; provider?: unknown; _privateMeta?: RsbuildConfigMeta; + environments?: { + [name: string]: EnvironmentConfig; + }; }>; export * from './dev';