From f3cf8360c264b611051012826fbbb70c492c9d20 Mon Sep 17 00:00:00 2001 From: aixianling Date: Tue, 4 Apr 2023 09:19:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B0=83=E6=95=B4web?= =?UTF-8?q?=E7=AB=AF=E5=86=85=E5=AD=98=E6=BA=A2=E5=87=BA=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + bin/mods.js | 33 +++++++++++++++++++++++ examples/router/autoRoutes.js | 51 +++++++++++++++++++++++------------ examples/router/axios.js | 4 +-- examples/views/appEntry.vue | 5 ++-- vue.config.js | 10 +++---- 6 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 bin/mods.js diff --git a/.gitignore b/.gitignore index f1193cd0..56ac5b42 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ yarn-error.log* /project/*/index.js /project/*/dist /ui/package-lock.json +/examples/modules.json diff --git a/bin/mods.js b/bin/mods.js new file mode 100644 index 00000000..d7d7b35f --- /dev/null +++ b/bin/mods.js @@ -0,0 +1,33 @@ +const {chalkTag, findApp, fs, fsExtra} = require("./tools"); +const compiler = require('vue-template-compiler') +const getAppInfo = (file, apps) => { + if (/[\\\/](App[A-Z][^\\\/]+)\.vue$/g.test(file)) { + const name = file.replace(/.+[\\\/](App[^\\\/]+)\.vue$/, '$1'), + source = fs.readFileSync(file).toString(), + parsed = compiler.parseComponent(source), + script = parsed.script?.content || "", + label = script.match(/label:[^,]+/)?.[0]?.replace(/.+["']([^"']+).+/, '$1') + const paths = file.split(/[\\\/]/) + apps.push({ + id: file.replace(/\.vue$/, '').replace(/[\\\/]/g, '_'), + label: label || name, + path: `/${file.replace(/\.vue$/, '').replace(/[\\\/]/g, '/')}`, + workspace: paths.at(0), + esm: ['.', paths.slice(1)].flat().join("/"), + name + }) + } +} + +const start = () => { + chalkTag.info("开始扫描库工程...") + const list = [] + Promise.all([ + findApp('packages', app => getAppInfo(app, list)), + findApp('project', app => getAppInfo(app, list)), + ]).then(() => { + fsExtra.outputJson('examples/modules.json', {apps: list}) + chalkTag.done("扫描完毕") + }) +} +start() diff --git a/examples/router/autoRoutes.js b/examples/router/autoRoutes.js index fa0f4f7a..65587230 100644 --- a/examples/router/autoRoutes.js +++ b/examples/router/autoRoutes.js @@ -2,6 +2,8 @@ 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, @@ -9,32 +11,47 @@ export default { //约束正则式 store.commit("cleanApps") // 自动化本工程应用 - this.loadApps() + 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的自动化格式 - let apps = require.context('../../packages/', true, /\.(\/.+)\/App[A-Z][^\/]+\.vue$/, 'lazy-once'), - projects = require.context('../../project/', true, /\.(\/.+)\/App[A-Z][^\/]+\.vue$/, 'lazy-once') const promise = (mods, base) => Promise.all(mods.keys().map(path => mods(path).then(file => { if (file.default) { - let {name, label} = file.default, - addApp = { - name: [base, path.replace(/\.\/?(vue)?/g, '')?.split("/")].flat().join("_"), - label: label || name, - path: `/${base}${path.replace(/\.(\/.+\/App.+)\.vue$/, '$1')}`, - component: appEntry, - module: 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 - }))) - waiting.init({innerHTML: '应用加载中..'}) - Promise.all([ - promise(apps, "packages"), - promise(projects, "project") - ]).finally(() => waiting.close()) + }).catch(err => console.log(err)))) + return Promise.all([ + promise(this.esm.packages, "packages"), + promise(this.esm.project, "project") + ]) } } diff --git a/examples/router/axios.js b/examples/router/axios.js index 19fdc89c..ccf785cb 100644 --- a/examples/router/axios.js +++ b/examples/router/axios.js @@ -21,8 +21,8 @@ instance.interceptors.request.use(config => { config.baseURL = "/qxn" } else if (/\/xiushan/.test(location.pathname)) { config.baseURL = "/xsjr" - } else if (/project\/oms/.test(location.pathname)) { - config.baseURL = "/omsapi" + // } else if (/project\/oms/.test(location.pathname)) { + // config.baseURL = "/omsapi" } else if (/#url-/.test(location.hash)) { config.baseURL = location.hash.replace(/#url-/, '/') } diff --git a/examples/views/appEntry.vue b/examples/views/appEntry.vue index fb0ce5dc..da4f5dee 100644 --- a/examples/views/appEntry.vue +++ b/examples/views/appEntry.vue @@ -8,6 +8,7 @@