@@ -5,6 +5,7 @@ const path = require("path");
55const axios = require ( 'axios' ) ;
66const fs = require ( 'fs' ) ;
77const fsPromises = require ( 'fs' ) . promises ;
8+ const htmlParse = require ( 'node-html-parser' ) ;
89const APP_CONST = require ( './config/constants' ) ;
910
1011let distPath = path . resolve ( __dirname , "./dist" ) ;
@@ -54,25 +55,30 @@ app.get("*", async function (req, res) {
5455 res . send ( { 'error' : { message : "Check application environment" , code : 500 } } )
5556 break ;
5657 }
57- if ( mfeIndex . indexOf ( "<!-- Routes Start -->" ) > - 1 ) {
58- let mfeRoutes
59- switch ( process . env . APPENV . toLowerCase ( ) ) {
60- case APP_CONST . APP_ENV_DEV . toLowerCase ( ) :
61- case APP_CONST . APP_ENV_PROD . toLowerCase ( ) :
62- mfeRoutes = await axios . get ( env_config . mfeRoutesPath )
63- mfeRoutes = mfeRoutes . data
64- break ;
65- case APP_CONST . APP_ENV_LOCAL . toLowerCase ( ) :
66- case APP_CONST . APP_ENV_LOCAL_MULTI . toLowerCase ( ) :
67- mfeRoutes = await fsPromises . readFile ( path . join ( configPath + env_config . mfeRoutesPath ) )
68- break ;
69- default :
70- res . send ( { 'error' : { message : "Check application environment" , code : 500 } } )
71- break ;
72- }
73- const mfe = mfeIndex . toString ( ) . replace ( / < ! - - R o u t e s S t a r t - - > ( [ \s \S ] * ?) < ! - - R o u t e s E n d - - > / , "<!-- Routes Start -->" + mfeRoutes + "<!-- Routes End -->" ) ;
74- await fsPromises . writeFile ( path . join ( distPath + "/index.html" ) , mfe ) ;
58+ let mfeRoutes
59+ switch ( process . env . APPENV . toLowerCase ( ) ) {
60+ case APP_CONST . APP_ENV_DEV . toLowerCase ( ) :
61+ case APP_CONST . APP_ENV_PROD . toLowerCase ( ) :
62+ mfeRoutes = await axios . get ( env_config . mfeRoutesPath )
63+ mfeRoutes = mfeRoutes . data
64+ break ;
65+ case APP_CONST . APP_ENV_LOCAL . toLowerCase ( ) :
66+ case APP_CONST . APP_ENV_LOCAL_MULTI . toLowerCase ( ) :
67+ mfeRoutes = await fsPromises . readFile ( path . join ( configPath + env_config . mfeRoutesPath ) )
68+ break ;
69+ default :
70+ res . send ( { 'error' : { message : "Check application environment" , code : 500 } } )
71+ break ;
7572 }
73+ let mfeIndexHtml = htmlParse . parse ( mfeIndex )
74+ let singleSpaMain = mfeIndexHtml . querySelector ( '#single-spa-main' )
75+ singleSpaMain . remove ( )
76+ let singleSpaRouter = mfeIndexHtml . querySelector ( 'single-spa-router' )
77+ singleSpaRouter . insertAdjacentHTML ( 'beforeend' , '<div id="single-spa-main"></div>' )
78+ singleSpaMain = mfeIndexHtml . querySelector ( '#single-spa-main' )
79+ singleSpaMain . insertAdjacentHTML ( 'beforeend' , mfeRoutes )
80+
81+ await fsPromises . writeFile ( path . join ( distPath + "/index.html" ) , mfeIndexHtml . toString ( ) ) ;
7682 res . sendFile ( path . join ( distPath + "/index.html" ) ) ;
7783} ) ;
7884
0 commit comments