import Vue from 'vue' ; import VueRouter , { RouteConfig } from 'vue-router' ; import config , { NavsItem } from '../config' ; import { importAll , ImportMap , RequireContext } from '../utils' ; import { Locale } from '../../../src/index' ; import { camelize } from '../../../src/utils/format/string' ; Vue . use ( VueRouter ) ; function wrapComponentLocale ( module , name ) { const component = module . default ; name = 'demo-' + name ; component . name = name ; const { i18n : config } = component ; if ( config ) { const formattedI18n = { } ; const camelizedName = camelize ( name ) ; Object . keys ( config ) . forEach ( key => { formattedI18n [ key ] = { [ camelizedName ] : config [ key ] } ; } ) ; Locale . add ( formattedI18n ) ; } return component ; } function registerRoute ( componentMap ) { const route = [ ] ; function addRoute ( nav ) { let { path } = nav ; if ( path ) { path = path . replace ( '/' , '' ) ; const module = componentMap [ `./${ path }/demo/index.vue` ] ; const component = module ? wrapComponentLocale ( module , path ) : null ; if ( component ) { route . push ( { component , name : path , path : `/${ path }` , meta : { path , } , } ) ; } } } function findNav ( nav ) { if ( nav . list ) { nav . list . forEach ( item => findNav ( item ) ) ; } else { addRoute ( nav ) ; } } config . forEach ( nav => { findNav ( nav ) ; } ) ; return route ; } const docs = { } ; const docsFromPackages = require . context ( '../../../src/components' , true , /demo\/index\.vue$/ ) ; importAll ( docs , docsFromPackages ) ; const routes = registerRoute ( docs ) ; const router = new VueRouter ( { mode : 'hash' , routes , } ) ; router . afterEach ( ( ) => { window . scrollTo ( 0 , 0 ) ; } ) ; export default router ;