vuex使用webpack中的require.context(’./modules’, true, /.js$/) 获取目录下的所有文件,升级vue3使用vite报 require is not defined
import { createStore } from 'vuex'
import getters from './getters'
// https://vitejs.dev/guide/features.html#glob-import
const modulesFiles = import.meta.globEager('./modules/*.js')
let modules = {}
for (const path in modulesFiles) {
const moduleName = path.replace(/(.*\/)*([^.]+).*/gi, '$2')
modules = { ...modules, ...modulesFiles[path] }
}
const store = new createStore({
modules,
getters,
})
export default store