mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
refactor: client store code style (#3683)
This commit is contained in:
parent
bc1cecf4a6
commit
054ea7931a
@ -3,7 +3,6 @@ import Vuex from 'vuex'
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
// Recursive find files in {srcDir}/{dir.store}
|
||||
const files = require.context('@/<%= dir.store %>', true, /^\.\/(?!<%= ignorePrefix %>)[^.]+\.(<%= extensions %>)$/)
|
||||
const filenames = files.keys()
|
||||
|
||||
@ -11,12 +10,8 @@ const filenames = files.keys()
|
||||
let storeData = {}
|
||||
|
||||
// Check if {dir.store}/index.js exists
|
||||
let indexFilename
|
||||
filenames.forEach((filename) => {
|
||||
if (filename.indexOf('./index.') !== -1) {
|
||||
indexFilename = filename
|
||||
}
|
||||
})
|
||||
const indexFilename = filenames.find(name => name.includes('./index.'))
|
||||
|
||||
if (indexFilename) {
|
||||
storeData = getModule(indexFilename)
|
||||
}
|
||||
@ -35,14 +30,13 @@ if (typeof storeData !== 'function') {
|
||||
const namePath = name.split(/\//)
|
||||
|
||||
name = namePath[namePath.length - 1]
|
||||
if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') {
|
||||
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 save as foo
|
||||
// If file is foo/index.js, it should be saved as foo
|
||||
const isIndex = (name === 'index')
|
||||
if (isIndex) {
|
||||
namePath.pop()
|
||||
@ -50,6 +44,7 @@ if (typeof storeData !== 'function') {
|
||||
|
||||
const module = getModuleNamespace(storeData, namePath)
|
||||
const fileModule = getModule(filename)
|
||||
|
||||
name = namePath.pop()
|
||||
module[name] = module[name] || {}
|
||||
|
||||
@ -100,7 +95,9 @@ function getModule (filename) {
|
||||
|
||||
function getModuleNamespace (storeData, namePath, forAppend = false) {
|
||||
if (namePath.length === 1) {
|
||||
if (forAppend) { return storeData }
|
||||
if (forAppend) {
|
||||
return storeData
|
||||
}
|
||||
return storeData.modules
|
||||
}
|
||||
const namespace = namePath.shift()
|
||||
|
Loading…
Reference in New Issue
Block a user