@@ -2,7 +2,6 @@ import * as fs from "fs";
2
2
import * as path from "path" ;
3
3
import { promisify } from "util" ;
4
4
import { JSDOM } from "jsdom" ;
5
- import { CallbackWrapper } from "./misc"
6
5
import * as SPAs from "../../../config/spa.config" ;
7
6
8
7
export async function postProcess ( workDir : string , filePattern : string ) : Promise < void > {
@@ -23,39 +22,27 @@ export async function postProcess(workDir: string, filePattern: string): Promise
23
22
24
23
async function postProcessBody ( workDir : string , htmlFile : string , ssrFile : string ) : Promise < void > {
25
24
const readFile = promisify ( fs . readFile ) ;
25
+ const writeFile = promisify ( fs . writeFile ) ;
26
+
26
27
const htmlFilePath = path . join ( workDir , htmlFile ) ;
27
28
const ssrFilePath = path . join ( workDir , ssrFile ) ;
28
29
29
- const dataHtml = await readFile ( htmlFilePath ) ;
30
+ const jsdom = await JSDOM . fromFile ( htmlFilePath ) ;
31
+
32
+ if ( ! jsdom ) {
33
+ throw "JSDOM creation failure" ;
34
+ }
35
+
30
36
const dataSsr = ( await readFile ( ssrFilePath ) ) . toString ( ) ;
31
- const reReact = / ^ \s * < d i v \s + i d = " a p p - r o o t " > / ;
32
- const ar : string [ ] = dataHtml . toString ( ) . replace ( / \r \n ? / g, "\n" ) . split ( "\n" ) ;
33
-
34
- const out = ar . map ( str => {
35
- if ( reReact . test ( str ) ) {
36
- str += "\n" ;
37
- str += dataSsr ;
38
- }
39
- str += "\n" ;
40
- return str ;
41
- } ) ;
42
-
43
- const stream = fs . createWriteStream ( htmlFilePath ) ;
44
- stream . on ( "error" , err => {
45
- console . error ( `Failed to write to file ${ htmlFilePath } , error: ${ err } ` )
46
- } ) ;
47
- out . forEach ( str => { stream . write ( str ) ; } ) ;
48
-
49
- stream . end ( ) ;
50
-
51
- let cbWrapper : CallbackWrapper | undefined ;
52
- const waitForBufferFlush = new Promise < void > ( ( resolve ) => { cbWrapper = new CallbackWrapper ( resolve ) ; } ) ;
53
-
54
- stream . on ( "close" , function ( ) {
55
- cbWrapper ?. invoke ( ) ;
56
- } ) ;
57
-
58
- await waitForBufferFlush ;
37
+ const fragment = JSDOM . fragment ( dataSsr ) ;
38
+ const appRoot = jsdom . window . document . querySelector ( "div[id='app-root']" ) ;
39
+
40
+ if ( ! appRoot ) {
41
+ throw "JSDOM - 'app-root' not found" ;
42
+ }
43
+
44
+ appRoot . appendChild ( fragment ) ;
45
+ await writeFile ( htmlFilePath , jsdom . serialize ( ) ) ;
59
46
}
60
47
61
48
async function postProcessHeader ( workDir : string , htmlFile : string ) : Promise < void > {
0 commit comments