Skip to content

Vite plugin ​

A Vite plugin that makes working with mobx-view-model smoother: smart HMR, automatic displayName for observer components, and one-click devtools setup.

Installation ​

bash
npm install mobx-view-model-vite-plugin
bash
pnpm add mobx-view-model-vite-plugin
bash
yarn add mobx-view-model-vite-plugin

Setup ​

Add the plugin to your vite.config.ts:

ts
import { mobxVmVitePlugin } from "mobx-view-model-vite-plugin";

export default {
  plugins: [mobxVmVitePlugin()],
};

Smart HMR and automatic display names are enabled by default in development. Enable devtools explicitly with devtools: true. The plugin injects nothing in production builds.

Features ​

Smart HMR for ViewModel classes ​

Fixes Error #2 that shows up when Vite HMR replaces a ViewModel class module.

Auto displayName for observer components ​

In dev mode, the plugin automatically sets displayName for direct named assignments wrapped in observer() from mobx-react-lite (for example, const Header = observer(...)).

tsx
// Before (no name in React DevTools)
const Header = observer(() => {
  return <div>Hello</div>;
});

// After plugin transform
const Header = observer(() => {
  return <div>Hello</div>;
});
Header.displayName = 'Header';

Works with named exports too:

tsx
export const CounterBody = observer(() => <div>{model.clicks}</div>);
// → CounterBody.displayName = "CounterBody"

Auto-connect devtools ​

The plugin hooks up mobx-view-model-devtools for you — no manual setup. It subscribes to viewModelsConfig.hooks.storeCreate and connects the devtools panel whenever a ViewModelStore is created.

Options ​

ts
mobxVmVitePlugin({
  hmr: true, // smart HMR for ViewModel classes
  autoDisplayName: true, // auto displayName for observer() components
  devtools: false, // auto-connect devtools (boolean or config object)
});

hmr ​

  • Type: boolean
  • Default: true

Enables smart HMR for ViewModel classes. When a ViewModel class file changes, the plugin remaps internal store references so useViewModel() keeps working without a full page reload.

Ordinary prototype methods can be updated while keeping existing instances and their state. Observable field accessors and computed properties or actions inherited from an unchanged base class do not force a reload. If a live instance has private class fields, accessors or bound actions from a changed prototype, constructor-created functions (including arrow fields), or is non-extensible, the plugin reloads the page instead. These cannot be safely updated by replacing the prototype. All affected instances are checked before any store is changed. Removing or renaming a ViewModel class also reloads the page.

Constructors and field initializers are not re-run during a state-preserving update. Reload the page after changing initialization logic.

autoDisplayName ​

  • Type: boolean
  • Default: true

Automatically injects displayName for direct named assignments wrapped in observer(). Instead of Observer in React DevTools, you'll see the actual component name.

devtools ​

  • Type: boolean | DevtoolsConfig
  • Default: false

Auto-connects mobx-view-model-devtools. Pass true for defaults, or an object to configure:

ts
mobxVmVitePlugin({
  devtools: {
    position: 'top-right', // 'top-right' | 'top-left' | 'bottom-left' | 'bottom-right'
    defaultIsOpened: false, // whether the panel starts open
  },
});