@@ -5,6 +5,7 @@ const path = require("path");
5
5
const axios = require ( 'axios' ) ;
6
6
const fs = require ( 'fs' ) ;
7
7
const fsPromises = require ( 'fs' ) . promises ;
8
+ const htmlParse = require ( 'node-html-parser' ) ;
8
9
const APP_CONST = require ( './config/constants' ) ;
9
10
10
11
let distPath = path . resolve ( __dirname , "./dist" ) ;
@@ -54,25 +55,30 @@ app.get("*", async function (req, res) {
54
55
res . send ( { 'error' : { message : "Check application environment" , code : 500 } } )
55
56
break ;
56
57
}
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 ;
75
72
}
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 ( ) ) ;
76
82
res . sendFile ( path . join ( distPath + "/index.html" ) ) ;
77
83
} ) ;
78
84
0 commit comments