file author tony7lee
  import { fillTabWith } from '../utils' ; import { ICollectItem } from '../types/collector' ; interface IPlayload { nestImports : string [ ] ; nestImportsNames : string [ ] ; nestSlice : string [ ] ; indent : number ; } param { * } payload param { * } collect function loopSplice ( payload , collect , routerOptions ) { const indent = payload . indent ; const indentTabs = fillTabWith ( indent ) ; const indentTabsSub1 = fillTabWith ( indent - 1 ) ; const { lazy } = routerOptions ; payload . indent += 2 ; payload . nestSlice . push ( '[' ) ; const depth = ( item ) => item ?. routePathAmend ?. split ( '/' ) . length || 0 ; const sortedCollect = collect . sort ( ( a , b ) => { return depth ( b ) - depth ( a ) ; } ) ; sortedCollect . forEach ( item => { const { routePathAmend , component , filePath , children , isLayoutLike , exact } = item ; if ( ! payload . nestImportsNames . includes ( component ) ) { let nestImportsData = `import ${ component } from '${ filePath }';\n` ; if ( lazy && ! children ) { nestImportsData = `const ${ component } = lazy(() => import(/* webpackChunkName: '${ component }' */ '${ filePath }'));\n` ; } payload . nestImports . push ( nestImportsData ) ; payload . nestImportsNames . push ( component ) ; } const defaultExact = exact || 'true' ; payload . nestSlice . push ( `
${ indentTabs }{
  ${ indentTabs }path: '${ routePathAmend }',
  ${ indentTabs }exact: ${ isLayoutLike ? 'false' : defaultExact },
  ${ indentTabs }component: ${ component }` ) ; if ( children && children . length ) { payload . nestSlice . push ( `,
  ${ indentTabs }children: ` ) ; loopSplice ( payload , children , routerOptions ) ; payload . nestSlice . push ( ',' ) ; } payload . nestSlice . push ( `
${ indentTabs }},` ) ; } ) ; payload . nestSlice . push ( `\n${ indentTabsSub1 }]` ) ; } export default function splicer ( routesCollect , routerOptions ) { const payload = { nestImports : [ ] , nestImportsNames : [ ] , nestSlice : [ ] , indent : 1 } ; loopSplice ( payload , routesCollect , routerOptions ) ; let importAhead = '' ; if ( routerOptions . lazy ) { importAhead = 'import { lazy } from \'ice\';\n' ; } return `${ importAhead }${ payload . nestImports . join ( '' ) }\nexport default ${ payload . nestSlice . join ( '' ) };` ; }