1
1
import path from 'path' ;
2
2
import type { AppTools , CliPlugin } from '@modern-js/app-tools' ;
3
- import type { NestedRouteForCli , PageRoute } from '@modern-js/types' ;
3
+ import type {
4
+ NestedRouteForCli ,
5
+ PageRoute ,
6
+ SSGSingleEntryOptions ,
7
+ } from '@modern-js/types' ;
4
8
import { filterRoutesForServer , logger } from '@modern-js/utils' ;
5
- import { generatePath } from 'react-router-dom' ;
6
9
import { makeRoute } from './libs/make' ;
7
10
import { writeHtmlFile } from './libs/output' ;
8
11
import { replaceRoute } from './libs/replace' ;
@@ -15,7 +18,13 @@ import {
15
18
writeJSONSpec ,
16
19
} from './libs/util' ;
17
20
import { createServer } from './server' ;
18
- import type { AgreedRouteMap , SSGConfig , SsgRoute } from './types' ;
21
+ import type {
22
+ AgreedRoute ,
23
+ AgreedRouteMap ,
24
+ SSGConfig ,
25
+ SSGRouteOptions ,
26
+ SsgRoute ,
27
+ } from './types' ;
19
28
20
29
export const ssgPlugin = ( ) : CliPlugin < AppTools > => ( {
21
30
name : '@modern-js/plugin-ssg' ,
@@ -42,11 +51,12 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
42
51
const { output, server } = resolvedConfig ;
43
52
const {
44
53
ssg,
54
+ ssgByEntries,
45
55
distPath : { root : outputPath } = { } ,
46
56
} = output ;
47
57
48
58
const ssgOptions : SSGConfig =
49
- ( Array . isArray ( ssg ) ? ssg . pop ( ) : ssg ) || true ;
59
+ ( Array . isArray ( ssg ) ? ssg . pop ( ) : ssg ) ?? true ;
50
60
51
61
const buildDir = path . join ( appDirectory , outputPath as string ) ;
52
62
const routes = readJSONSpec ( buildDir ) ;
@@ -65,6 +75,7 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
65
75
entrypoints ,
66
76
pageRoutes ,
67
77
server ,
78
+ ssgByEntries ,
68
79
) ;
69
80
70
81
if ( ! intermediateOptions ) {
@@ -76,9 +87,8 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
76
87
pageRoutes . forEach ( pageRoute => {
77
88
const { entryName, entryPath } = pageRoute ;
78
89
const agreedRoutes = agreedRouteMap [ entryName as string ] ;
79
- let entryOptions =
80
- intermediateOptions [ entryName as string ] ||
81
- intermediateOptions [ pageRoute . urlPath ] ;
90
+ let entryOptions = ( intermediateOptions [ entryName as string ] ||
91
+ intermediateOptions [ pageRoute . urlPath ] ) as SSGSingleEntryOptions ;
82
92
83
93
if ( ! agreedRoutes ) {
84
94
// default behavior for non-agreed route
@@ -93,7 +103,7 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
93
103
// if entryOptions is object and has routes options
94
104
// add every route in options
95
105
const { routes : enrtyRoutes , headers } = entryOptions ;
96
- enrtyRoutes . forEach ( route => {
106
+ enrtyRoutes . forEach ( ( route : SSGRouteOptions ) => {
97
107
ssgRoutes . push ( makeRoute ( pageRoute , route , headers ) ) ;
98
108
} ) ;
99
109
}
@@ -105,42 +115,32 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
105
115
}
106
116
107
117
if ( entryOptions === true ) {
108
- entryOptions = { preventDefault : [ ] , routes : [ ] , headers : { } } ;
118
+ entryOptions = { routes : [ ] , headers : { } } ;
109
119
}
110
120
111
- const {
112
- preventDefault = [ ] ,
113
- routes : userRoutes = [ ] ,
114
- headers,
115
- } = entryOptions ;
121
+ const { routes : userRoutes = [ ] , headers } =
122
+ ( entryOptions as {
123
+ routes ?: SSGRouteOptions [ ] ;
124
+ headers ?: Record < string , string > ;
125
+ } ) || { } ;
116
126
// if the user sets the routes, then only add them
117
127
if ( userRoutes . length > 0 ) {
118
- userRoutes . forEach ( route => {
119
- if ( typeof route === 'string' ) {
120
- ssgRoutes . push ( makeRoute ( pageRoute , route , headers ) ) ;
121
- } else if ( Array . isArray ( route . params ) ) {
122
- route . params . forEach ( param => {
123
- ssgRoutes . push (
124
- makeRoute (
125
- pageRoute ,
126
- { ...route , url : generatePath ( route . url , param ) } ,
127
- headers ,
128
- ) ,
129
- ) ;
130
- } ) ;
131
- } else {
132
- ssgRoutes . push ( makeRoute ( pageRoute , route , headers ) ) ;
128
+ ( userRoutes as SSGRouteOptions [ ] ) . forEach (
129
+ ( route : SSGRouteOptions ) => {
130
+ if ( typeof route === 'string' ) {
131
+ ssgRoutes . push ( makeRoute ( pageRoute , route , headers ) ) ;
132
+ } else {
133
+ ssgRoutes . push ( makeRoute ( pageRoute , route , headers ) ) ;
134
+ }
135
+ } ,
136
+ ) ;
137
+ } else {
138
+ // default: add all non-dynamic routes
139
+ agreedRoutes . forEach ( ( route : AgreedRoute ) => {
140
+ if ( ! isDynamicUrl ( route . path ! ) ) {
141
+ ssgRoutes . push ( makeRoute ( pageRoute , route . path ! , headers ) ) ;
133
142
}
134
143
} ) ;
135
- } else {
136
- // otherwith add all except dynamic routes
137
- agreedRoutes
138
- . filter ( route => ! preventDefault . includes ( route . path ! ) )
139
- . forEach ( route => {
140
- if ( ! isDynamicUrl ( route . path ! ) ) {
141
- ssgRoutes . push ( makeRoute ( pageRoute , route . path ! , headers ) ) ;
142
- }
143
- } ) ;
144
144
}
145
145
}
146
146
} ) ;
@@ -177,12 +177,11 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
177
177
} ) ;
178
178
179
179
const htmlAry = await createServer (
180
- api ,
180
+ appContext ,
181
181
ssgRoutes ,
182
182
pageRoutes ,
183
183
apiRoutes ,
184
184
resolvedConfig ,
185
- appDirectory ,
186
185
) ;
187
186
188
187
// write to dist file
0 commit comments