From e2d5d2fa617b5ed807bb387e0236ff9a09aadde7 Mon Sep 17 00:00:00 2001 From: Brendan Date: Mon, 26 Dec 2016 17:19:10 +0100 Subject: [PATCH] Accept both ways of creating the store --- lib/app/store.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/app/store.js b/lib/app/store.js index dc8e030b0f..3cdf3ccc84 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -3,16 +3,36 @@ import Vuex from 'vuex' Vue.use(Vuex) const files = require.context('~store', false, /^\.\/.*\.js$/) +function getModule (filename) { + let file = files(filename) + return file.default + ? file.default + : file +} -const storeData = { modules: {} } -for (let filename of files.keys()) { - let name = filename.replace(/^\.\//, '').replace(/\.js$/, '') - if (name === 'index') { - Object.assign(storeData, files(filename)) +let store +let storeData = {} + +// Check if store/index.js returns a vuex store +if (files.keys().includes('./index.js')) { + let mainModule = getModule('./index.js') + if (mainModule.commit) { + store = mainModule } else { - storeData.modules[name] = files(filename) - storeData.modules[name].namespaced = true + storeData = mainModule } } -export default new Vuex.Store(storeData) +// Generate the store if there is no store yet +if (store == null) { + storeData.modules = storeData.modules || {} + for (let filename of files.keys()) { + 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