mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-01 09:57:14 +00:00
48 lines
1.0 KiB
JavaScript
48 lines
1.0 KiB
JavaScript
import Vue from 'vue'
|
|
import Vuex from 'vuex'
|
|
Vue.use(Vuex)
|
|
|
|
let files
|
|
let filenames = []
|
|
|
|
try {
|
|
files = require.context('~store', false, /^\.\/.*\.js$/)
|
|
filenames = files.keys()
|
|
} catch (e) {
|
|
console.warn('Nuxt.js store:', e.message)
|
|
}
|
|
|
|
function getModule (filename) {
|
|
let file = files(filename)
|
|
return file.default
|
|
? file.default
|
|
: file
|
|
}
|
|
|
|
let store
|
|
let storeData = {}
|
|
|
|
// Check if store/index.js returns a vuex store
|
|
if (filenames.indexOf('./index.js') !== -1) {
|
|
let mainModule = getModule('./index.js')
|
|
if (mainModule.commit) {
|
|
store = mainModule
|
|
} else {
|
|
storeData = mainModule
|
|
}
|
|
}
|
|
|
|
// Generate the store if there is no store yet
|
|
if (store == null) {
|
|
storeData.modules = storeData.modules || {}
|
|
for (let filename of filenames) {
|
|
let name = filename.replace(/^\.\//, '').replace(/\.js$/, '')
|
|
if (name === 'index') continue
|
|
storeData.modules[name] = getModule(filename)
|
|
storeData.modules[name].namespaced = true
|
|
}
|
|
store = new Vuex.Store(storeData)
|
|
}
|
|
|
|
export default store
|