From 4a8a369b47c72374dd2de5285c9d3a2eb0e8e28a Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 4 Aug 2021 11:47:54 +0200 Subject: [PATCH] chore: repro for vite --- playground/src/main.ts | 20 +++++++++++++++++++- playground/src/stores/counter.ts | 6 +++--- src/index.ts | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/playground/src/main.ts b/playground/src/main.ts index 3de1a062..22cc088e 100644 --- a/playground/src/main.ts +++ b/playground/src/main.ts @@ -1,6 +1,6 @@ import { computed, createApp, markRaw } from 'vue' import App from './App.vue' -import { createPinia } from '../../src' +import { acceptHMRUpdate, createPinia, isUseStore } from '../../src' import { router } from './router' const pinia = createPinia() @@ -10,6 +10,24 @@ pinia.use(() => ({ })) if (import.meta.hot) { + const stores = import.meta.glob('./stores/*.ts') + for (const filePath in stores) { + console.log('configuring HMR for', filePath) + + stores[filePath]().then((mod) => { + for (const exportName in mod) { + const storeDef = mod[exportName] + if (isUseStore(storeDef)) { + console.log(`Detected store "${storeDef.$id}"`) + import.meta.hot!.accept( + filePath, + acceptHMRUpdate(storeDef, import.meta.hot) + ) + } + } + }) + } + // const isUseStore = (fn: any): fn is StoreDefinition => { // return typeof fn === 'function' && typeof fn.$id === 'string' // } diff --git a/playground/src/stores/counter.ts b/playground/src/stores/counter.ts index bc1d0c9c..87b408d4 100644 --- a/playground/src/stores/counter.ts +++ b/playground/src/stores/counter.ts @@ -63,6 +63,6 @@ export const useCounter = defineStore({ }, }) -if (import.meta.hot) { - import.meta.hot.accept(acceptHMRUpdate(useCounter, import.meta.hot)) -} +// if (import.meta.hot) { +// import.meta.hot.accept(acceptHMRUpdate(useCounter, import.meta.hot)) +// } diff --git a/src/index.ts b/src/index.ts index b15060c9..b24ebba2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,6 +60,6 @@ export type { export { createTestingPinia } from './testing' export type { TestingOptions, TestingPinia } from './testing' -export { acceptHMRUpdate } from './hmr' +export { acceptHMRUpdate, isUseStore } from './hmr' export { PiniaPlugin } from './vue2-plugin' -- 2.47.2