對於webpack來說,可以使用require.context方法來實現文件的批量導出,但是vite搭建vue3項目時,不支持require,對於這種情況可以使用import.meta.glob或者import.meta.globEager來實現
二者使用方法相似,只是引入時機不同,globEager時立即引入,glob是異步引入
globEager
const directives = import.meta.globEager('./*/index.js')
for (let com in directives) {
const comKey = com.match(/\.\/(.*?)\/index\.js/)[1]
const comValue = directives[com].default
app.directive(comKey, comValue)
}

glob
const directives = import.meta.glob('./*/index.js')
for (let dire in directives) {
const direKey = dire.match(/\.\/(.*?)\/index\.js/)[1]
directives[dire]().then((res) => {
const direValue = res.default
app.directive(direKey, direValue)
})
}

