championswimmer / vuex-module-decorators

TypeScript/ES7 Decorators to create Vuex modules declaratively
https://championswimmer.in/vuex-module-decorators/
MIT License
1.8k stars 170 forks source link

Can vite hot update be supported? #394

Open CaoMeiYouRen opened 2 years ago

CaoMeiYouRen commented 2 years ago

For example

// src/module/index.ts 
function registerDynamicModule<S>(dynamicModule: Mod<S, any>, modOpt: DynamicModuleOptions) {
  if (!modOpt.name) {
    throw new Error('Name of module not provided in decorator options')
  }

  if (!modOpt.store) {
    throw new Error('Store not provided in decorator options when using dynamic option')
  }
////
  if (import.meta.hot) {
    // Hot update of vite. 
    if (modOpt.store.hasModule(modOpt.name)) {
      // Hot update if duplicate modules are encountered. 
      modOpt.store.hotUpdate({
        modules: {
          [modOpt.name]: dynamicModule
        }
      })
      return
    }
  }
////
  modOpt.store.registerModule(modOpt.name, dynamicModule, {
    preserveState: modOpt.preserveState || false
  })
}