HMR using Vite: module is not defined
l-portet opened this issue · comments
Lucas Portet commented
Hey guys,
I'm having an error when configuring the hot reloading using Vite.
I use the same code as the one provided in the docs, I guess it's suited for Webpack only.
import { createStore } from 'easy-peasy';
import { model } from './model';
const store = createStore(model);
if (process.env.NODE_ENV === "development") {
if (module.hot) {
module.hot.accept("./model", () => {
store.reconfigure(model);
});
}
}
export default store;
It's probably not directly related to this Easy Peasy but I thought it could be useful to other folks having the same issue.
Lucas Portet commented
Reading the Vite HMR API docs, it seems the right way to support hot reloading is to use import.meta.hot
instead of module.hot
.
I changed my code like so:
import { createStore } from 'easy-peasy';
import { model } from './model';
const store = createStore(model);
if (process.env.NODE_ENV === "development") {
if (import.meta.hot) {
import.meta.hot.accept("./model", (newModel) => {
store.reconfigure(newModel.model);
});
}
}
export default store;
Self-closing this issue.