Skip to content

[Feature]: Support multiple environment configuration #2620

@9aoy

Description

@9aoy

What problem does this feature solve?

Enhance the current multi-target building capabilities, users can define completely different Rsbuild configurations for different environments.

Different environments can be different usage scenarios of the same rsbuild target (node ​​SSR / node RSC) or different rsbuild targets (web / node SSR)

before:
image

after:
image

What does the proposed API look like?

The design of environment configuration in Rsbuild is inspired by Vite Environment API. Thanks to the Vite team ❤️

Add the environments field in Rsbuild config, and environment configuration can override the basic Rsbuild configuration.

export default {
  // Shared configuration for all environments
  source: {
    alias: {
      '@common': './src/common',
    },
  },
  environments: {
    // configuration for web
    web: {
      source: {
        entry: {
          index: './src/index.client.js',
        }
      },
      target: 'web',
      tools: {
        bundlerChain: (chain) => {
           chain.plugin('xxx').use(xxx);
        }
      },
    },
    // configuration for ssr
    'ssr': {
      source: {
        entry: {
          index: './src/index.server.js',
        }
      },
       target: 'node',
       tools: {
          htmlPlugin: false,
       }
    },
  },
};
### MVP
- [ ] https://github.com/web-infra-dev/rsbuild/issues/2623
- [ ] https://github.com/web-infra-dev/rsbuild/issues/2632
- [x] chore: Generate rspack config based on environment instead of `output.targets`
- [x] breaking change:  Replace `output.targets` with `output.target`
- [x] breaking change:  Replace api.context.targets / entry / distPath / xxx with api.environments[index].xxx
- [x] feat: Should use environment.config instead of `api.getNormalizedConfig()` in plugins
- [x] feat: `api.getHTMLPaths` support get specify environment htmlPath by environment parameter
- [x] breaking change: `output.emitAssets` changed to boolean type
- [x] breaking change: `output.overrideBrowserslist` no longer supports object type
### More
- [x] feat: support add single environment plugin via `environments.[name].plugins` configuration.
- [x] feat: inspect config support output environment config
- [x] feat: supports building only specified environment through `--environment=xxx` in rsbuild cli.
- [x] feat: rsbuild server supports `rsbuild.environments.xxx` api
- [x] breaking change: Deprecated `service-worker` target
- [x] breaking change: `output.distPath.server / worker` no longer support
- [x] breaking change: Deprecated target / isServer / isWebWorker util in configuration function

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions