import { existsSync } from 'fs' ; import { join } from 'path' ; import { IApi , IConfig , IRoute } from 'umi-types' ; import { defaultHistoryMode , defaultMasterRootId , testPathWithPrefix , toArray } from '../common' ; import { Options } from '../types' ; export default function ( api , options ) { const { registerRuntimeKeyInIndex = false } = options || { } ; api . addRuntimePlugin ( require . resolve ( './runtimePlugin' ) ) ; if ( ! registerRuntimeKeyInIndex ) { api . addRuntimePluginKey ( 'qiankun' ) ; } api . modifyDefaultConfig ( config => ( { ... config , mountElementId : defaultMasterRootId , disableGlobalVariables : true , } ) ) ; const { config : { history = defaultHistoryMode } , } = api ; const { apps = [ ] } = options || { } ; if ( apps . length ) { const findRouteWithPrefix = ( routes , basePath ) : IRoute | null => { for ( const route of routes ) { if ( route . path && testPathWithPrefix ( basePath , route . path ) ) return route ; if ( route . routes && route . routes . length ) { const r = findRouteWithPrefix ( route . routes , basePath ) ; if ( r ) { return r ; } } } return null ; } ; const modifyAppRoutes = ( masterHistory ) => { api . modifyRoutes ( routes => { const newRoutes = routes . map ( route => { if ( route . path === '/' && route . routes && route . routes . length ) { apps . forEach ( ( { history : slaveHistory = history , base } ) => { if ( slaveHistory === masterHistory ) { const baseConfig = toArray ( base ) ; baseConfig . forEach ( basePath => { const routeWithPrefix = findRouteWithPrefix ( routes , basePath ) ; if ( ! routeWithPrefix ) { route . routes ! . unshift ( { path : basePath , exact : false , component : `() => {
                        if (process.env.NODE_ENV === 'development') {
                          console.log('${ basePath } 404 mock rendered');
                        }

                        return React.createElement('div');
                      }` , } ) ; } else { routeWithPrefix . exact = false ; } } ) ; } } ) ; } return route ; } ) ; return newRoutes ; } ) ; } ; modifyAppRoutes ( history ) ; } const rootExportsJsFile = join ( api . paths . absSrcPath , 'rootExports.js' ) ; const rootExportsTsFile = join ( api . paths . absSrcPath , 'rootExports.ts' ) ; const rootExportsJsFileExisted = existsSync ( rootExportsJsFile ) ; const rootExportsFileExisted = rootExportsJsFileExisted || existsSync ( rootExportsTsFile ) ; api . addPageWatcher ( rootExportsJsFileExisted ? rootExportsJsFile : rootExportsTsFile ) ; api . onGenerateFiles ( ( ) => { const rootExports = `
window.g_rootExports = ${ rootExportsFileExisted ? `require('@/rootExports')` : `{}` };
    ` . trim ( ) ; api . writeTmpFile ( 'qiankunRootExports.js' , rootExports ) ; api . writeTmpFile ( 'subAppsConfig.json' , JSON . stringify ( { masterHistory : history , ... options , } ) ) ; } ) ; api . writeTmpFile ( 'qiankunDefer.js' , `
      class Deferred {
        constructor() {
          this.promise = new Promise(resolve => this.resolve = resolve);
        }
      }
      export const deferred = new Deferred();
      export const qiankunStart = deferred.resolve;
    ` . trim ( ) ) ; api . addUmiExports ( [ { specifiers : [ 'qiankunStart' ] , source : '@tmp/qiankunDefer' , } , ] ) ; }