cmc-web/packages/common/utils/autoImport.ts

31 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-08-20 12:11:31 +00:00
// 同步加载
export function getModules(files: any) {
// 当files为方法时为webapock,为对象时为vite
const isWebpack = typeof files === 'function'
// 路径包含compoennts的是内置组件不向外暴露
2024-08-21 01:17:14 +00:00
const modules = (isWebpack ? files.keys() : Object.keys(files))
.filter((item: string) => !item.includes('components'))
.reduce((modules: any, modulePath: string) => {
// set './app.vue' => 'app'
const moduleName = modulePath.replace(/^\.\S*\/(.*)\.\w+$/, '$1')
const value = isWebpack ? files(modulePath) : files[modulePath]
modules[moduleName] = value.default
return modules
}, {})
return modules
2024-08-20 12:11:31 +00:00
}
// 异步加载
export function getAsyncModules(files: any, asyncImport: any) {
// 当files为方法时为webapock,为对象时为vite
const isWebpack = typeof files === 'function'
2024-08-21 01:17:14 +00:00
const modules = (isWebpack ? files.keys() : Object.keys(files))
.filter((item: string) => !item.includes('components'))
.reduce((modules: any, modulePath: string) => {
// set './app.vue' => 'app'
const moduleName = modulePath.replace(/^\.\S*\/(.*)\.\w+$/, '$1')
modules[moduleName] = isWebpack ? asyncImport(modulePath) : files[moduleName]
return modules
}, {})
return modules
2024-08-20 12:11:31 +00:00
}