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}
|
2018-03-01 06:08:42 +00:00
|
|
|
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
|
2017-07-04 13:41:43 +00:00
|
|
|
let indexFilename
|
|
|
|
filenames.forEach((filename) => {
|
|
|
|
if (filename.indexOf('./index.') !== -1) {
|
|
|
|
indexFilename = filename
|
|
|
|
}
|
|
|
|
})
|
2017-05-20 09:36:35 +00:00
|
|
|
if (indexFilename) {
|
2017-05-21 13:45:21 +00:00
|
|
|
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
|
|
|
|
2018-08-10 23:24:53 +00:00
|
|
|
for (const filename of filenames) {
|
2018-01-04 17:10:17 +00:00
|
|
|
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
|
|
|
|
2018-08-10 23:24:53 +00:00
|
|
|
const namePath = name.split(/\//)
|
2017-05-21 00:03:32 +00:00
|
|
|
|
2018-08-10 23:24:53 +00:00
|
|
|
name = namePath[namePath.length - 1]
|
2018-01-31 18:07:26 +00:00
|
|
|
if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') {
|
2018-08-10 23:24:53 +00:00
|
|
|
const module = getModuleNamespace(storeData, namePath, true)
|
2018-01-31 18:07:26 +00:00
|
|
|
appendModule(module, filename, name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:24:53 +00:00
|
|
|
// 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-08-10 23:24:53 +00:00
|
|
|
}
|
2018-01-31 20:22:02 +00:00
|
|
|
|
2018-08-10 23:24:53 +00:00
|
|
|
const module = getModuleNamespace(storeData, namePath)
|
|
|
|
const fileModule = getModule(filename)
|
2017-05-21 00:03:32 +00:00
|
|
|
name = namePath.pop()
|
2018-01-31 18:07:26 +00:00
|
|
|
module[name] = module[name] || {}
|
2018-02-06 16:13:28 +00:00
|
|
|
|
2018-08-10 23:24:53 +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
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:24:53 +00:00
|
|
|
// 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
|
2018-08-20 12:19:09 +00:00
|
|
|
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
|
2017-05-13 17:52:04 +00:00
|
|
|
export const createStore = storeData instanceof Function ? storeData : () => {
|
2017-08-13 20:19:37 +00:00
|
|
|
return new Vuex.Store(Object.assign({
|
2018-08-10 23:24:53 +00:00
|
|
|
strict: (process.env.NODE_ENV !== 'production')
|
2017-08-13 20:19:37 +00:00
|
|
|
}, storeData, {
|
2017-05-21 13:45:21 +00:00
|
|
|
state: storeData.state instanceof Function ? storeData.state() : {}
|
2017-05-13 18:52:37 +00:00
|
|
|
}))
|
2017-05-13 17:45:42 +00:00
|
|
|
}
|
2017-05-13 17:32:11 +00:00
|
|
|
|
|
|
|
// Dynamically require module
|
2017-05-21 13:45:21 +00:00
|
|
|
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.')
|
2017-05-21 13:45:21 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-31 18:07:26 +00:00
|
|
|
function getModuleNamespace (storeData, namePath, forAppend = false) {
|
2017-03-28 14:28:24 +00:00
|
|
|
if (namePath.length === 1) {
|
2018-08-10 23:24:53 +00:00
|
|
|
if (forAppend) { return storeData }
|
2017-03-28 14:28:24 +00:00
|
|
|
return storeData.modules
|
|
|
|
}
|
2018-08-10 23:24:53 +00:00
|
|
|
const namespace = namePath.shift()
|
2017-03-25 20:21:11 +00:00
|
|
|
storeData.modules[namespace] = storeData.modules[namespace] || {}
|
|
|
|
storeData.modules[namespace].namespaced = true
|
|
|
|
storeData.modules[namespace].modules = storeData.modules[namespace].modules || {}
|
2018-01-31 18:07:26 +00:00
|
|
|
return getModuleNamespace(storeData.modules[namespace], namePath, forAppend)
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:24:53 +00:00
|
|
|
function appendModule (module, filename, name) {
|
2018-01-31 18:07:26 +00:00
|
|
|
const file = files(filename)
|
2018-02-06 16:13:28 +00:00
|
|
|
module.appends = module.appends || []
|
|
|
|
module.appends.push(name)
|
2018-01-31 18:07:26 +00:00
|
|
|
module[name] = file.default || file
|
2017-03-25 20:21:11 +00:00
|
|
|
}
|