Nuxt/packages/vue-app/template/store.js

133 lines
4.2 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)
2017-05-13 17:32:11 +00:00
let storeData = {}
2019-01-06 07:56:59 +00:00
let files
2019-01-06 07:56:59 +00:00
void (function updateModules() {
files = require.context('@/<%= dir.store %>', true, /^\.\/(?!<%= ignorePrefix %>)[^.]+\.(<%= extensions %>)$/)
const filenames = files.keys()
2017-05-13 17:32:11 +00:00
// Check if {dir.store}/index.js exists
const indexFilename = filenames.find(filename => filename.includes('./index.'))
if (indexFilename) {
storeData = getModule(indexFilename)
2017-05-21 00:03:32 +00:00
}
2017-01-02 09:13:53 +00:00
// If store is not an exported method = modules store
if (typeof storeData !== 'function') {
// Store modules
if (!storeData.modules) {
storeData.modules = {}
}
for (const filename of filenames) {
let name = filename.replace(/^\.\//, '').replace(/\.(<%= extensions %>)$/, '')
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 (['state', 'getters', 'actions', 'mutations'].includes(name)) {
const module = getModuleNamespace(storeData, namePath, true)
appendModule(module, filename, name)
continue
}
// If file is foo/index.js, it should be saved as foo
const isIndex = (name === 'index')
if (isIndex) {
namePath.pop()
}
2018-01-31 20:22:02 +00:00
const module = getModuleNamespace(storeData, namePath)
const fileModule = getModule(filename)
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
if (!isIndex) {
module[name] = Object.assign({}, fileModule, module[name])
module[name].namespaced = true
continue
}
2018-02-06 16:13:28 +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 = {}
if (module[name].appends) {
appendedMods.appends = module[name].appends
for (const append of module[name].appends) {
appendedMods[append] = module[name][append]
}
}
2018-02-06 16:47:55 +00:00
module[name] = Object.assign({}, module[name], fileModule, appendedMods)
module[name].namespaced = true
}
// If the environment supports hot reloading...
<% if (isDev) { %>
if (process.client && module.hot) {
// Whenever any Vuex module is updated...
module.hot.accept(files.id, () => {
// Update `root.modules` with the latest definitions.
updateModules()
// Trigger a hot update in the store.
window.<%= globals.nuxt %>.$store.hotUpdate(storeData)
})
}<% } %>
2019-01-06 07:56:59 +00:00
} else {
const log = (process.server ? require('consola') : console)
log.warn('Classic mode for store/ is deprecated and will be removed in Nuxt 3.')
}
2019-01-06 07:56:59 +00:00
})()
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
2018-10-24 13:46:06 +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.')
}
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-10-24 13:46:06 +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)
}
2018-10-24 13:46:06 +00:00
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
}