Skip to content

Commit

Permalink
feat: dynamic router & store
Browse files Browse the repository at this point in the history
feat: dynamic  router & store
  • Loading branch information
Faizal Andyka committed Mar 9, 2021
1 parent 3293de7 commit be101c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
15 changes: 7 additions & 8 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/**
* TODO:
* dynamically read file inside modules
* using require.context. https://github.com/vitejs/vite/issues/77
*/

import {
createRouter,
createWebHashHistory
} from 'vue-router'

import home from '@@/Home/router'
const domainRouterFiles = import.meta.globEager('../src/modules/**/router.ts')
const domainRoutes: any = []

for (const path in domainRouterFiles) {
domainRoutes.push(...domainRouterFiles[path].default)
}

const router = createRouter({
history: createWebHashHistory(),
Expand All @@ -20,7 +19,7 @@ const router = createRouter({
component: () => import('./views/notFound.vue')
},

...home,
...domainRoutes,
]
})

Expand Down
22 changes: 14 additions & 8 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/**
* TODO:
* dynamically read file inside modules
* using require.context. https://github.com/vitejs/vite/issues/77
*/

import { createStore } from 'vuex'
import globalStores from './stores'

const domainStoreFiles = import.meta.globEager('../src/modules/**/store.ts')
let domainStores = {}

for (const path in domainStoreFiles) {
for (const key in domainStoreFiles[path]) {
domainStores[key] = domainStoreFiles[path][key]
}
}

const store = createStore({
strict: import.meta.env.VITE_APP_ENV !== 'production',
modules: { ...globalStores }
modules: {
...globalStores,
...domainStores
}
})

export default store
export default store

0 comments on commit be101c1

Please sign in to comment.