{"id":30737592,"url":"https://github.com/js2me/mobx-view-model-vite-plugin","last_synced_at":"2026-05-30T23:00:40.333Z","repository":{"id":301522493,"uuid":"1009552764","full_name":"js2me/mobx-view-model-vite-plugin","owner":"js2me","description":"vite plugin for mobx-view-model","archived":false,"fork":false,"pushed_at":"2026-05-30T21:17:59.000Z","size":156,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-30T22:20:41.864Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/js2me.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-06-27T10:13:04.000Z","updated_at":"2026-05-30T21:07:58.000Z","dependencies_parsed_at":"2025-06-27T10:15:54.666Z","dependency_job_id":"e5b462f3-4e71-4d9a-ac74-328ab1a0b735","html_url":"https://github.com/js2me/mobx-view-model-vite-plugin","commit_stats":null,"previous_names":["js2me/mobx-view-model-vite-plugin"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/js2me/mobx-view-model-vite-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js2me%2Fmobx-view-model-vite-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js2me%2Fmobx-view-model-vite-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js2me%2Fmobx-view-model-vite-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js2me%2Fmobx-view-model-vite-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/js2me","download_url":"https://codeload.github.com/js2me/mobx-view-model-vite-plugin/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js2me%2Fmobx-view-model-vite-plugin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33712579,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-09-03T21:18:40.606Z","updated_at":"2026-05-30T23:00:40.328Z","avatar_url":"https://github.com/js2me.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mobx-view-model-vite-plugin\n\nVite plugin for [mobx-view-model](https://github.com/js2me/mobx-view-model) — smart HMR, auto `displayName` for observer components, and one-click devtools setup.\n\n## Install\n\n```bash\nnpm install mobx-view-model-vite-plugin\n```\n\n## Usage\n\n```ts\n// vite.config.ts\nimport { mobxVmVitePlugin } from 'mobx-view-model-vite-plugin';\n\nexport default {\n  plugins: [mobxVmVitePlugin()],\n};\n```\n\nAll features are enabled by default in development. Nothing is injected in production builds.\n\n## Features\n\n### Smart HMR for ViewModel classes\n\nFixes [Error #2](https://js2me.github.io/mobx-view-model/errors/2) that occurs when Vite HMR replaces a ViewModel class module.\n\n**The problem:** When you edit a ViewModel class (e.g. `CounterVM`), Vite creates a new class constructor. `withViewModel(NewCounterVM)` creates/reuses a VM instance, but `ViewModelStoreBase.viewModelIdsByClasses` still maps the **old** class to the VM IDs. When `useViewModel(NewCounterVM)` tries to look up the VM, it fails because the new class isn't in the map.\n\n**The fix:** The plugin injects `import.meta.hot.dispose()` code into every ViewModel class file. On HMR update, it remaps:\n\n- `viewModelIdsByClasses` — old class → new class\n- `linkedAnchorVMClasses` — anchor → old class → anchor → new class\n- VM instance prototypes — `Object.setPrototypeOf(instance, NewClass.prototype)`\n\nNo full page reload. State is preserved. `useViewModel()` continues to work after HMR.\n\nThe plugin also ensures that all consumer modules (files using `useViewModel(YourVM)` or `withViewModel(YourVM)`) are included in the HMR update, so they receive the new class reference.\n\n### Auto `displayName` for observer components\n\nAutomatically sets `displayName` on all components wrapped in `observer()` from `mobx-react-lite` in development mode.\n\n```tsx\n// Before (no displayName in React DevTools)\nconst Header = observer(() =\u003e {\n  return \u003cdiv\u003eHello\u003c/div\u003e;\n});\n\n// After plugin transform\nconst Header = observer(() =\u003e {\n  return \u003cdiv\u003eHello\u003c/div\u003e;\n});\nHeader.displayName = 'Header';\n```\n\nWorks with named exports too:\n\n```tsx\nexport const CounterBody = observer(() =\u003e \u003cdiv\u003e{model.clicks}\u003c/div\u003e);\n// → CounterBody.displayName = \"CounterBody\"\n```\n\n### Auto-connect devtools\n\nAutomatically connects [mobx-view-model-devtools](https://github.com/js2me/mobx-view-model-devtools) to your project. No manual setup required — the plugin subscribes to `viewModelsConfig.hooks.storeCreate` and connects the devtools panel whenever a `ViewModelStore` is created.\n\n## Options\n\n```ts\nmobxVmVitePlugin({\n  hmr: true, // Smart HMR for ViewModel classes\n  autoDisplayName: true, // Auto displayName for observer() components\n  devtools: false, // Auto-connect devtools (boolean or config object)\n});\n```\n\n### `hmr`\n\n**Type:** `boolean`  \n**Default:** `true`\n\nEnable smart HMR for ViewModel classes. When a ViewModel class file changes, the plugin remaps internal store references so `useViewModel()` continues to work without a full page reload.\n\n### `autoDisplayName`\n\n**Type:** `boolean`  \n**Default:** `true`\n\nAutomatically inject `displayName` for all `observer()` wrapped components. Makes React DevTools more useful by showing component names instead of `Observer`.\n\n### `devtools`\n\n**Type:** `boolean | DevtoolsConfig`  \n**Default:** `false`\n\nAuto-connect `mobx-view-model-devtools`. Pass `true` for defaults, or an object to configure:\n\n```ts\nmobxVmVitePlugin({\n  devtools: {\n    position: 'top-right', // 'top-right' | 'top-left' | 'bottom-left' | 'bottom-right'\n    defaultIsOpened: false, // whether the panel starts open\n  },\n});\n```\n\n## How it works\n\n### HMR mechanism\n\n1. The plugin scans `.ts`/`.tsx` files for ViewModel classes (extends `ViewModelBase`, implements `ViewModel`/`ViewModelSimple`)\n2. It injects `import.meta.hot.dispose()` + class-remapping code into ViewModel class files\n3. On first load, the injected code saves current class references via `import.meta.hot.data`\n4. On HMR re-evaluation, it reads the old references, compares with new ones, and remaps the store's internal maps\n5. The runtime module subscribes to `viewModelsConfig.hooks.storeCreate` to access `ViewModelStore` instances\n\n### Store access\n\nThe plugin uses the official `viewModelsConfig.hooks.storeCreate` PubSub hook (the same mechanism used by `mobx-view-model-devtools`) to discover `ViewModelStore` instances. No monkey-patching or fragile internal hacks.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs2me%2Fmobx-view-model-vite-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjs2me%2Fmobx-view-model-vite-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs2me%2Fmobx-view-model-vite-plugin/lists"}