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