diff --git a/packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts b/packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts index f32d095..6cb74b6 100644 --- a/packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts +++ b/packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts @@ -147,21 +147,58 @@ export default async ( throw new Error('Cache is required') } + let contentFromConfig: RsbuildConfig = {} const { content } = await loadConfig({ cwd: workingDir, path: rsbuildConfigPath, }) + const { environments, ...withoutEnv } = content + if (content.environments) { + const envCount = Object.keys(content.environments).length + if (envCount === 0) { + // Empty useless environment field. + contentFromConfig = withoutEnv + } else if (envCount === 1) { + // Directly use the unique environment. + contentFromConfig = mergeRsbuildConfig( + withoutEnv, + content.environments[0], + ) + } else { + // User need to specify the environment first if more than one provided. + const userEnv = builderOptions.environment + if (typeof userEnv !== 'string') { + throw new Error( + 'You must specify an environment when there are multiple environments in the Rsbuild config.', + ) + } + + if (Object.keys(content.environments).includes(userEnv)) { + contentFromConfig = mergeRsbuildConfig( + withoutEnv, + content.environments[userEnv], + ) + } else { + throw new Error( + `The specified environment "${userEnv}" is not found in the Rsbuild config.`, + ) + } + } + } else { + contentFromConfig = content + } + // Reset `config.source.entry` field, do not use provided entry // see https://github.com/rspack-contrib/storybook-rsbuild/issues/43 - content.source ??= {} - content.source.entry = {} + contentFromConfig.source ??= {} + contentFromConfig.source.entry = {} const resourceFilename = isProd ? 'static/media/[name].[contenthash:8][ext]' : 'static/media/[path][name][ext]' - const merged = mergeRsbuildConfig(content, { + const merged = mergeRsbuildConfig(contentFromConfig, { output: { cleanDistPath: false, dataUriLimit: { diff --git a/packages/builder-rsbuild/src/types.ts b/packages/builder-rsbuild/src/types.ts index 6d1306b..caea197 100644 --- a/packages/builder-rsbuild/src/types.ts +++ b/packages/builder-rsbuild/src/types.ts @@ -43,6 +43,10 @@ export type BuilderOptions = { * Enable Rspack's lazy compilation (experimental). */ lazyCompilation?: boolean + /** + * Which environment to use from the Rsbuild config. + */ + environment?: string } export interface BuilderResult extends BuilderResultBase { diff --git a/sandboxes/react-16/rsbuild.config.ts b/sandboxes/react-16/rsbuild.config.ts index 94cc799..27da09d 100644 --- a/sandboxes/react-16/rsbuild.config.ts +++ b/sandboxes/react-16/rsbuild.config.ts @@ -2,5 +2,12 @@ import { defineConfig } from '@rsbuild/core' import { pluginReact } from '@rsbuild/plugin-react' export default defineConfig({ + environments: { + web: { + source: { + entry: { index: './src/index.tsx' }, + }, + }, + }, plugins: [pluginReact()], })