import address from 'address' ; import assert from 'assert' ; import { isString } from 'lodash' ; import { join } from 'path' ; import { IApi } from 'umi-types' ; import webpack from 'webpack' ; import { addSpecifyPrefixedRoute , defaultSlaveRootId } from '../common' ; import { Options } from '../types' ; const localIpAddress = process . env . USE_REMOTE_IP ? address . ip ( ) : 'localhost' ; export default function ( api , options ) { const { registerRuntimeKeyInIndex = false , keepOriginalRoutes = false , shouldNotModifyRuntimePublicPath = false , shouldNotModifyDefaultBase = false , shouldNotModifyMountElementId = false , } = options || { } ; api . addRuntimePlugin ( require . resolve ( './runtimePlugin' ) ) ; if ( ! registerRuntimeKeyInIndex ) { api . addRuntimePluginKey ( 'qiankun' ) ; } const lifecyclePath = require . resolve ( './lifecycles' ) ; const mountElementId = api . config . mountElementId || defaultSlaveRootId ; const { name : pkgName } = require ( join ( api . cwd , 'package.json' ) ) ; api . modifyDefaultConfig ( memo => { const config = { runtimePublicPath : true , } ; if ( ! shouldNotModifyMountElementId ) { config . mountElementId = mountElementId ; } if ( ! shouldNotModifyDefaultBase ) { config . base = `/${ pkgName }` ; } return { ... memo , ... config , } ; } ) ; if ( api . config . runtimePublicPath !== false && ! shouldNotModifyRuntimePublicPath ) { api . modifyPublicPathStr ( `window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || window.publicPath || "${ process . env . NODE_ENV !== 'development' ? api . config . publicPath || '/' : '/' }"` ) ; } const port = process . env . PORT ; const protocol = process . env . HTTPS ? 'https' : 'http' ; api . modifyWebpackConfig ( memo => { memo . output ! . libraryTarget = 'umd' ; assert ( api . pkg . name , 'You should have name in package.json' ) ; memo . output ! . library = `${ api . pkg . name }-[name]` ; memo . output ! . jsonpFunction = `webpackJsonp_${ api . pkg . name }` ; if ( process . env . NODE_ENV === 'development' && port ) { memo . output ! . publicPath = `${ protocol }://${ localIpAddress }:${ port }/` ; } return memo ; } ) ; api . modifyHTMLWithAST ( $ => { $ ( 'script' ) . each ( ( _ , el ) => { const scriptEl = $ ( el ) ; const umiEntryJs = /\/?umi(\.\w+)?\.js$/g ; const src = scriptEl . attr ( 'src' ) ; if ( src && umiEntryJs . test ( scriptEl . attr ( 'src' ) ) ) { scriptEl . attr ( 'entry' , '' ) ; } } ) ; return $ ; } ) ; if ( process . env . NODE_ENV === 'development' && port ) { process . env . SOCKET_SERVER = `${ protocol }://${ localIpAddress }:${ port }/` ; api . chainWebpackConfig ( memo => { memo . devtool ( false ) ; memo . plugin ( 'source-map' ) . use ( webpack . SourceMapDevToolPlugin , [ { namespace : pkgName , append : `\n//# sourceMappingURL=${ protocol }://${ localIpAddress }:${ port }/[url]` , filename : '[file].map' , } , ] ) ; } ) ; } api . writeTmpFile ( 'qiankunContext.js' , `
import { createContext, useContext } from 'react';

export const Context = createContext(null);
export function useRootExports() {
  return useContext(Context);
};
  ` . trim ( ) ) ; api . addUmiExports ( [ { specifiers : [ 'useRootExports' ] , source : '@tmp/qiankunContext' , } , ] ) ; api . addEntryImport ( { source : lifecyclePath , specifier : '{ genMount as qiankun_genMount, genBootstrap as qiankun_genBootstrap, genUnmount as qiankun_genUnmount }' , } ) ; api . addRendererWrapperWithModule ( lifecyclePath ) ; api . addEntryCode ( `
    export const bootstrap = qiankun_genBootstrap(Promise.all(moduleBeforeRendererPromises), render);
    export const mount = qiankun_genMount();
    export const unmount = qiankun_genUnmount('${ mountElementId }');

    if (!window.__POWERED_BY_QIANKUN__) {
      bootstrap().then(mount);
    }
    ` ) ; api . modifyRoutes ( routes => { if ( keepOriginalRoutes === true || isString ( keepOriginalRoutes ) ) { return addSpecifyPrefixedRoute ( routes , keepOriginalRoutes , pkgName ) ; } return routes ; } ) ; }