58 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			58 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import store from "../store";
 | |
| import {waiting} from "../utils";
 | |
| import appEntry from "../views/appEntry";
 | |
| import router from "./router";
 | |
| import mods from "../modules"
 | |
| import Vue from "vue";
 | |
| 
 | |
| export default {
 | |
|   routes: () => store.state.apps,
 | |
|   init() {
 | |
|     //约束正则式
 | |
|     store.commit("cleanApps")
 | |
|     // 自动化本工程应用
 | |
|     waiting.init({innerHTML: '应用加载中..'})
 | |
|     this.esm = {
 | |
|       packages: require.context('../../packages/', true, /\.(\/.+)\/App[A-Z][^\/]+\.vue$/, 'lazy'),
 | |
|       project: require.context('../../project/', true, /\.(\/.+)\/App[A-Z][^\/]+\.vue$/, 'lazy')
 | |
|     }
 | |
|     const startTime = new Date().getTime()
 | |
|     this.loadApps().finally(() => {
 | |
|       console.log('模块加载用了%s秒', (new Date().getTime() - startTime) / 1000)
 | |
|       waiting.close()
 | |
|     })
 | |
|   },
 | |
|   loadMods() {
 | |
|     return Promise.all(mods.apps.map(e => {
 | |
|       Vue.component(e.name, this.esm[e.workspace](e.esm))
 | |
|       const addApp = {...e, component: appEntry}
 | |
|       waiting.setContent(`加载${e.name}...`)
 | |
|       //命名规范入口文件必须以App开头
 | |
|       return store.commit("addApp", addApp)
 | |
|     }))
 | |
|   },
 | |
|   loadApps() {
 | |
|     //新App的自动化格式
 | |
|     const promise = (mods, base) => Promise.all(mods.keys().map(path => mods(path).then(file => {
 | |
|       if (file.default) {
 | |
|         const {name, label} = file.default
 | |
|         const addApp = {
 | |
|           name: [base, path.replace(/\.\/?(vue)?/g, '')?.split("/")].flat().join("_"),
 | |
|           label: label || name,
 | |
|           path: `/${base}${path.replace(/\.(\/.+\/App.+)\.vue$/, '$1')}`,
 | |
|           component: appEntry,
 | |
|           esm: file.default
 | |
|         }
 | |
|         waiting.setContent(`加载${name}...`)
 | |
|         router.addRoute(addApp)
 | |
|         //命名规范入口文件必须以App开头
 | |
|         return store.commit("addApp", addApp)
 | |
|       } else return 0
 | |
|     }).catch(err => console.log(err))))
 | |
|     return Promise.all([
 | |
|       promise(this.esm.packages, "packages"),
 | |
|       promise(this.esm.project, "project")
 | |
|     ])
 | |
|   }
 | |
| }
 |