Nuxt/lib/app/store.js

117 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-12-25 20:16:30 +00:00
import Vue from 'vue'
import Vuex from 'vuex'
2017-05-13 17:32:11 +00:00
2016-12-25 20:16:30 +00:00
Vue.use(Vuex)
2018-02-04 09:31:03 +00:00
// Recursive find files in {srcDir}/{dir.store}
const files = require.context('@/<%= dir.store %>', true, /^\.\/(?!<%= ignorePrefix %>)[^.]+\.(<%= extensions %>)$/)
2017-05-13 17:32:11 +00:00
const filenames = files.keys()
// Store
let storeData = {}
2018-02-04 09:31:03 +00:00
// Check if {dir.store}/index.js exists
let indexFilename
filenames.forEach((filename) => {
if (filename.indexOf('./index.') !== -1) {
indexFilename = filename
}
})
2017-05-20 09:36:35 +00:00
if (indexFilename) {
storeData = getModule(indexFilename)
2017-05-13 17:32:11 +00:00
}
2017-05-21 00:03:32 +00:00
// If store is not an exported method = modules store
if (typeof storeData !== 'function') {
// Store modules
if (!storeData.modules) {
storeData.modules = {}
}
2017-01-02 09:13:53 +00:00
for (const filename of filenames) {
let name = filename.replace(/^\.\//, '').replace(/\.(<%= extensions %>)$/, '')
2017-05-21 00:03:32 +00:00
if (name === 'index') continue
2017-05-13 17:32:11 +00:00
const namePath = name.split(/\//)
2017-05-21 00:03:32 +00:00
name = namePath[namePath.length - 1]
if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') {
const module = getModuleNamespace(storeData, namePath, true)
appendModule(module, filename, name)
continue
}
// if file is foo/index.js
// it should save as foo
const isIndex = (name === 'index')
if (isIndex) {
2018-01-31 20:22:02 +00:00
namePath.pop()
}
2018-01-31 20:22:02 +00:00
const module = getModuleNamespace(storeData, namePath)
const fileModule = getModule(filename)
2017-05-21 00:03:32 +00:00
name = namePath.pop()
module[name] = module[name] || {}
2018-02-06 16:13:28 +00:00
// if file is foo.js, existing properties take priority
// because it's the least specific case
2018-02-06 16:13:28 +00:00
if (!isIndex) {
module[name] = Object.assign({}, fileModule, module[name])
module[name].namespaced = true
continue
}
// if file is foo/index.js we want to overwrite properties from foo.js
// but not from appended mods like foo/actions.js
const appendedMods = {}
2018-02-06 16:13:28 +00:00
if (module[name].appends) {
2018-02-06 16:47:55 +00:00
appendedMods.appends = module[name].appends
for (const append of module[name].appends) { appendedMods[append] = module[name][append] }
2018-02-06 16:13:28 +00:00
}
2018-02-06 16:47:55 +00:00
module[name] = Object.assign({}, module[name], fileModule, appendedMods)
2017-05-21 00:03:32 +00:00
module[name].namespaced = true
}
2017-05-13 17:32:11 +00:00
}
// createStore
export const createStore = storeData instanceof Function ? storeData : () => {
return new Vuex.Store(Object.assign({
strict: (process.env.NODE_ENV !== 'production')
}, storeData, {
state: storeData.state instanceof Function ? storeData.state() : {}
}))
2017-05-13 17:45:42 +00:00
}
2017-05-13 17:32:11 +00:00
// Dynamically require module
function getModule (filename) {
2017-05-13 17:32:11 +00:00
const file = files(filename)
2017-05-13 17:45:42 +00:00
const module = file.default || file
2017-05-13 17:32:11 +00:00
if (module.commit) {
2018-02-04 09:31:03 +00:00
throw new Error('[nuxt] <%= dir.store %>/' + filename.replace('./', '') + ' should export a method which returns a Vuex instance.')
}
if (module.state && typeof module.state !== 'function') {
2018-02-04 09:31:03 +00:00
throw new Error('[nuxt] state should be a function in <%= dir.store %>/' + filename.replace('./', ''))
2017-05-13 17:32:11 +00:00
}
return module
2016-12-26 16:19:10 +00:00
}
function getModuleNamespace (storeData, namePath, forAppend = false) {
2017-03-28 14:28:24 +00:00
if (namePath.length === 1) {
if (forAppend) { return storeData }
2017-03-28 14:28:24 +00:00
return storeData.modules
}
const namespace = namePath.shift()
storeData.modules[namespace] = storeData.modules[namespace] || {}
storeData.modules[namespace].namespaced = true
storeData.modules[namespace].modules = storeData.modules[namespace].modules || {}
return getModuleNamespace(storeData.modules[namespace], namePath, forAppend)
}
function appendModule (module, filename, name) {
const file = files(filename)
2018-02-06 16:13:28 +00:00
module.appends = module.appends || []
module.appends.push(name)
module[name] = file.default || file
}