store.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. let store = {};
  5. (function updateModules () {
  6. store = normalizeRoot(require('../store/index.js'), 'store/index.js')
  7. // If store is an exported method = classic mode (deprecated)
  8. // Enforce store modules
  9. store.modules = store.modules || {}
  10. // If the environment supports hot reloading...
  11. })()
  12. // createStore
  13. export const createStore = store instanceof Function ? store : () => {
  14. return new Vuex.Store(Object.assign({
  15. strict: (process.env.NODE_ENV !== 'production')
  16. }, store))
  17. }
  18. function normalizeRoot (moduleData, filePath) {
  19. moduleData = moduleData.default || moduleData
  20. if (moduleData.commit) {
  21. throw new Error(`[nuxt] ${filePath} should export a method that returns a Vuex instance.`)
  22. }
  23. if (typeof moduleData !== 'function') {
  24. // Avoid TypeError: setting a property that has only a getter when overwriting top level keys
  25. moduleData = Object.assign({}, moduleData)
  26. }
  27. return normalizeModule(moduleData, filePath)
  28. }
  29. function normalizeModule (moduleData, filePath) {
  30. if (moduleData.state && typeof moduleData.state !== 'function') {
  31. console.warn(`'state' should be a method that returns an object in ${filePath}`)
  32. const state = Object.assign({}, moduleData.state)
  33. // Avoid TypeError: setting a property that has only a getter when overwriting top level keys
  34. moduleData = Object.assign({}, moduleData, { state: () => state })
  35. }
  36. return moduleData
  37. }