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
const indexFilename = filenames.find((filename) => filename.includes('./index.'))
if (indexFilename) {
storeData = getModule(indexFilename)
storeData = getModule(indexFilename, true)
}
// Store modules
if (!storeData.modules) {
storeData.modules = {}
}
// If store is not an exported method = modules store
if (typeof storeData !== 'function') {
for (let filename of filenames) {
let name = filename.replace(/^\.\//, '').replace(/\.(js|ts)$/, '')
if (name === 'index') continue
// Store modules
if (!storeData.modules) {
storeData.modules = {}
}
let namePath = name.split(/\//)
let module = getModuleNamespace(storeData, namePath)
for (let filename of filenames) {
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
export const createStore = storeData instanceof Function ? 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
function getModule (filename) {
function getModule (filename, root) {
const file = files(filename)
const module = file.default || file
if (module.state && typeof module.state !== 'function') {
throw new Error('[nuxt] store state should be a function.')
}
// if (root && module.state && typeof module.state !== 'function') {
// throw new Error('[nuxt] root store state should be a function.')
// }
if (module.commit) {
throw new Error('[nuxt] store should export raw store options instead of an instance.')
}