Rsbuild 支持同时为多个环境构建产物。你可以使用 environments
为每个环境定义不同的 Rsbuild 配置。
请查看 多环境构建 了解更多。
interface EnvironmentConfig {
plugins?: RsbuildPlugins;
dev?: Pick<
DevConfig,
| 'hmr'
| 'client'
| 'liveReload'
| 'assetPrefix'
| 'progressBar'
| 'lazyCompilation'
| 'writeToDisk'
>;
html?: HtmlConfig;
tools?: ToolsConfig;
source?: SourceConfig;
output?: OutputConfig;
resolve?: ResolveConfig;
security?: SecurityConfig;
performance?: PerformanceConfig;
moduleFederation?: ModuleFederationConfig;
}
type Environments = {
[name: string]: EnvironmentConfig;
};
undefined
environments
中不支持配置 server
选项以及一部分 dev
选项,因为多个 environment 共享同一个 server 实例。
分别为 web
(client) 和 node
(SSR) 环境配置 Rsbuild:
export default {
// 所有环境共享配置
resolve: {
alias: {
'@common': './src/common',
},
},
environments: {
// client 环境配置
web: {
source: {
entry: {
index: './src/index.client.js',
},
},
output: {
target: 'web',
},
resolve: {
alias: {
'@common1': './src/web/common1',
},
},
},
// SSR 环境配置
node: {
source: {
entry: {
index: './src/index.server.js',
},
},
output: {
target: 'node',
},
resolve: {
alias: {
'@common1': './src/ssr/common1',
},
},
},
},
};
对于 web
环境,合并后的 Rsbuild 配置为:
const webConfig = {
source: {
entry: {
index: './src/index.client.js',
},
},
output: {
target: 'web',
},
resolve: {
alias: {
'@common': './src/common',
'@common1': './src/web/common1',
},
},
};
对于 node
环境,合并后的 Rsbuild 配置为:
const nodeConfig = {
source: {
entry: {
index: './src/index.server.js',
},
},
output: {
target: 'node',
},
resolve: {
alias: {
'@common': './src/common',
'@common1': './src/ssr/common1',
},
},
};
由于 environment 名称会用于目录名和对象属性名,因此建议只包含字母、数字、-
、_
和 $
,使用其他字符时,Rsbuild 会输出 warning 进行提示。
export default {
environments: {
someName: {}, // ✅
some_name: {}, // ✅
'some-name': {}, // ✅
'some:name': {}, // ❌
},
};