generated from Daydreamer-riri/starter-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
types.ts
220 lines (207 loc) · 6.35 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import type { ReactElement, ReactNode } from 'react'
import type { IndexRouteObject, NonIndexRouteObject, createBrowserRouter } from 'react-router-dom'
import type { Options as BeastiesOptions } from 'beasties'
type Router = ReturnType<typeof createBrowserRouter>
export interface CrittersOptions {
path?: string
publicPath?: string
external?: boolean
inlineThreshold?: number
minimumExternalSize?: number
pruneSource?: boolean
mergeStylesheets?: boolean
additionalStylesheets?: string[]
preload?: 'body' | 'media' | 'swap' | 'js' | 'js-lazy'
noscriptFallback?: boolean
inlineFonts?: boolean
preloadFonts?: boolean
fonts?: boolean
keyframes?: string
compress?: boolean
logLevel?: 'info' | 'warn' | 'error' | 'trace' | 'debug' | 'silent'
reduceInlineStyles?: boolean
// logger?: Logger
}
export interface ViteReactSSGOptions<Context = ViteReactSSGContext> {
/**
* Set the scripts' loading mode. Only works for `type="module"`.
*
* @default 'sync'
*/
script?: 'sync' | 'async' | 'defer' | 'async defer'
/**
* Build format.
*
* @default 'esm'
*/
format?: 'esm' | 'cjs'
/**
* The path of the main entry file (relative to the project root).
*
* @default 'src/main.ts'
*/
entry?: string
/**
* Mock browser global variables (window, document, etc...) from SSG.
*
* @default false
*/
mock?: boolean
/**
* Apply formatter to the generated index file.
*
* **It will cause Hydration Failed.**
*
* @default 'none'
*/
formatting?: 'prettify' | 'none'
/**
* Vite environmeng mode.
*/
mode?: string
/**
* Directory style of the output directory.
*
* flat: `/foo` -> `/foo.html`
* nested: `/foo` -> `/foo/index.html`
*
* @default 'flat'
*/
dirStyle?: 'flat' | 'nested'
/**
* Generate for all routes, including dynamic routes.
* If enabled, you will need to configGure your serve
* manually to handle dynamic routes properly.
*
* @default false
*/
includeAllRoutes?: boolean
/**
* Options for the critters packages.
*
* @deprecated Use `beastiesOptions` instead.
* @see https://github.com/GoogleChromeLabs/critters
*/
crittersOptions?: CrittersOptions | false
/**
* Options for the beasties package.
*
* @see https://github.com/danielroe/beasties
*/
beastiesOptions?: BeastiesOptions | false
/**
* Custom function to modify the routes to do the SSG.
*
* Works only when `includeAllRoutes` is set to false.
*
* Defaults to a handler that filters out all the dynamic routes.
* When passing your custom handler, you should also take care of the dynamic routes yourself.
*/
includedRoutes?: (paths: string[], routes: Readonly<RouteRecord[]>) => Promise<string[]> | string[]
/**
* Callback to be called before every page render.
*
* It can be used to transform the project's `index.html` file before passing it to the renderer.
*
* To do so, you can change the 'index.html' file contents (passed in through the `indexHTML` parameter), and return it.
* The returned value will then be passed to renderer.
*/
onBeforePageRender?: (route: string, indexHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined
/**
* Callback to be called on every rendered page.
*
* It can be used to transform the current route's rendered HTML.
*
* To do so, you can transform the route's rendered HTML (passed in through the `renderedHTML` parameter), and return it.
* The returned value will be used as the HTML of the route.
*/
onPageRendered?: (route: string, renderedHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined
onFinished?: () => Promise<void> | void
/**
* The application's root container `id`.
*
* @default `root`
*/
rootContainerId?: string
/**
* The size of the SSG processing queue.
*
* @default 20
*/
concurrency?: number
}
export interface ViteReactSSGContext<HasRouter extends boolean = true> {
router?: HasRouter extends true ? Router : undefined
routes: HasRouter extends true ? Readonly<RouteRecord[]> : undefined
routerOptions: HasRouter extends true ? RouterOptions : undefined
initialState: Record<string, any>
isClient: boolean
onSSRAppRendered: (cb: Function) => void
triggerOnSSRAppRendered: (route: string, appHTML: string, appCtx: ViteReactSSGContext) => Promise<unknown[]>
transformState?: (state: any) => any
/**
* Current router path on SSG, `undefined` on client side.
*/
routePath?: string
base: string
getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null
app?: HasRouter extends true ? never : ReactNode
routerType: 'remix' | 'single-page'
}
export interface ViteReactSSGClientOptions {
transformState?: (state: any) => any
registerComponents?: boolean
/**
* The application's root container query selector.
*
* @default `#root`
*/
rootContainer?: string | Element
/**
* @deprecated This option is no longer needed
*/
ssrWhenDev?: boolean
getStyleCollector?: (() => StyleCollector | Promise<StyleCollector>) | null
}
interface CommonRouteOptions {
/**
* Used to obtain static resources through manifest
*
* **You are not required to use this field. It is only necessary when "prehydration style loss" occurs.**
*
* @example `src/pages/home.tsx`
*/
entry?: string
/**
* The getStaticPaths() function should return an array of path
* to determine which paths will be pre-rendered by vite-react-ssg.
*
* This function is only valid for dynamic route.
*
* @example () => ['path1', 'path2']
*/
getStaticPaths?: () => string[] | Promise<string[]>
}
export type NonIndexRouteRecord = Omit<NonIndexRouteObject, 'children'> & {
children?: RouteRecord[]
} & CommonRouteOptions
export type IndexRouteRecord = IndexRouteObject & CommonRouteOptions
export type RouteRecord = NonIndexRouteRecord | IndexRouteRecord
export interface RouterOptions {
routes: RouteRecord[]
createFetchRequest?: <T>(req: T) => Request
basename?: string
}
export interface StyleCollector {
collect: (app: ReactElement) => ReactElement
toString: (html: string) => string
cleanup?: () => void
}
// extend vite.config.ts
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore
declare module 'vite' {
interface UserConfig {
ssgOptions?: ViteReactSSGOptions
}
}