refactor(core): reuse base environment config#7663
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors environment initialization in @rsbuild/core to reuse a single shared “base environment” config object, keeping the default-environment path and named-environments path aligned while avoiding duplicated base-config construction.
Changes:
- Introduce a
baseEnvironmentConfigcomposed frombaseConfigplus a dev subset (pick(dev, allowedEnvironmentDevKeys)). - Reuse
baseEnvironmentConfigwhen merging named environment configs viamergeRsbuildConfig. - Reuse
baseEnvironmentConfigwhen constructing the default environment config.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Code Review
This pull request refactors initEnvironmentConfigs in packages/core/src/initConfigs.ts by extracting a shared baseEnvironmentConfig object to reduce code duplication. Feedback from the review suggests removing a redundant object spread around mergeRsbuildConfig for better clarity. Additionally, it is recommended to use mergeRsbuildConfig instead of a shallow spread for the default environment configuration to avoid side effects caused by mutating nested properties in the base configuration.
| const environmentConfig = { | ||
| ...mergeRsbuildConfig( | ||
| { | ||
| ...baseConfig, | ||
| dev: pick(dev, allowedEnvironmentDevKeys), | ||
| }, | ||
| config, | ||
| ), | ||
| ...mergeRsbuildConfig(baseEnvironmentConfig, config), | ||
| } as unknown as MergedEnvironmentConfig; |
There was a problem hiding this comment.
| [defaultEnvironmentName]: applyEnvironmentDefaultConfig({ | ||
| ...baseConfig, | ||
| dev: pick(dev, allowedEnvironmentDevKeys), | ||
| ...baseEnvironmentConfig, | ||
| } as MergedEnvironmentConfig), |
There was a problem hiding this comment.
Using a shallow spread { ...baseEnvironmentConfig } means that nested objects like source and output are shared with the original normalizedConfig. Since applyEnvironmentDefaultConfig mutates these nested properties (e.g., config.source.entry), it leads to side effects on the base configuration passed to this function. For consistency with the multi-environment path and to ensure the base configuration remains immutable, consider using mergeRsbuildConfig to create a fresh copy.
[defaultEnvironmentName]: applyEnvironmentDefaultConfig(
mergeRsbuildConfig(baseEnvironmentConfig) as MergedEnvironmentConfig,
),
Summary
This PR reuses a shared base environment config when initializing Rsbuild environments, keeping the default and named environment paths aligned while avoiding duplicate construction of the same base config.