Skip to content

Commit

Permalink
types: allow passing user config to mergeEnvironmentConfig (#2863)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Jul 10, 2024
1 parent 5e40840 commit b32983f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ export type ModifyRsbuildConfigUtils = {
mergeRsbuildConfig: (...configs: RsbuildConfig[]) => RsbuildConfig;
};

type ArrayAtLeastOne<A, B> = [A, ...Array<A | B>] | [...Array<A | B>, A];

export type ModifyEnvironmentConfigUtils = {
/** environment name. */
name: string;
/** Merge multiple Rsbuild environment config objects into one. */
mergeEnvironmentConfig: (
config: MergedEnvironmentConfig,
...configs: EnvironmentConfig[]
...configs: ArrayAtLeastOne<MergedEnvironmentConfig, EnvironmentConfig>
) => MergedEnvironmentConfig;
};

Expand Down
7 changes: 6 additions & 1 deletion website/docs/en/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ In the callback function, the config object in the parameters has already been m
- **Type:**

```ts
type ArrayAtLeastOne<A, B> = [A, ...Array<A | B>] | [...Array<A | B>, A];

type ModifyEnvironmentConfigUtils = {
/** Current environment name */
name: string;
mergeEnvironmentConfig: (
...configs: EnvironmentConfig[]
...configs: ArrayAtLeastOne<MergedEnvironmentConfig, EnvironmentConfig>
) => EnvironmentConfig;
};

Expand Down Expand Up @@ -252,6 +254,9 @@ const myPlugin = () => ({
},
};

// extraConfig will override fields in userConfig,
// If you do not want to override the fields in userConfig,
// you can adjust to `mergeEnvironmentConfig(extraConfig, userConfig)`
return mergeEnvironmentConfig(userConfig, extraConfig);
});
},
Expand Down
6 changes: 5 additions & 1 deletion website/docs/zh/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ const myPlugin = () => ({
- **类型:**

```ts
type ArrayAtLeastOne<A, B> = [A, ...Array<A | B>] | [...Array<A | B>, A];

type ModifyEnvironmentConfigUtils = {
/** 当前 environment 名称 */
name: string;
mergeEnvironmentConfig: (
...configs: EnvironmentConfig[]
...configs: ArrayAtLeastOne<MergedEnvironmentConfig, EnvironmentConfig>
) => EnvironmentConfig;
};

Expand Down Expand Up @@ -251,6 +253,8 @@ const myPlugin = () => ({
},
};

// extraConfig 会覆盖 userConfig 里的字段
// 如果你不希望覆盖 userConfig,可以调整为 `mergeEnvironmentConfig(extraConfig, userConfig)`
return mergeEnvironmentConfig(userConfig, extraConfig);
});
},
Expand Down

0 comments on commit b32983f

Please sign in to comment.