author Kuitos
 *  since import { cloneDeep } from 'lodash' ; import pathToRegexp from 'path-to-regexp' ; import { IRoute } from 'umi-types' ; import { keepOriginalRoutesOption } from './types' ; export const defaultMountContainerId = 'root-subapp' ; export const defaultMasterRootId = 'root-master' ; export const defaultSlaveRootId = 'root-slave' ; export const defaultHistoryMode = 'browser' ; export const noop = ( ) => { } ; export function toArray < T > ( source ) { return Array . isArray ( source ) ? source : [ source ] ; } function testPathWithStaticPrefix ( pathPrefix , realPath ) { if ( pathPrefix . endsWith ( '/' ) ) { return realPath . startsWith ( pathPrefix ) ; } const pathRegex = new RegExp ( `^${ pathPrefix }(\\/|\\?)+.*$` , 'g' ) ; const normalizedPath = `${ realPath }/` ; return pathRegex . test ( normalizedPath ) ; } function testPathWithDynamicRoute ( dynamicRoute , realPath ) { return ! ! pathToRegexp ( dynamicRoute , { strict : true , end : false } ) . exec ( realPath ) ; } export function testPathWithPrefix ( pathPrefix , realPath ) { return testPathWithStaticPrefix ( pathPrefix , realPath ) || testPathWithDynamicRoute ( pathPrefix , realPath ) ; } const recursiveCoverRouter = ( source , nameSpacePath ) => source . map ( ( router ) => { if ( router . routes ) { recursiveCoverRouter ( router . routes , nameSpacePath ) ; } if ( router . path !== '/' && router . path ) { return { ... router , path : `${ nameSpacePath }${ router . path }` , } ; } return router ; } ) ; export const addSpecifyPrefixedRoute = ( originRoute , keepOriginalRoutes , pkgName ? ) => { const copyBase = originRoute . filter ( _ => _ . path === '/' ) ; if ( ! copyBase [ 0 ] ) { return originRoute ; } const nameSpaceRouter = cloneDeep ( copyBase [ 0 ] ) ; const nameSpace = keepOriginalRoutes === true ? pkgName : keepOriginalRoutes ; nameSpaceRouter . path = `/${ nameSpace }` ; nameSpaceRouter . routes = recursiveCoverRouter ( nameSpaceRouter . routes , `/${ nameSpace }` ) ; return [ nameSpaceRouter , ... originRoute ] ; } ;