import * as path from 'path' ; import { IRouterOptions } from './types/router' ; import walker from './collector/walker' ; import vitePluginLazy from './vitePluginLazy' ; const TEM_ROUTER_COMPATIBLE = '$ice/routes' ; const TEM_ROUTER_SETS = [ TEM_ROUTER_COMPATIBLE ] ; const plugin = ( { context , onGetWebpackConfig , modifyUserConfig , getValue , applyMethod , log } ) => { const { rootDir , userConfig , command } = context ; const projectType = getValue ( 'PROJECT_TYPE' ) ; const iceTempPath = getValue ( 'TEMP_PATH' ) ; const routerOptions = ( userConfig . router || { } ) as IRouterOptions ; const { configPath } = routerOptions ; const { mpa : isMpa } = userConfig ; const routesTempPath = path . join ( iceTempPath , `routes.${ projectType }` ) ; const srcDir = applyMethod ( 'getSourceDir' , userConfig . entry ) ; const { routesPath , isConfigRoutes } = applyMethod ( 'getRoutes' , { rootDir , tempPath : iceTempPath , configPath , projectType , isMpa , srcDir } ) ; if ( ! isConfigRoutes ) { log . info ( '开启文件约定式路由能力' ) ; } onGetWebpackConfig ( ( config ) => { TEM_ROUTER_SETS . forEach ( i => { config . resolve . alias . set ( i , routesPath ) ; } ) ; config . resolve . alias . set ( '$ice/Router' , path . join ( iceTempPath , 'plugins/router/pluginRuntime/runtime/Router.tsx' ) ) ; config . resolve . alias . set ( '$ice/history' , path . join ( iceTempPath , 'plugins/router/history.ts' ) ) ; const routerName = 'react-router-dom' ; const packagePath = require . resolve ( `${ routerName }/package.json` ) ; config . resolve . alias . set ( routerName , path . dirname ( packagePath ) ) ; if ( ! config . devServer . get ( 'historyApiFallback' ) ) { config . devServer . set ( 'historyApiFallback' , true ) ; } } ) ; applyMethod ( 'addPluginTemplate' , { template : path . join ( __dirname , '../src/types' ) , targetDir : 'router/types' , } ) ; applyMethod ( 'addAppConfigTypes' , { source : '../plugins/router/types' , specifier : '{ IAppRouterProps }' , exportName : 'router?: IAppRouterProps' } ) ; applyMethod ( 'addTypesExport' , { source : '../plugins/router/types' } ) ; applyMethod ( 'addImportDeclaration' , { importSource : '$$ice/plugins/router/types' , exportMembers : [ 'IAppRouterProps' , 'IRouterConfig' ] , } ) ; modifyUserConfig ( 'babelPlugins' , [ ... ( userConfig . babelPlugins as [ ] || [ ] ) , [ require . resolve ( './babelPluginLazy' ) , { routesPath } ] ] ) ; if ( userConfig . vite ) { modifyUserConfig ( 'vite.plugins' , [ vitePluginLazy ( routesPath ) ] , { deepmerge : true } ) ; } const routerTemplatesPath = path . join ( __dirname , '../templates' ) ; applyMethod ( 'addPluginTemplate' , routerTemplatesPath ) ; applyMethod ( 'addExport' , { source : './plugins/router' , importSource : '$$ice/plugins/router' , exportMembers : [ 'createBrowserHistory' , 'createHashHistory' , 'createMemoryHistory' , 'Link' , 'NavLink' , 'Prompt' , 'Redirect' , 'Route' , 'Switch' , 'matchPath' , 'generatePath' , 'useHistory' , 'useLocation' , 'useParams' , 'useRouteMatch' ] } ) ; if ( ! isConfigRoutes ) { const routerMatch = 'src/pages' ; const pagesDir = path . join ( rootDir , routerMatch ) ; const walkerOptions = { rootDir , routerOptions , routesTempPath , pagesDir } ; walker ( walkerOptions ) ; log . verbose ( '根据 src/pages 结构生成路由配置：' , routesTempPath ) ; if ( command === 'start' ) { applyMethod ( 'watchFileChange' , routerMatch , ( ) => { walker ( walkerOptions ) ; log . verbose ( '监听到 src/pages 目录变化，重新生成路由配置：' , routesTempPath ) ; } ) ; } } } ; export default plugin ;