fix: hotfix for vuex hmr (#4801)

* hotfix: Vuex HMR and warning when exporting an object for state

* hotfix: Handle sub modules too

* Update store.js
This commit is contained in:
Sébastien Chopin 2019-01-21 11:42:06 +01:00 committed by GitHub
parent 70775e121a
commit 5f0b34fcd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ import Vuex from 'vuex'
Vue.use(Vuex) Vue.use(Vuex)
const log = console // on server-side, consola will catch all console.log
const VUEX_PROPERTIES = ['state', 'getters', 'actions', 'mutations'] const VUEX_PROPERTIES = ['state', 'getters', 'actions', 'mutations']
let store = {} let store = {}
let fileResolver let fileResolver
@ -31,7 +32,6 @@ void (function updateModules() {
// If store is an exported method = classic mode (deprecated) // If store is an exported method = classic mode (deprecated)
if (typeof store === 'function') { if (typeof store === 'function') {
const log = (process.server ? require('consola') : console)
return log.warn('Classic mode for store/ is deprecated and will be removed in Nuxt 3.') return log.warn('Classic mode for store/ is deprecated and will be removed in Nuxt 3.')
} }
@ -97,19 +97,26 @@ export const createStore = store instanceof Function ? store : () => {
// Dynamically require module // Dynamically require module
function requireModule(path, { isRoot = false, isState = false } = {}) { function requireModule(path, { isRoot = false, isState = false } = {}) {
const file = fileResolver(path) const file = fileResolver(path)
const moduleData = file.default || file let moduleData = file.default || file
if (isState && typeof moduleData !== 'function') { if (isState && typeof moduleData !== 'function') {
return () => moduleData log.warn(`${path} should export a method that returns an object`)
const state = Object.assign({}, moduleData)
return () => state
} }
if (isRoot && moduleData.commit) { if (isRoot && moduleData.commit) {
throw new Error('[nuxt] <%= dir.store %>/' + path.replace('./', '') + ' should export a method which returns a Vuex instance.') throw new Error('[nuxt] <%= dir.store %>/' + path.replace('./', '') + ' should export a method that returns a Vuex instance.')
} }
if (isRoot && typeof moduleData !== 'function') { if (isRoot && typeof moduleData !== 'function') {
// Avoid TypeError: setting a property that has only a getter when overwriting top level keys // Avoid TypeError: setting a property that has only a getter when overwriting top level keys
const state = moduleData.state && typeof moduleData.state !== 'function' ? (() => state) : moduleData.state moduleData = Object.assign({}, moduleData)
return Object.assign({}, moduleData, { state }) }
if (moduleData.state && typeof moduleData.state !== 'function') {
log.warn(`'state' should be a method that returns an object in ${path}`)
const state = Object.assign({}, moduleData.state)
// Avoid TypeError: setting a property that has only a getter when overwriting top level keys
moduleData = Object.assign({}, moduleData, { state: () => state })
} }
return moduleData return moduleData
} }