From bb81f11766f331c1d8f4e711bbd9c7ab639e03cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E7=BA=AF?= Date: Wed, 6 Jan 2021 15:33:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20plugin-layou=E5=AE=9E=E7=8E=B0=E4=BB=8E?= =?UTF-8?q?rootContainer=E6=94=B9=E4=B8=BA=E5=9C=A8=E6=A0=B9=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E6=8F=92=E5=85=A5=E5=85=AC=E5=85=B1=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 好处: 1、布局组件涉及到router-view,需要包裹住router-view,跟rootContainer的默认参数冲突 2、布局组件的内容应该是rootContainer的最底层,但是由于rootContainer的机制,插件循序影响dom结构层级。不能保证布局的内容属于最底层 --- packages/fes-plugin-layout/package.json | 3 + packages/fes-plugin-layout/src/index.js | 22 +++- .../fes-plugin-layout/src/template/index.tpl | 41 +++++++ .../src/template/runtime.tpl | 37 ------ .../src/views/BaseLayout.vue | 96 ++------------- .../src/views/MultiTabProvider.vue | 111 ++++++++++++++++++ .../src/plugins/generateFiles/fes/fes.tpl | 5 +- .../src/plugins/misc/route/index.js | 7 -- .../plugins/misc/route/template/baseView.vue | 22 ---- 9 files changed, 179 insertions(+), 165 deletions(-) create mode 100644 packages/fes-plugin-layout/src/template/index.tpl delete mode 100644 packages/fes-plugin-layout/src/template/runtime.tpl create mode 100644 packages/fes-plugin-layout/src/views/MultiTabProvider.vue delete mode 100644 packages/fes-preset-built-in/src/plugins/misc/route/template/baseView.vue diff --git a/packages/fes-plugin-layout/package.json b/packages/fes-plugin-layout/package.json index 5ae5a579..da450c6b 100644 --- a/packages/fes-plugin-layout/package.json +++ b/packages/fes-plugin-layout/package.json @@ -27,6 +27,9 @@ "publishConfig": { "access": "public" }, + "dependencies": { + "@umijs/utils": "3.3.3" + }, "peerDependencies": { "@ant-design/icons-vue": "^5.1.6", "@webank/fes": "^2.0.0", diff --git a/packages/fes-plugin-layout/src/index.js b/packages/fes-plugin-layout/src/index.js index 0095de4f..38dbfc73 100644 --- a/packages/fes-plugin-layout/src/index.js +++ b/packages/fes-plugin-layout/src/index.js @@ -1,5 +1,6 @@ import { readFileSync, copyFileSync, statSync } from 'fs'; import { join } from 'path'; +import { winPath } from '@umijs/utils'; const namespace = 'plugin-layout'; @@ -18,16 +19,18 @@ export default (api) => { } }); - const absRuntimeFilePath = join(namespace, 'runtime.js'); + api.addRuntimePluginKey(() => 'layout'); + + const absFilePath = join(namespace, 'index.js'); api.onGenerateFiles(() => { // 文件写出 const userConfig = api.config.layout || {}; api.writeTmpFile({ - path: absRuntimeFilePath, + path: absFilePath, content: Mustache.render( - readFileSync(join(__dirname, 'template/runtime.tpl'), 'utf-8'), + readFileSync(join(__dirname, 'template/index.tpl'), 'utf-8'), { REPLACE_USER_CONFIG: JSON.stringify(userConfig), HAS_LOCALE: api.pkg.dependencies?.['@webank/fes-plugin-locale'] @@ -58,7 +61,14 @@ export default (api) => { }); }); - api.addRuntimePluginKey(() => 'layout'); - - api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`); + // 把BaseLayout插入到路由配置中,作为跟路由 + api.modifyRoutes(routes => [ + { + path: '/', + component: winPath( + join(api.paths.absTmpPath || '', absFilePath) + ), + children: routes + } + ]); }; diff --git a/packages/fes-plugin-layout/src/template/index.tpl b/packages/fes-plugin-layout/src/template/index.tpl new file mode 100644 index 00000000..ff67008d --- /dev/null +++ b/packages/fes-plugin-layout/src/template/index.tpl @@ -0,0 +1,41 @@ +import { reactive, defineComponent } from "vue"; +import { getRoutes, plugin, ApplyPluginsType } from "@@/core/coreExports"; +import BaseLayout from "./views/BaseLayout.vue"; +import { fillMenuData } from "./helpers"; + +const userConfig = reactive({{{REPLACE_USER_CONFIG}}}); + +const Layout = defineComponent({ + name: 'Layout', + setup(){ + const runtimeConfig = plugin.applyPlugins({ + key: "layout", + type: ApplyPluginsType.modify, + initialValue: {}, + }); + const localeShared = plugin.getShared("locale"); + const routeConfig = getRoutes(); + userConfig.menus = fillMenuData(userConfig.menus, routeConfig); + return () => { + const slots = { + userCenter: () => { + if (runtimeConfig.userCenter) { + return ( + + ); + } + return null; + }, + locale: () => { + if (localeShared) { + return ; + } + return null; + }, + }; + return ; + }; + } +}) + +export default Layout; diff --git a/packages/fes-plugin-layout/src/template/runtime.tpl b/packages/fes-plugin-layout/src/template/runtime.tpl deleted file mode 100644 index da4f8bdf..00000000 --- a/packages/fes-plugin-layout/src/template/runtime.tpl +++ /dev/null @@ -1,37 +0,0 @@ -import { reactive } from "vue"; -import { getRoutes, plugin, ApplyPluginsType } from "@@/core/coreExports"; -import BaseLayout from "./views/BaseLayout.vue"; -import { fillMenuData } from "./helpers"; - -const userConfig = reactive({{{REPLACE_USER_CONFIG}}}); -export function rootContainer(childComponent, args) { - const runtimeConfig = plugin.applyPlugins({ - key: "layout", - type: ApplyPluginsType.modify, - initialValue: {}, - }); - const localeShared = plugin.getShared("locale"); - const routeConfig = getRoutes(); - userConfig.menus = fillMenuData(userConfig.menus, routeConfig); - return () => { - const slots = { - default: () => , - userCenter: () => { - if(runtimeConfig.userCenter){ - return () - } - return null - }, - locale: () => { - if(localeShared){ - return () - } - return null - } - }; - return ( - - - ); - }; -} \ No newline at end of file diff --git a/packages/fes-plugin-layout/src/views/BaseLayout.vue b/packages/fes-plugin-layout/src/views/BaseLayout.vue index 0dbfd332..40b451c9 100644 --- a/packages/fes-plugin-layout/src/views/BaseLayout.vue +++ b/packages/fes-plugin-layout/src/views/BaseLayout.vue @@ -23,24 +23,8 @@ - - + + Ant Design ©2020 Created by MumbleFe @@ -51,18 +35,14 @@ + diff --git a/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl index 994c16ec..ca548063 100644 --- a/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl +++ b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl @@ -8,7 +8,6 @@ import { plugin } from './core/plugin'; import './core/pluginRegister'; import { ApplyPluginsType } from '{{{ runtimePath }}}'; import { getRoutes } from './core/routes/routes'; -import BaseView from './core/routes/baseView'; {{{ imports }}} @@ -19,9 +18,7 @@ const renderClient = (opts = {}) => { const rootContainer = plugin.applyPlugins({ type: ApplyPluginsType.modify, key: 'rootContainer', - initialValue: defineComponent((props) => { - return () => () - }), + initialValue: defineComponent(() => () => ()), args: { routes: routes, plugin: plugin diff --git a/packages/fes-preset-built-in/src/plugins/misc/route/index.js b/packages/fes-preset-built-in/src/plugins/misc/route/index.js index a632d323..d978fe30 100644 --- a/packages/fes-preset-built-in/src/plugins/misc/route/index.js +++ b/packages/fes-preset-built-in/src/plugins/misc/route/index.js @@ -250,8 +250,6 @@ export default function (api) { const absRuntimeFilePath = join(namespace, 'runtime.js'); - const baseViewFilePath = join(namespace, 'baseView.vue'); - api.onGenerateFiles(async () => { const routesTpl = readFileSync(join(__dirname, 'template/routes.tpl'), 'utf-8'); const routes = await api.getRoutesJSON(); @@ -269,11 +267,6 @@ export default function (api) { path: absRuntimeFilePath, content: readFileSync(join(__dirname, 'template/runtime.tpl'), 'utf-8') }); - - api.writeTmpFile({ - path: baseViewFilePath, - content: readFileSync(join(__dirname, 'template/baseView.vue'), 'utf-8') - }); }); api.addCoreExports(() => [ diff --git a/packages/fes-preset-built-in/src/plugins/misc/route/template/baseView.vue b/packages/fes-preset-built-in/src/plugins/misc/route/template/baseView.vue deleted file mode 100644 index 7073fc6a..00000000 --- a/packages/fes-preset-built-in/src/plugins/misc/route/template/baseView.vue +++ /dev/null @@ -1,22 +0,0 @@ - -