vite-react-ssg 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -175,4 +175,4 @@ declare module 'vite' {
175
175
  }
176
176
  }
177
177
 
178
- export { IndexRouteRecord as I, NonIndexRouteRecord as N, RouterOptions as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouteRecord as c };
178
+ export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouterOptions as R, StyleCollector as S, ViteReactSSGOptions as V, ViteReactSSGContext as a, ViteReactSSGClientOptions as b, RouteRecord as c };
@@ -0,0 +1,178 @@
1
+ import { Options } from 'critters';
2
+ import { ReactElement } from 'react';
3
+ import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
+
5
+ type Router = ReturnType<typeof createBrowserRouter>;
6
+ interface ViteReactSSGOptions {
7
+ /**
8
+ * Set the scripts' loading mode. Only works for `type="module"`.
9
+ *
10
+ * @default 'sync'
11
+ */
12
+ script?: 'sync' | 'async' | 'defer' | 'async defer';
13
+ /**
14
+ * Build format.
15
+ *
16
+ * @default 'esm'
17
+ */
18
+ format?: 'esm' | 'cjs';
19
+ /**
20
+ * The path of the main entry file (relative to the project root).
21
+ *
22
+ * @default 'src/main.ts'
23
+ */
24
+ entry?: string;
25
+ /**
26
+ * Mock browser global variables (window, document, etc...) from SSG.
27
+ *
28
+ * @default false
29
+ */
30
+ mock?: boolean;
31
+ /**
32
+ * Apply formatter to the generated index file.
33
+ *
34
+ * **It will cause Hydration Failed.**
35
+ *
36
+ * @default 'none'
37
+ */
38
+ formatting?: 'minify' | 'prettify' | 'none';
39
+ /**
40
+ * Vite environmeng mode.
41
+ */
42
+ mode?: string;
43
+ /**
44
+ * Directory style of the output directory.
45
+ *
46
+ * flat: `/foo` -> `/foo.html`
47
+ * nested: `/foo` -> `/foo/index.html`
48
+ *
49
+ * @default 'flat'
50
+ */
51
+ dirStyle?: 'flat' | 'nested';
52
+ /**
53
+ * Generate for all routes, including dynamic routes.
54
+ * If enabled, you will need to configGure your serve
55
+ * manually to handle dynamic routes properly.
56
+ *
57
+ * @default false
58
+ */
59
+ includeAllRoutes?: boolean;
60
+ /**
61
+ * Options for the critters packages.
62
+ *
63
+ * @see https://github.com/GoogleChromeLabs/critters
64
+ */
65
+ crittersOptions?: Options | false;
66
+ /**
67
+ * Custom function to modify the routes to do the SSG.
68
+ *
69
+ * Works only when `includeAllRoutes` is set to false.
70
+ *
71
+ * Defaults to a handler that filters out all the dynamic routes.
72
+ * When passing your custom handler, you should also take care of the dynamic routes yourself.
73
+ */
74
+ includedRoutes?: (paths: string[], routes: Readonly<RouteRecord[]>) => Promise<string[]> | string[];
75
+ /**
76
+ * Callback to be called before every page render.
77
+ *
78
+ * It can be used to transform the project's `index.html` file before passing it to the renderer.
79
+ *
80
+ * To do so, you can change the 'index.html' file contents (passed in through the `indexHTML` parameter), and return it.
81
+ * The returned value will then be passed to renderer.
82
+ */
83
+ onBeforePageRender?: (route: string, indexHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
84
+ /**
85
+ * Callback to be called on every rendered page.
86
+ *
87
+ * It can be used to transform the current route's rendered HTML.
88
+ *
89
+ * To do so, you can transform the route's rendered HTML (passed in through the `renderedHTML` parameter), and return it.
90
+ * The returned value will be used as the HTML of the route.
91
+ */
92
+ onPageRendered?: (route: string, renderedHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
93
+ onFinished?: () => Promise<void> | void;
94
+ /**
95
+ * The application's root container `id`.
96
+ *
97
+ * @default `root`
98
+ */
99
+ rootContainerId?: string;
100
+ /**
101
+ * The size of the SSG processing queue.
102
+ *
103
+ * @default 20
104
+ */
105
+ concurrency?: number;
106
+ }
107
+ interface ViteReactSSGContext<HasRouter extends boolean = true> {
108
+ router?: HasRouter extends true ? Router : undefined;
109
+ routes: HasRouter extends true ? Readonly<RouteRecord[]> : undefined;
110
+ routerOptions: RouterOptions;
111
+ initialState: Record<string, any>;
112
+ isClient: boolean;
113
+ onSSRAppRendered(cb: Function): void;
114
+ triggerOnSSRAppRendered(route: string, appHTML: string, appCtx: ViteReactSSGContext): Promise<unknown[]>;
115
+ transformState?(state: any): any;
116
+ /**
117
+ * Current router path on SSG, `undefined` on client side.
118
+ */
119
+ routePath?: string;
120
+ getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
121
+ }
122
+ interface ViteReactSSGClientOptions {
123
+ transformState?: (state: any) => any;
124
+ registerComponents?: boolean;
125
+ /**
126
+ * The application's root container query selector.
127
+ *
128
+ * @default `#root`
129
+ */
130
+ rootContainer?: string | Element;
131
+ /**
132
+ * Use SSR during development.
133
+ *
134
+ * If false, the 'dev' script in 'package.json' needs to be set to `"vite"`.
135
+ *
136
+ * @default true
137
+ */
138
+ ssrWhenDev?: boolean;
139
+ getStyleCollector?: (() => StyleCollector | Promise<StyleCollector>) | null;
140
+ }
141
+ interface CommonRouteOptions {
142
+ /**
143
+ * Used to obtain static resources through manifest
144
+ *
145
+ * @example `src/pages/home.tsx`
146
+ */
147
+ entry?: string;
148
+ /**
149
+ * The getStaticPaths() function should return an array of path
150
+ * to determine which paths will be pre-rendered by vite-react-ssg.
151
+ *
152
+ * This function is only valid for dynamic route.
153
+ *
154
+ * @example () => ['path1', 'path2']
155
+ */
156
+ getStaticPaths?: () => string[] | Promise<string[]>;
157
+ }
158
+ type NonIndexRouteRecord = Omit<NonIndexRouteObject, 'children'> & {
159
+ children?: RouteRecord[];
160
+ } & CommonRouteOptions;
161
+ type IndexRouteRecord = IndexRouteObject & CommonRouteOptions;
162
+ type RouteRecord = NonIndexRouteRecord | IndexRouteRecord;
163
+ interface RouterOptions {
164
+ routes: RouteRecord[];
165
+ createFetchRequest?: <T>(req: T) => Request;
166
+ }
167
+ interface StyleCollector {
168
+ collect: (app: ReactElement) => ReactElement;
169
+ toString: (html: string) => string;
170
+ cleanup?: () => void;
171
+ }
172
+ declare module 'vite' {
173
+ interface UserConfig {
174
+ ssgOptions?: ViteReactSSGOptions;
175
+ }
176
+ }
177
+
178
+ export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouterOptions as R, StyleCollector as S, ViteReactSSGOptions as V, ViteReactSSGContext as a, ViteReactSSGClientOptions as b, RouteRecord as c };
@@ -0,0 +1,178 @@
1
+ import { Options } from 'critters';
2
+ import { ReactElement } from 'react';
3
+ import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
+
5
+ type Router = ReturnType<typeof createBrowserRouter>;
6
+ interface ViteReactSSGOptions {
7
+ /**
8
+ * Set the scripts' loading mode. Only works for `type="module"`.
9
+ *
10
+ * @default 'sync'
11
+ */
12
+ script?: 'sync' | 'async' | 'defer' | 'async defer';
13
+ /**
14
+ * Build format.
15
+ *
16
+ * @default 'esm'
17
+ */
18
+ format?: 'esm' | 'cjs';
19
+ /**
20
+ * The path of the main entry file (relative to the project root).
21
+ *
22
+ * @default 'src/main.ts'
23
+ */
24
+ entry?: string;
25
+ /**
26
+ * Mock browser global variables (window, document, etc...) from SSG.
27
+ *
28
+ * @default false
29
+ */
30
+ mock?: boolean;
31
+ /**
32
+ * Apply formatter to the generated index file.
33
+ *
34
+ * **It will cause Hydration Failed.**
35
+ *
36
+ * @default 'none'
37
+ */
38
+ formatting?: 'minify' | 'prettify' | 'none';
39
+ /**
40
+ * Vite environmeng mode.
41
+ */
42
+ mode?: string;
43
+ /**
44
+ * Directory style of the output directory.
45
+ *
46
+ * flat: `/foo` -> `/foo.html`
47
+ * nested: `/foo` -> `/foo/index.html`
48
+ *
49
+ * @default 'flat'
50
+ */
51
+ dirStyle?: 'flat' | 'nested';
52
+ /**
53
+ * Generate for all routes, including dynamic routes.
54
+ * If enabled, you will need to configGure your serve
55
+ * manually to handle dynamic routes properly.
56
+ *
57
+ * @default false
58
+ */
59
+ includeAllRoutes?: boolean;
60
+ /**
61
+ * Options for the critters packages.
62
+ *
63
+ * @see https://github.com/GoogleChromeLabs/critters
64
+ */
65
+ crittersOptions?: Options | false;
66
+ /**
67
+ * Custom function to modify the routes to do the SSG.
68
+ *
69
+ * Works only when `includeAllRoutes` is set to false.
70
+ *
71
+ * Defaults to a handler that filters out all the dynamic routes.
72
+ * When passing your custom handler, you should also take care of the dynamic routes yourself.
73
+ */
74
+ includedRoutes?: (paths: string[], routes: Readonly<RouteRecord[]>) => Promise<string[]> | string[];
75
+ /**
76
+ * Callback to be called before every page render.
77
+ *
78
+ * It can be used to transform the project's `index.html` file before passing it to the renderer.
79
+ *
80
+ * To do so, you can change the 'index.html' file contents (passed in through the `indexHTML` parameter), and return it.
81
+ * The returned value will then be passed to renderer.
82
+ */
83
+ onBeforePageRender?: (route: string, indexHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
84
+ /**
85
+ * Callback to be called on every rendered page.
86
+ *
87
+ * It can be used to transform the current route's rendered HTML.
88
+ *
89
+ * To do so, you can transform the route's rendered HTML (passed in through the `renderedHTML` parameter), and return it.
90
+ * The returned value will be used as the HTML of the route.
91
+ */
92
+ onPageRendered?: (route: string, renderedHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
93
+ onFinished?: () => Promise<void> | void;
94
+ /**
95
+ * The application's root container `id`.
96
+ *
97
+ * @default `root`
98
+ */
99
+ rootContainerId?: string;
100
+ /**
101
+ * The size of the SSG processing queue.
102
+ *
103
+ * @default 20
104
+ */
105
+ concurrency?: number;
106
+ }
107
+ interface ViteReactSSGContext<HasRouter extends boolean = true> {
108
+ router?: HasRouter extends true ? Router : undefined;
109
+ routes: HasRouter extends true ? Readonly<RouteRecord[]> : undefined;
110
+ routerOptions: RouterOptions;
111
+ initialState: Record<string, any>;
112
+ isClient: boolean;
113
+ onSSRAppRendered(cb: Function): void;
114
+ triggerOnSSRAppRendered(route: string, appHTML: string, appCtx: ViteReactSSGContext): Promise<unknown[]>;
115
+ transformState?(state: any): any;
116
+ /**
117
+ * Current router path on SSG, `undefined` on client side.
118
+ */
119
+ routePath?: string;
120
+ getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
121
+ }
122
+ interface ViteReactSSGClientOptions {
123
+ transformState?: (state: any) => any;
124
+ registerComponents?: boolean;
125
+ /**
126
+ * The application's root container query selector.
127
+ *
128
+ * @default `#root`
129
+ */
130
+ rootContainer?: string | Element;
131
+ /**
132
+ * Use SSR during development.
133
+ *
134
+ * If false, the 'dev' script in 'package.json' needs to be set to `"vite"`.
135
+ *
136
+ * @default true
137
+ */
138
+ ssrWhenDev?: boolean;
139
+ getStyleCollector?: (() => StyleCollector | Promise<StyleCollector>) | null;
140
+ }
141
+ interface CommonRouteOptions {
142
+ /**
143
+ * Used to obtain static resources through manifest
144
+ *
145
+ * @example `src/pages/home.tsx`
146
+ */
147
+ entry?: string;
148
+ /**
149
+ * The getStaticPaths() function should return an array of path
150
+ * to determine which paths will be pre-rendered by vite-react-ssg.
151
+ *
152
+ * This function is only valid for dynamic route.
153
+ *
154
+ * @example () => ['path1', 'path2']
155
+ */
156
+ getStaticPaths?: () => string[] | Promise<string[]>;
157
+ }
158
+ type NonIndexRouteRecord = Omit<NonIndexRouteObject, 'children'> & {
159
+ children?: RouteRecord[];
160
+ } & CommonRouteOptions;
161
+ type IndexRouteRecord = IndexRouteObject & CommonRouteOptions;
162
+ type RouteRecord = NonIndexRouteRecord | IndexRouteRecord;
163
+ interface RouterOptions {
164
+ routes: RouteRecord[];
165
+ createFetchRequest?: <T>(req: T) => Request;
166
+ }
167
+ interface StyleCollector {
168
+ collect: (app: ReactElement) => ReactElement;
169
+ toString: (html: string) => string;
170
+ cleanup?: () => void;
171
+ }
172
+ declare module 'vite' {
173
+ interface UserConfig {
174
+ ssgOptions?: ViteReactSSGOptions;
175
+ }
176
+ }
177
+
178
+ export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouterOptions as R, StyleCollector as S, ViteReactSSGOptions as V, ViteReactSSGContext as a, ViteReactSSGClientOptions as b, RouteRecord as c };
@@ -0,0 +1,9 @@
1
+ import { ReactElement } from 'react';
2
+
3
+ declare function ssrCollector(): Promise<{
4
+ collect(app: ReactElement): JSX.Element;
5
+ toString(): string;
6
+ cleanup(): void;
7
+ }>;
8
+
9
+ export { ssrCollector as default };
@@ -0,0 +1,9 @@
1
+ import { ReactElement } from 'react';
2
+
3
+ declare function ssrCollector(): Promise<{
4
+ collect(app: ReactElement): JSX.Element;
5
+ toString(): string;
6
+ cleanup(): void;
7
+ }>;
8
+
9
+ export { ssrCollector as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-react-ssg",
3
3
  "type": "module",
4
- "version": "0.3.3",
4
+ "version": "0.4.0",
5
5
  "packageManager": "pnpm@8.6.6",
6
6
  "description": "",
7
7
  "author": "Riri <Daydreamerriri@outlook.com>",
@@ -23,18 +23,18 @@
23
23
  "exports": {
24
24
  ".": {
25
25
  "types": "./dist/index.d.ts",
26
- "require": "./dist/index.cjs",
27
- "import": "./dist/index.mjs"
26
+ "import": "./dist/index.mjs",
27
+ "require": "./dist/index.cjs"
28
28
  },
29
29
  "./node": {
30
30
  "types": "./dist/node.d.ts",
31
- "require": "./dist/node.cjs",
32
- "import": "./dist/node.mjs"
31
+ "import": "./dist/node.mjs",
32
+ "require": "./dist/node.cjs"
33
33
  },
34
34
  "./style-collectors/styled-components": {
35
35
  "types": "./dist/style-collectors/styled-components.d.ts",
36
- "require": "./dist/style-collectors/styled-components.cjs",
37
- "import": "./dist/style-collectors/styled-components.mjs"
36
+ "import": "./dist/style-collectors/styled-components.mjs",
37
+ "require": "./dist/style-collectors/styled-components.cjs"
38
38
  }
39
39
  },
40
40
  "main": "./dist/index.mjs",
@@ -88,7 +88,7 @@
88
88
  }
89
89
  },
90
90
  "dependencies": {
91
- "@remix-run/web-fetch": "^4.3.5",
91
+ "@remix-run/web-fetch": "^4.3.8",
92
92
  "express": "^4.18.2",
93
93
  "fs-extra": "^11.1.1",
94
94
  "html-minifier": "^4.0.0",
@@ -100,13 +100,13 @@
100
100
  "yargs": "^17.7.2"
101
101
  },
102
102
  "devDependencies": {
103
- "@ririd/eslint-config": "0.6.0",
103
+ "@childrentime/devcert": "^1.2.5",
104
+ "@ririd/eslint-config": "0.7.0",
104
105
  "@types/express": "^4.17.17",
105
106
  "@types/fs-extra": "^11.0.1",
106
107
  "@types/html-minifier": "^4.0.2",
107
108
  "@types/jsdom": "^21.1.1",
108
109
  "@types/node": "^18.15.11",
109
- "@types/prettier": "^2.7.3",
110
110
  "@types/react": "^18.2.14",
111
111
  "@types/react-dom": "^18.2.6",
112
112
  "@types/yargs": "^17.0.24",
@@ -115,14 +115,14 @@
115
115
  "eslint": "^8.45.0",
116
116
  "esno": "^0.17.0",
117
117
  "fast-glob": "3.3.1",
118
- "p-queue": "^7.3.4",
118
+ "p-queue": "^7.4.1",
119
119
  "react": "^18.2.0",
120
120
  "react-dom": "^18.2.0",
121
- "react-router-dom": "^6.14.1",
121
+ "react-router-dom": "^6.15.0",
122
122
  "rimraf": "5.0.1",
123
123
  "styled-components": "6.0.5",
124
124
  "typescript": "5.1.6",
125
- "unbuild": "^1.2.1",
125
+ "unbuild": "^2.0.0",
126
126
  "vite": "^4.4.9",
127
127
  "vite-plugin-pwa": "^0.16.4",
128
128
  "vitest": "0.34.1"