From b32983fc61386e73031d948b9d1abbedadcbe385 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Wed, 10 Jul 2024 15:01:43 +0800 Subject: [PATCH] types: allow passing user config to mergeEnvironmentConfig (#2863) --- packages/core/src/types/hooks.ts | 5 +++-- website/docs/en/plugins/dev/hooks.mdx | 7 ++++++- website/docs/zh/plugins/dev/hooks.mdx | 6 +++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/core/src/types/hooks.ts b/packages/core/src/types/hooks.ts index d424d22172..d408542cec 100644 --- a/packages/core/src/types/hooks.ts +++ b/packages/core/src/types/hooks.ts @@ -104,13 +104,14 @@ export type ModifyRsbuildConfigUtils = { mergeRsbuildConfig: (...configs: RsbuildConfig[]) => RsbuildConfig; }; +type ArrayAtLeastOne = [A, ...Array] | [...Array, A]; + export type ModifyEnvironmentConfigUtils = { /** environment name. */ name: string; /** Merge multiple Rsbuild environment config objects into one. */ mergeEnvironmentConfig: ( - config: MergedEnvironmentConfig, - ...configs: EnvironmentConfig[] + ...configs: ArrayAtLeastOne ) => MergedEnvironmentConfig; }; diff --git a/website/docs/en/plugins/dev/hooks.mdx b/website/docs/en/plugins/dev/hooks.mdx index fb2810b2ea..46e283177d 100644 --- a/website/docs/en/plugins/dev/hooks.mdx +++ b/website/docs/en/plugins/dev/hooks.mdx @@ -203,11 +203,13 @@ In the callback function, the config object in the parameters has already been m - **Type:** ```ts +type ArrayAtLeastOne = [A, ...Array] | [...Array, A]; + type ModifyEnvironmentConfigUtils = { /** Current environment name */ name: string; mergeEnvironmentConfig: ( - ...configs: EnvironmentConfig[] + ...configs: ArrayAtLeastOne ) => EnvironmentConfig; }; @@ -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); }); }, diff --git a/website/docs/zh/plugins/dev/hooks.mdx b/website/docs/zh/plugins/dev/hooks.mdx index a1a1bf59a6..8da48041b1 100644 --- a/website/docs/zh/plugins/dev/hooks.mdx +++ b/website/docs/zh/plugins/dev/hooks.mdx @@ -202,11 +202,13 @@ const myPlugin = () => ({ - **类型:** ```ts +type ArrayAtLeastOne = [A, ...Array] | [...Array, A]; + type ModifyEnvironmentConfigUtils = { /** 当前 environment 名称 */ name: string; mergeEnvironmentConfig: ( - ...configs: EnvironmentConfig[] + ...configs: ArrayAtLeastOne ) => EnvironmentConfig; }; @@ -251,6 +253,8 @@ const myPlugin = () => ({ }, }; + // extraConfig 会覆盖 userConfig 里的字段 + // 如果你不希望覆盖 userConfig,可以调整为 `mergeEnvironmentConfig(extraConfig, userConfig)` return mergeEnvironmentConfig(userConfig, extraConfig); }); },