> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt.

# HotModuleReplacementPlugin

此插件用于实现 HMR（hot module replacement）。

## 示例

启用 HMR 非常简单，在大多数场景下无需额外配置。

注意 HMR 不能被用于生产构建，因此你需要通过 `process.env.NODE_ENV` 来判断当前是否是开发环境。

```js title="rspack.config.mjs"
import { rspack } from '@rspack/core';
const isDev = process.env.NODE_ENV === 'development';

export default {
  plugins: [isDev && new rspack.HotModuleReplacementPlugin()],
};
```

## 运行时 API

当你注册 `HotModuleReplacementPlugin` 插件后，Rspack 会注入 HMR 相关的运行时 API，例如 `module.hot` 和 `import.meta.webpackHot`。

详见 [模块热替换 API](/zh/api/runtime-api/hmr.md)。

## React Fast Refresh

为了在 React 项目中启用 Fast Refresh 能力，你还需要使用 [@rspack/plugin-react-refresh](https://www.npmjs.com/package/@rspack/plugin-react-refresh) 插件。

请参考 [React - Fast Refresh](/zh/guide/integrations/react.md#fast-refresh) 了解更多信息。

## Preact Fast Refresh

为了在 Preact 项目中启用 Fast Refresh 能力，你还需要使用 [@rspack/plugin-preact-refresh](https://www.npmjs.com/package/@rspack/plugin-preact-refresh) 插件。

请参考 [Preact - Fast Refresh](/zh/guide/integrations/preact.md#preact-refresh) 了解更多信息。
