modular-vue
Version:
modular vue support
22 lines • 3.44 kB
JavaScript
/*!
* Copyright (c) 2021 han_feng@foxmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
import Logger from"js-logger";import Vue from"vue";import createVuexAlong from"vuex-along";import router from"./router";import store from"./store";var logger=Logger.get("modular.vue.activator"),HOOK_KEYS=Object.freeze({beforeEach:!0,beforeResolve:!0,afterEach:!0,onReady:!0,onError:!0}),defaultAppComponent={name:"app",render:function(e){return e("router-view")}},defaultAppElement="#app";function addVuexModules(e){for(var r=e.getApplication(),o={name:"VAS:"+r.name+"-"+r.version},t=[],n=[],u=e.getExtension("vuex.modules"),a=0,i=Object.keys(u);a<i.length;a++){var s=i[a],l=u[s];store.registerModule(s,l),l.storage&&("session"===l.storage?n.push(s):"local"===l.storage&&t.push(s))}n.length>0&&(o.session={list:n}),t.length>0?o.local={list:t}:o.justSession=!0,createVuexAlong(o)(store)}function addPlugins(e){var r=e.getExtension("vue.plugins");null!==r&&r.forEach((function(e){return Vue.use(e)}))}function addRoutes(e){var r=e.getExtension("vue.router.routes"),o=[],t={},n={};function u(e){if(void 0!==e.children&&Array.isArray(e.children)){var r=e.name;if(void 0===r||""===r)return void logger.error("Error: 路由名称未定义,"+JSON.stringify(e));var o=e.children;t[r]=o,n[r]&&(n[r].forEach((function(e){o.push(e)})),delete n[r]),e.children.forEach((function(e){u(e)}))}}if(null!==r){r.forEach((function(e){if(e&&e.parent&&e.routes){var r,a=e.parent;"root"===a?r=o:void 0===(r=t[a])&&(n[a]=n[a]||[],r=n[a]),e.routes.forEach((function(e){r.push(e),u(e)}))}}));for(var a=0,i=Object.keys(n);a<i.length;a++){var s=i[a];logger.error("Error: 父路由“"+s+"”不存在")}router.addRoutes(o),e.setAttribute("vue.routes",o)}}function addRouterHooks(e){var r=e.getExtension("vue.router.hooks");if(null!==r){var o=router;r.forEach((function(e){for(var r in e)void 0!==HOOK_KEYS[r]&&o[r](e[r])}))}}function createVueInstance(e){var r=e.getExtension("vue.app")||{component:defaultAppComponent,element:defaultAppElement},o=r.component||defaultAppComponent,t={router:router,store:store,render:function(e){return e(o)}},n=e.getExtension("vue.options");null!==n&&(t.mixins=n);var u=new Vue(t),a=r.element||defaultAppElement;u.$mount(a),e.setAttribute("vue.instance",u)}var activator={start:function(e){addVuexModules(e),addPlugins(e),addRoutes(e),addRouterHooks(e),createVueInstance(e)}};export default activator;