Skip to content

refactor(core): reuse base environment config#7663

Open
chenjiahan wants to merge 1 commit into
mainfrom
chenjiahan/refactor-base-environment-config
Open

refactor(core): reuse base environment config#7663
chenjiahan wants to merge 1 commit into
mainfrom
chenjiahan/refactor-base-environment-config

Conversation

@chenjiahan
Copy link
Copy Markdown
Member

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.

Copilot AI review requested due to automatic review settings May 17, 2026 14:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 baseEnvironmentConfig composed from baseConfig plus a dev subset (pick(dev, allowedEnvironmentDevKeys)).
  • Reuse baseEnvironmentConfig when merging named environment configs via mergeRsbuildConfig.
  • Reuse baseEnvironmentConfig when constructing the default environment config.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 158 to 160
const environmentConfig = {
...mergeRsbuildConfig(
{
...baseConfig,
dev: pick(dev, allowedEnvironmentDevKeys),
},
config,
),
...mergeRsbuildConfig(baseEnvironmentConfig, config),
} as unknown as MergedEnvironmentConfig;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The object spread around mergeRsbuildConfig is redundant because mergeRsbuildConfig already returns a new merged object. Removing the extra spread and braces simplifies the code.

          const environmentConfig = mergeRsbuildConfig(
            baseEnvironmentConfig,
            config,
          ) as unknown as MergedEnvironmentConfig;

Comment on lines 179 to 181
[defaultEnvironmentName]: applyEnvironmentDefaultConfig({
...baseConfig,
dev: pick(dev, allowedEnvironmentDevKeys),
...baseEnvironmentConfig,
} as MergedEnvironmentConfig),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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,
    ),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants