Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: init environment config #2624

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/createRsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export const pickRsbuildConfig = (
'security',
'performance',
'moduleFederation',
'environments',
'_privateMeta',
];
return pick(rsbuildConfig, keys);
Expand Down
49 changes: 49 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`environment config > should print environment config when inspect config 1`] = `
{
"ssr": {
"source": {
"entry": {
"index": "src/index.server.js",
},
},
},
"web": {
"source": {
"entry": {
"index": "src/index.client.js",
},
},
},
}
`;

exports[`environment config > should support modify environment config by api.modifyRsbuildConfig 1`] = `
{
"ssr": {
"source": {
"entry": {
"index": "src/index.server.js",
},
},
},
"web": {
"source": {
"alias": {
"@common1": "src/common1",
},
"entry": {
"index": "src/index.client.js",
},
},
},
"web1": {
"source": {
"alias": {
"@common1": "src/common1",
},
},
},
}
`;
101 changes: 101 additions & 0 deletions packages/core/tests/environments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { createRsbuild } from '../src';

describe('environment config', () => {
it('should support modify environment config by api.modifyRsbuildConfig', async () => {
process.env.NODE_ENV = 'development';
const rsbuild = await createRsbuild({
rsbuildConfig: {
source: {
alias: {
'@common': './src/common',
},
},
environments: {
web: {
source: {
entry: {
index: './src/index.client.js',
},
},
},
ssr: {
source: {
entry: {
index: './src/index.server.js',
},
},
},
},
},
});

rsbuild.addPlugins([
{
name: 'test-environment',
setup: (api) => {
api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
return mergeRsbuildConfig(config, {
environments: {
web: {
source: {
alias: {
'@common1': './src/common1',
},
},
},
web1: {
source: {
alias: {
'@common1': './src/common1',
},
},
},
},
});
});
},
},
]);

const {
origin: { rsbuildConfig },
} = await rsbuild.inspectConfig();

expect(rsbuildConfig.environments).toMatchSnapshot();
});

it('should print environment config when inspect config', async () => {
process.env.NODE_ENV = 'development';
const rsbuild = await createRsbuild({
rsbuildConfig: {
source: {
alias: {
'@common': './src/common',
},
},
environments: {
web: {
source: {
entry: {
index: './src/index.client.js',
},
},
},
ssr: {
source: {
entry: {
index: './src/index.server.js',
},
},
},
},
},
});

const {
origin: { rsbuildConfig },
} = await rsbuild.inspectConfig();

expect(rsbuildConfig.environments).toMatchSnapshot();
});
});
48 changes: 32 additions & 16 deletions packages/shared/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ export type RsbuildConfigMeta = {
};

/**
* The shared Rsbuild config.
* Can be used with both Rspack and Webpack.
* The rsbuild config to run in the specified environment.
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
* */
export interface RsbuildConfig {
/**
* Options for local development.
*/
dev?: DevConfig;

export interface RsbuildEnvironmentConfig {
9aoy marked this conversation as resolved.
Show resolved Hide resolved
/**
* Options for HTML generation.
*/
Expand All @@ -47,19 +43,10 @@ export interface RsbuildConfig {
* Options for source code parsing and compilation.
*/
source?: SourceConfig;
/**
* Options for the Rsbuild Server,
* will take effect during local development and preview.
*/
server?: ServerConfig;
/**
* Options for build outputs.
*/
output?: OutputConfig;
/**
* Configure Rsbuild plugins.
*/
plugins?: RsbuildPlugins;
/**
* Options for Web security.
*/
Expand All @@ -72,6 +59,32 @@ export interface RsbuildConfig {
* Options for module federation.
*/
moduleFederation?: ModuleFederationConfig;
}

/**
* The shared Rsbuild config.
* Can be used with both Rspack and Webpack.
Copy link
Member

Choose a reason for hiding this comment

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

I think this comment is outdated

* */
export interface RsbuildConfig extends RsbuildEnvironmentConfig {
/**
* Options for local development.
*/
dev?: DevConfig;
/**
* Options for the Rsbuild Server,
* will take effect during local development and preview.
*/
server?: ServerConfig;
/**
* Configure Rsbuild plugins.
*/
plugins?: RsbuildPlugins;
/**
* Configure rsbuild config by environment.
*/
environments?: {
[name: string]: RsbuildEnvironmentConfig;
};
/**
* Used to switch the bundler type.
*/
Expand All @@ -95,6 +108,9 @@ export type NormalizedConfig = DeepReadonly<{
moduleFederation?: ModuleFederationConfig;
provider?: unknown;
_privateMeta?: RsbuildConfigMeta;
environments?: {
[name: string]: RsbuildEnvironmentConfig;
};
}>;

export * from './dev';
Expand Down
Loading