ctrlplusb / easy-peasy

Vegetarian friendly state for React

Home Page:https://easy-peasy.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HMR using Vite: module is not defined

l-portet opened this issue · comments

Hey guys,

I'm having an error when configuring the hot reloading using Vite.

Screenshot 2023-02-02 at 5 06 28 PM

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.

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.