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

docs: add modifyEnvironmentConfig hook #2756

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 62 additions & 0 deletions website/docs/en/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,68 @@ const myPlugin = () => ({
});
```

### modifyEnvironmentConfig

Modify the environment config passed to the Rsbuild, you can directly modify the config object, or return a new object to replace the previous object.
9aoy marked this conversation as resolved.
Show resolved Hide resolved

- **Type:**

```ts
type ModifyEnvironmentConfigUtils = {
/** Current Environment name */
9aoy marked this conversation as resolved.
Show resolved Hide resolved
name: string;
mergeEnvironmentConfig: (
...configs: EnvironmentConfig[]
) => EnvironmentConfig;
};

function ModifyEnvironmentConfig(
callback: (
config: EnvironmentConfig,
utils: ModifyEnvironmentConfigUtils,
) => MaybePromise<EnvironmentConfig | void>,
): void;
```

- **Example:** Set a default value for a specific Environment configuration item:
9aoy marked this conversation as resolved.
Show resolved Hide resolved

```ts
const myPlugin = () => ({
setup: (api) => {
api.modifyEnvironmentConfig((config, { name }) => {
if (name !== 'web') {
return config;
}
config.html ||= {};
config.html.title = 'My Default Title';
});
},
});
```

- **Example:** Using `mergeEnvironmentConfig` to merge config objects, and return the merged object.

```ts
import type { EnvironmentConfig } from '@rsbuild/core';

const myPlugin = () => ({
setup: (api) => {
api.modifyEnvironmentConfig((userConfig, { mergeEnvironmentConfig }) => {
const extraConfig: EnvironmentConfig = {
source: {
// ...
},
output: {
// ...
},
};

return mergeEnvironmentConfig(userConfig, extraConfig);
});
},
});
```

### modifyRspackConfig

To modify the final Rspack config object, you can directly modify the config object, or return a new object to replace the previous object.
Expand Down
62 changes: 62 additions & 0 deletions website/docs/zh/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,68 @@ const myPlugin = () => ({
});
```

### modifyEnvironmentConfig

修改传递给 Rsbuild 的某个 Environment 的配置项,你可以直接修改传入的 config 对象,也可以返回一个新的对象来替换传入的对象。
9aoy marked this conversation as resolved.
Show resolved Hide resolved

- **类型:**

```ts
type ModifyEnvironmentConfigUtils = {
/** 当前 Environment 名称 */
9aoy marked this conversation as resolved.
Show resolved Hide resolved
name: string;
mergeEnvironmentConfig: (
...configs: EnvironmentConfig[]
) => EnvironmentConfig;
};

function ModifyEnvironmentConfig(
callback: (
config: EnvironmentConfig,
utils: ModifyEnvironmentConfigUtils,
) => MaybePromise<EnvironmentConfig | void>,
): void;
```

- **示例:** 为某个 Environment 的配置项设置一个默认值:
9aoy marked this conversation as resolved.
Show resolved Hide resolved

```ts
const myPlugin = () => ({
setup: (api) => {
api.modifyEnvironmentConfig((config, { name }) => {
if (name !== 'web') {
return config;
}
config.html ||= {};
config.html.title = 'My Default Title';
});
},
});
```

- **示例:** 通过 `mergeEnvironmentConfig` 合并配置多个对象,并返回合并后的对象。

```ts
import type { EnvironmentConfig } from '@rsbuild/core';

const myPlugin = () => ({
setup: (api) => {
api.modifyEnvironmentConfig((userConfig, { mergeEnvironmentConfig }) => {
const extraConfig: EnvironmentConfig = {
source: {
// ...
},
output: {
// ...
},
};

return mergeEnvironmentConfig(userConfig, extraConfig);
});
},
});
```

### modifyRspackConfig

修改最终的 Rspack 配置对象,你可以直接修改传入的 config 对象,也可以返回一个新的对象来替换传入的对象。
Expand Down
Loading