@@ -6,19 +6,18 @@ import { build, mergeConfig } from 'vite';
6
6
7
7
import { getPageData , renderViteHtml } from '../react-app-static/static.js' ;
8
8
import { buildReactAppViteConfig } from '../react-app-vite/app.config.js' ;
9
- import { removeUndefinedProperties , runParamsConfigModifier } from '../util.js' ;
9
+ import { buildParams } from '../util.js' ;
10
10
11
11
12
12
export const buildStaticReactApp = async ( inputParams = { } ) => {
13
13
const defaultParams = {
14
- dev : false ,
15
14
configModifier : undefined ,
16
15
polyfill : true ,
17
16
polyfillTargets : undefined ,
18
17
webpackConfigModifier : undefined ,
19
18
analyzeBundle : false ,
20
19
shouldAliasModules : true ,
21
- addHtmlOutput : false ,
20
+ addHtmlOutput : true ,
22
21
addRuntimeConfig : true ,
23
22
runtimeConfigVars : { } ,
24
23
seoTags : [ ] ,
@@ -28,8 +27,7 @@ export const buildStaticReactApp = async (inputParams = {}) => {
28
27
publicDirectory : path . join ( process . cwd ( ) , './public' ) ,
29
28
appEntryFilePath : path . join ( process . cwd ( ) , './src/App.tsx' ) ,
30
29
} ;
31
- let params = { ...defaultParams , ...removeUndefinedProperties ( inputParams ) } ;
32
- params = await runParamsConfigModifier ( params ) ;
30
+ const params = await buildParams ( defaultParams , inputParams , false ) ;
33
31
let viteConfig = buildReactAppViteConfig ( params ) ;
34
32
if ( params . viteConfigModifier ) {
35
33
viteConfig = params . viteConfigModifier ( viteConfig ) ;
@@ -39,19 +37,19 @@ export const buildStaticReactApp = async (inputParams = {}) => {
39
37
40
38
const outputDirectoryPath = path . resolve ( params . outputDirectory ) ;
41
39
const appEntryFilePath = path . resolve ( params . appEntryFilePath ) ;
42
- const clientOutputDirectoryPath = path . join ( outputDirectoryPath , '_client' ) ;
43
- const ssrOutputDirectoryPath = path . join ( outputDirectoryPath , '_ssr' ) ;
40
+ const clientDirectory = path . join ( outputDirectoryPath , '_client' ) ;
41
+ const ssrDirectory = path . join ( outputDirectoryPath , '_ssr' ) ;
44
42
console . log ( 'building app...' ) ;
45
43
await build ( mergeConfig ( viteConfig , {
46
44
build : {
47
- outDir : clientOutputDirectoryPath ,
45
+ outDir : clientDirectory ,
48
46
} ,
49
47
} ) ) ;
50
48
console . log ( 'building server app...' ) ;
51
49
await build ( mergeConfig ( viteConfig , {
52
50
build : {
53
51
ssr : true ,
54
- outDir : ssrOutputDirectoryPath ,
52
+ outDir : ssrDirectory ,
55
53
rollupOptions : {
56
54
input : appEntryFilePath ,
57
55
// NOTE(krishan711): prevent the hashes in the names
@@ -63,17 +61,17 @@ export const buildStaticReactApp = async (inputParams = {}) => {
63
61
} ,
64
62
} ,
65
63
} ) ) ;
66
-
67
- const app = ( await import ( path . join ( ssrOutputDirectoryPath , 'assets/App.js' ) ) ) ;
68
- const htmlTemplate = await fs . readFileSync ( path . join ( clientOutputDirectoryPath , 'index.html' ) , 'utf-8' ) ;
64
+ const app = ( await import ( path . join ( ssrDirectory , 'assets/app.js' ) ) ) ;
65
+ const appData = { name , port : params . port , defaultSeoTags : params . seoTags } ;
66
+ const htmlTemplate = await fs . readFileSync ( path . join ( clientDirectory , 'index.html' ) , 'utf-8' ) ;
69
67
// NOTE(krishan711): if this could be done in an parallel way it would be faster!
70
68
params . pages . forEach ( async ( page ) => {
71
69
console . log ( `Rendering page ${ page . path } to ${ page . filename } ` ) ;
72
70
const pageData = ( app . routes && app . globals ) ? await getPageData ( page . path , app . routes , app . globals ) : null ;
73
- const output = renderViteHtml ( app . App , page , params . seoTags , name , pageData , htmlTemplate ) ;
71
+ const html = await renderViteHtml ( app . App , page , appData . defaultSeoTags , appData . name , pageData , htmlTemplate ) ;
74
72
const outputPath = path . join ( outputDirectoryPath , page . filename ) ;
75
73
fs . mkdirSync ( path . dirname ( outputPath ) , { recursive : true } ) ;
76
- fs . writeFileSync ( outputPath , output ) ;
74
+ fs . writeFileSync ( outputPath , html ) ;
77
75
console . log ( `Done rendering page ${ page . path } ` ) ;
78
76
} ) ;
79
77
} ;
0 commit comments