Fix store module vs store

This commit is contained in:
Sébastien Chopin 2017-05-21 02:03:32 +02:00
parent e95c0a6573
commit 01de17b827
1 changed files with 23 additions and 18 deletions

View File

@ -13,40 +13,45 @@ let storeData = {}
// Check if store/index.js exists // Check if store/index.js exists
const indexFilename = filenames.find((filename) => filename.includes('./index.')) const indexFilename = filenames.find((filename) => filename.includes('./index.'))
if (indexFilename) { if (indexFilename) {
storeData = getModule(indexFilename) storeData = getModule(indexFilename, true)
} }
// Store modules // If store is not an exported method = modules store
if (!storeData.modules) { if (typeof storeData !== 'function') {
storeData.modules = {}
}
for (let filename of filenames) { // Store modules
let name = filename.replace(/^\.\//, '').replace(/\.(js|ts)$/, '') if (!storeData.modules) {
if (name === 'index') continue storeData.modules = {}
}
let namePath = name.split(/\//) for (let filename of filenames) {
let module = getModuleNamespace(storeData, namePath) let name = filename.replace(/^\.\//, '').replace(/\.(js|ts)$/, '')
if (name === 'index') continue
let namePath = name.split(/\//)
let module = getModuleNamespace(storeData, namePath)
name = namePath.pop()
module[name] = getModule(filename)
module[name].namespaced = true
}
name = namePath.pop()
module[name] = getModule(filename)
module[name].namespaced = true
} }
// createStore // createStore
export const createStore = storeData instanceof Function ? storeData : () => { export const createStore = storeData instanceof Function ? storeData : () => {
return new Vuex.Store(Object.assign({}, storeData, { return new Vuex.Store(Object.assign({}, storeData, {
state: storeData.state instanceof Function ? storeData.state() : {} // state: storeData.state instanceof Function ? storeData.state() : {}
})) }))
} }
// Dynamically require module // Dynamically require module
function getModule (filename) { function getModule (filename, root) {
const file = files(filename) const file = files(filename)
const module = file.default || file const module = file.default || file
if (module.state && typeof module.state !== 'function') { // if (root && module.state && typeof module.state !== 'function') {
throw new Error('[nuxt] store state should be a function.') // throw new Error('[nuxt] root store state should be a function.')
} // }
if (module.commit) { if (module.commit) {
throw new Error('[nuxt] store should export raw store options instead of an instance.') throw new Error('[nuxt] store should export raw store options instead of an instance.')
} }