The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.
The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.
Rspack provides the same interface as webpack for implementing HMR.
If you've enabled Hot Module Replacement via the HotModuleReplacementPlugin, its interface is exposed on the module.hot and import.meta.webpackHot objects.
In ESM, use import.meta.webpackHot instead of module.hot.
First, check if the interface is available, then use it. Here's how to accept an updated module:
The following methods are supported by module.hot and import.meta.webpackHot.
Accepts updates for the given dependencies and fires a callback to handle those updates. You can also attach an optional error handler:
When using ESM import, all imported symbols from dependencies are automatically updated. Note: The dependency string must exactly match the from string in the import. In some cases, callback can be omitted. Using require() in the callback isn't useful here.
When using CommonJS, update dependencies manually using require() in the callback. Omitting the callback isn't useful here.
(err, {moduleId, dependencyId}) => {}
err: The error thrown by the callback in the second argument or during dependency execution when using ESM dependencies.moduleId: The current module ID.dependencyId: The module ID of the (first) changed dependency.Accepts updates for itself.
When this module or its dependencies are updated, it can be disposed and re-evaluated without notifying parents. This makes sense if the module has no exports (or exports are updated in another way).
The errorHandler fires when evaluating this module (or its dependencies) throws an exception.
(err, {moduleId, module}) => {}
err: The error when evaluating the new version.moduleId: The current module ID.module: The current module instance.
module.hot: Allows using the HMR API of the errored module instance. A common scenario is to self-accept it again. It also makes sense to add a dispose handler to pass data along. Note that the errored module might already be partially executed, so avoid getting into an inconsistent state. Use module.hot.data to store partial state.module.exports: Can be overridden, but be careful since property names might be mangled in production mode.Rejects updates for the given dependencies, forcing the update to fail with a 'decline' code.
Flags a dependency as not updatable. This makes sense when changes to this dependency's exports can't be handled or handling isn't implemented yet. Depending on your HMR management code, updates to these dependencies (or their unaccepted dependencies) usually cause a full page reload.
Rejects updates for itself.
Flag this module as not-update-able. This makes sense when this module has irreversible side-effects, or HMR handling is not implemented for this module yet. Depending on your HMR management code, an update to this module (or unaccepted dependencies) usually causes a full-reload of the page.
Add a handler which is executed when the current module code is replaced. This should be used to remove any persistent resource you have claimed or created. If you want to transfer state to the updated module, add it to the given data parameter. This object will be available at module.hot.data after the update.
Calling this method will invalidate the current module, which disposes and recreates it when the HMR update is applied. This bubbles like a normal update of this module. invalidate can't be self-accepted by this module.
When called during the idle state, a new HMR update will be created containing this module. HMR will enter the ready state.
When called during the ready or prepare state, this module will be added to the current HMR update.
When called during the check state, this module will be added to the update when an update is available. If no update is available it will create a new update. HMR will enter the ready state.
When called during the dispose or apply state, HMR will pick it up after getting out of those states.
Conditional Accepting
A module can accept a dependency, but can call invalidate when the change of the dependency is not handleable:
Conditional self accept
A module can self-accept itself, but can invalidate itself when the change is not handleable:
Triggering custom HMR updates
T> When invalidate is called, the dispose handler will be eventually called and fill module.hot.data. If dispose handler is not registered, an empty object will be supplied to module.hot.data.
W> Do not get caught in an invalidate loop, by calling invalidate again and again. This will result in stack overflow and HMR entering the fail state.
Remove the handler added via dispose or addDisposeHandler.
Retrieve the current status of the hot module replacement process.
| Status | Description |
|---|---|
| idle | The process is waiting for a call to check |
| check | The process is checking for updates |
| prepare | The process is getting ready for the update (e.g. downloading the updated module) |
| ready | The update is prepared and available |
| dispose | The process is calling the dispose handlers on the modules that will be replaced |
| apply | The process is calling the accept handlers and re-executing self-accepted modules |
| abort | An update was aborted, but the system is still in its previous state |
| fail | An update has thrown an exception and the system's state has been compromised |
Test all loaded modules for updates and, if updates exist, apply them.
The autoApply parameter can either be a boolean or options to pass to the apply method when called.
Continue the update process (as long as module.hot.status() === 'ready').
The optional options object can include the following properties:
ignoreUnaccepted (boolean): Ignore changes made to unaccepted modules.ignoreDeclined (boolean): Ignore changes made to declined modules.ignoreErrored (boolean): Ignore errors thrown in accept handlers, error handlers and while reevaluating module.onDeclined (function(info)): Notifier for declined modulesonUnaccepted (function(info)): Notifier for unaccepted modulesonAccepted (function(info)): Notifier for accepted modulesonDisposed (function(info)): Notifier for disposed modulesonErrored (function(info)): Notifier for errorsThe info parameter will be an object containing some of the following values:
Register a function to listen for changes in status.
Bear in mind that when the status handler returns a Promise, the HMR system will wait for the Promise to resolve before continuing.
Remove a registered status handler.