vite-react-ssg 0.5.1 → 0.6.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.
@@ -0,0 +1,40 @@
1
+ import React, { useState, useEffect, isValidElement } from 'react';
2
+ import { Helmet } from 'react-helmet-async';
3
+
4
+ function documentReady(_passThrough) {
5
+ if (document.readyState === "loading") {
6
+ return new Promise((resolve) => {
7
+ document.addEventListener("DOMContentLoaded", () => resolve(_passThrough));
8
+ });
9
+ }
10
+ return Promise.resolve(_passThrough);
11
+ }
12
+
13
+ function Head(props) {
14
+ return /* @__PURE__ */ React.createElement(Helmet, { ...props });
15
+ }
16
+
17
+ function useIsClient() {
18
+ const [isBrowser, setIsBrowser] = useState(false);
19
+ useEffect(() => {
20
+ setIsBrowser(true);
21
+ }, []);
22
+ return isBrowser;
23
+ }
24
+
25
+ function ClientOnly({
26
+ children,
27
+ fallback
28
+ }) {
29
+ const isBrowser = useIsClient();
30
+ if (isBrowser) {
31
+ if (typeof children !== "function" && process.env.NODE_ENV === "development") {
32
+ throw new Error(`vite-react-ssg error: The children of <ClientOnly> must be a "render function", e.g. <ClientOnly>{() => <span>{window.location.href}</span>}</ClientOnly>.
33
+ Current type: ${isValidElement(children) ? "React element" : typeof children}`);
34
+ }
35
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children?.());
36
+ }
37
+ return fallback ?? null;
38
+ }
39
+
40
+ export { ClientOnly as C, Head as H, documentReady as d };
@@ -0,0 +1,34 @@
1
+ const UNSAFE_CHARS_REGEXP = /[<>\/\u2028\u2029]/g;
2
+ const ESCAPED_CHARS = {
3
+ "<": "\\u003C",
4
+ ">": "\\u003E",
5
+ "/": "\\u002F",
6
+ "\u2028": "\\u2028",
7
+ "\u2029": "\\u2029"
8
+ };
9
+ function escapeUnsafeChars(unsafeChar) {
10
+ return ESCAPED_CHARS[unsafeChar];
11
+ }
12
+ function serializeState(state) {
13
+ if (state == null || Object.keys(state).length === 0)
14
+ return null;
15
+ try {
16
+ return JSON.stringify(JSON.stringify(state || {})).replace(
17
+ UNSAFE_CHARS_REGEXP,
18
+ escapeUnsafeChars
19
+ );
20
+ } catch (error) {
21
+ console.error("[SSG] On state serialization -", error, state);
22
+ return null;
23
+ }
24
+ }
25
+ function deserializeState(state) {
26
+ try {
27
+ return JSON.parse(state || "{}");
28
+ } catch (error) {
29
+ console.error("[SSG] On state deserialization -", error, state);
30
+ return {};
31
+ }
32
+ }
33
+
34
+ export { deserializeState as d, serializeState as s };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ const React = require('react');
4
+ const reactHelmetAsync = require('react-helmet-async');
5
+
6
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
+
8
+ const React__default = /*#__PURE__*/_interopDefaultCompat(React);
9
+
10
+ function documentReady(_passThrough) {
11
+ if (document.readyState === "loading") {
12
+ return new Promise((resolve) => {
13
+ document.addEventListener("DOMContentLoaded", () => resolve(_passThrough));
14
+ });
15
+ }
16
+ return Promise.resolve(_passThrough);
17
+ }
18
+
19
+ function Head(props) {
20
+ return /* @__PURE__ */ React__default.createElement(reactHelmetAsync.Helmet, { ...props });
21
+ }
22
+
23
+ function useIsClient() {
24
+ const [isBrowser, setIsBrowser] = React.useState(false);
25
+ React.useEffect(() => {
26
+ setIsBrowser(true);
27
+ }, []);
28
+ return isBrowser;
29
+ }
30
+
31
+ function ClientOnly({
32
+ children,
33
+ fallback
34
+ }) {
35
+ const isBrowser = useIsClient();
36
+ if (isBrowser) {
37
+ if (typeof children !== "function" && process.env.NODE_ENV === "development") {
38
+ throw new Error(`vite-react-ssg error: The children of <ClientOnly> must be a "render function", e.g. <ClientOnly>{() => <span>{window.location.href}</span>}</ClientOnly>.
39
+ Current type: ${React.isValidElement(children) ? "React element" : typeof children}`);
40
+ }
41
+ return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, children?.());
42
+ }
43
+ return fallback ?? null;
44
+ }
45
+
46
+ exports.ClientOnly = ClientOnly;
47
+ exports.Head = Head;
48
+ exports.documentReady = documentReady;
@@ -1,5 +1,5 @@
1
1
  import { Options } from 'critters';
2
- import { ReactElement } from 'react';
2
+ import { ReactNode, ReactElement } from 'react';
3
3
  import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
4
 
5
5
  type Router = ReturnType<typeof createBrowserRouter>;
@@ -107,7 +107,7 @@ interface ViteReactSSGOptions {
107
107
  interface ViteReactSSGContext<HasRouter extends boolean = true> {
108
108
  router?: HasRouter extends true ? Router : undefined;
109
109
  routes: HasRouter extends true ? Readonly<RouteRecord[]> : undefined;
110
- routerOptions: RouterOptions;
110
+ routerOptions: HasRouter extends true ? RouterOptions : undefined;
111
111
  initialState: Record<string, any>;
112
112
  isClient: boolean;
113
113
  onSSRAppRendered(cb: Function): void;
@@ -119,6 +119,7 @@ interface ViteReactSSGContext<HasRouter extends boolean = true> {
119
119
  routePath?: string;
120
120
  base: string;
121
121
  getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
122
+ app?: HasRouter extends true ? never : ReactNode;
122
123
  }
123
124
  interface ViteReactSSGClientOptions {
124
125
  transformState?: (state: any) => any;
@@ -130,11 +131,7 @@ interface ViteReactSSGClientOptions {
130
131
  */
131
132
  rootContainer?: string | Element;
132
133
  /**
133
- * Use SSR during development.
134
- *
135
- * If false, the 'dev' script in 'package.json' needs to be set to `"vite"`.
136
- *
137
- * @default true
134
+ * @deprecated This option is no longer needed
138
135
  */
139
136
  ssrWhenDev?: boolean;
140
137
  getStyleCollector?: (() => StyleCollector | Promise<StyleCollector>) | null;
@@ -164,6 +161,7 @@ type RouteRecord = NonIndexRouteRecord | IndexRouteRecord;
164
161
  interface RouterOptions {
165
162
  routes: RouteRecord[];
166
163
  createFetchRequest?: <T>(req: T) => Request;
164
+ basename?: string;
167
165
  }
168
166
  interface StyleCollector {
169
167
  collect: (app: ReactElement) => ReactElement;
@@ -176,4 +174,4 @@ declare module 'vite' {
176
174
  }
177
175
  }
178
176
 
179
- 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 };
177
+ export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
@@ -1,5 +1,5 @@
1
1
  import { Options } from 'critters';
2
- import { ReactElement } from 'react';
2
+ import { ReactNode, ReactElement } from 'react';
3
3
  import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
4
 
5
5
  type Router = ReturnType<typeof createBrowserRouter>;
@@ -107,7 +107,7 @@ interface ViteReactSSGOptions {
107
107
  interface ViteReactSSGContext<HasRouter extends boolean = true> {
108
108
  router?: HasRouter extends true ? Router : undefined;
109
109
  routes: HasRouter extends true ? Readonly<RouteRecord[]> : undefined;
110
- routerOptions: RouterOptions;
110
+ routerOptions: HasRouter extends true ? RouterOptions : undefined;
111
111
  initialState: Record<string, any>;
112
112
  isClient: boolean;
113
113
  onSSRAppRendered(cb: Function): void;
@@ -119,6 +119,7 @@ interface ViteReactSSGContext<HasRouter extends boolean = true> {
119
119
  routePath?: string;
120
120
  base: string;
121
121
  getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
122
+ app?: HasRouter extends true ? never : ReactNode;
122
123
  }
123
124
  interface ViteReactSSGClientOptions {
124
125
  transformState?: (state: any) => any;
@@ -130,11 +131,7 @@ interface ViteReactSSGClientOptions {
130
131
  */
131
132
  rootContainer?: string | Element;
132
133
  /**
133
- * Use SSR during development.
134
- *
135
- * If false, the 'dev' script in 'package.json' needs to be set to `"vite"`.
136
- *
137
- * @default true
134
+ * @deprecated This option is no longer needed
138
135
  */
139
136
  ssrWhenDev?: boolean;
140
137
  getStyleCollector?: (() => StyleCollector | Promise<StyleCollector>) | null;
@@ -164,6 +161,7 @@ type RouteRecord = NonIndexRouteRecord | IndexRouteRecord;
164
161
  interface RouterOptions {
165
162
  routes: RouteRecord[];
166
163
  createFetchRequest?: <T>(req: T) => Request;
164
+ basename?: string;
167
165
  }
168
166
  interface StyleCollector {
169
167
  collect: (app: ReactElement) => ReactElement;
@@ -176,4 +174,4 @@ declare module 'vite' {
176
174
  }
177
175
  }
178
176
 
179
- 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 };
177
+ export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
@@ -1,5 +1,5 @@
1
1
  import { Options } from 'critters';
2
- import { ReactElement } from 'react';
2
+ import { ReactNode, ReactElement } from 'react';
3
3
  import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
4
 
5
5
  type Router = ReturnType<typeof createBrowserRouter>;
@@ -107,7 +107,7 @@ interface ViteReactSSGOptions {
107
107
  interface ViteReactSSGContext<HasRouter extends boolean = true> {
108
108
  router?: HasRouter extends true ? Router : undefined;
109
109
  routes: HasRouter extends true ? Readonly<RouteRecord[]> : undefined;
110
- routerOptions: RouterOptions;
110
+ routerOptions: HasRouter extends true ? RouterOptions : undefined;
111
111
  initialState: Record<string, any>;
112
112
  isClient: boolean;
113
113
  onSSRAppRendered(cb: Function): void;
@@ -119,6 +119,7 @@ interface ViteReactSSGContext<HasRouter extends boolean = true> {
119
119
  routePath?: string;
120
120
  base: string;
121
121
  getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
122
+ app?: HasRouter extends true ? never : ReactNode;
122
123
  }
123
124
  interface ViteReactSSGClientOptions {
124
125
  transformState?: (state: any) => any;
@@ -130,11 +131,7 @@ interface ViteReactSSGClientOptions {
130
131
  */
131
132
  rootContainer?: string | Element;
132
133
  /**
133
- * Use SSR during development.
134
- *
135
- * If false, the 'dev' script in 'package.json' needs to be set to `"vite"`.
136
- *
137
- * @default true
134
+ * @deprecated This option is no longer needed
138
135
  */
139
136
  ssrWhenDev?: boolean;
140
137
  getStyleCollector?: (() => StyleCollector | Promise<StyleCollector>) | null;
@@ -164,6 +161,7 @@ type RouteRecord = NonIndexRouteRecord | IndexRouteRecord;
164
161
  interface RouterOptions {
165
162
  routes: RouteRecord[];
166
163
  createFetchRequest?: <T>(req: T) => Request;
164
+ basename?: string;
167
165
  }
168
166
  interface StyleCollector {
169
167
  collect: (app: ReactElement) => ReactElement;
@@ -176,4 +174,4 @@ declare module 'vite' {
176
174
  }
177
175
  }
178
176
 
179
- 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 };
177
+ export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
@@ -1,5 +1,4 @@
1
- import React from 'react';
2
- import { Helmet } from 'react-helmet-async';
1
+ 'use strict';
3
2
 
4
3
  const UNSAFE_CHARS_REGEXP = /[<>\/\u2028\u2029]/g;
5
4
  const ESCAPED_CHARS = {
@@ -34,12 +33,5 @@ function deserializeState(state) {
34
33
  }
35
34
  }
36
35
 
37
- function Head(props) {
38
- return /* @__PURE__ */ React.createElement(Helmet, { ...props });
39
- }
40
-
41
- function SiteMetadataDefaults() {
42
- return /* @__PURE__ */ React.createElement(Head, null, /* @__PURE__ */ React.createElement("html", null), /* @__PURE__ */ React.createElement("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0" }));
43
- }
44
-
45
- export { Head as H, SiteMetadataDefaults as S, deserializeState as d, serializeState as s };
36
+ exports.deserializeState = deserializeState;
37
+ exports.serializeState = serializeState;
@@ -0,0 +1,15 @@
1
+ import { ReactNode } from 'react';
2
+ import { HelmetProps } from 'react-helmet-async';
3
+
4
+ type Props = HelmetProps & {
5
+ children: ReactNode;
6
+ };
7
+ declare function Head(props: Props): JSX.Element;
8
+
9
+ interface ClientOnlyProps {
10
+ children?: () => JSX.Element;
11
+ fallback?: JSX.Element;
12
+ }
13
+ declare function ClientOnly({ children, fallback, }: ClientOnlyProps): JSX.Element | null;
14
+
15
+ export { ClientOnly as C, Head as H };
@@ -0,0 +1,15 @@
1
+ import { ReactNode } from 'react';
2
+ import { HelmetProps } from 'react-helmet-async';
3
+
4
+ type Props = HelmetProps & {
5
+ children: ReactNode;
6
+ };
7
+ declare function Head(props: Props): JSX.Element;
8
+
9
+ interface ClientOnlyProps {
10
+ children?: () => JSX.Element;
11
+ fallback?: JSX.Element;
12
+ }
13
+ declare function ClientOnly({ children, fallback, }: ClientOnlyProps): JSX.Element | null;
14
+
15
+ export { ClientOnly as C, Head as H };
@@ -0,0 +1,15 @@
1
+ import { ReactNode } from 'react';
2
+ import { HelmetProps } from 'react-helmet-async';
3
+
4
+ type Props = HelmetProps & {
5
+ children: ReactNode;
6
+ };
7
+ declare function Head(props: Props): JSX.Element;
8
+
9
+ interface ClientOnlyProps {
10
+ children?: () => JSX.Element;
11
+ fallback?: JSX.Element;
12
+ }
13
+ declare function ClientOnly({ children, fallback, }: ClientOnlyProps): JSX.Element | null;
14
+
15
+ export { ClientOnly as C, Head as H };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-react-ssg",
3
3
  "type": "module",
4
- "version": "0.5.1",
4
+ "version": "0.6.0",
5
5
  "packageManager": "pnpm@8.6.6",
6
6
  "description": "",
7
7
  "author": "Riri <Daydreamerriri@outlook.com>",
@@ -31,6 +31,11 @@
31
31
  "import": "./dist/node.mjs",
32
32
  "require": "./dist/node.cjs"
33
33
  },
34
+ "./single-page": {
35
+ "types": "./dist/client/single-page.d.ts",
36
+ "import": "./dist/client/single-page.mjs",
37
+ "require": "./dist/client/single-page.cjs"
38
+ },
34
39
  "./style-collectors/styled-components": {
35
40
  "types": "./dist/style-collectors/styled-components.d.ts",
36
41
  "import": "./dist/style-collectors/styled-components.mjs",
@@ -45,6 +50,9 @@
45
50
  "node": [
46
51
  "./dist/node.d.ts"
47
52
  ],
53
+ "single-page": [
54
+ "dist/client/single-page.d.ts"
55
+ ],
48
56
  "style-collectors/styled-components": [
49
57
  "./dist/style-collectors/styled-components.d.ts"
50
58
  ]
@@ -69,6 +77,7 @@
69
77
  "typecheck": "tsc --noEmit"
70
78
  },
71
79
  "peerDependencies": {
80
+ "@childrentime/devcert": "*",
72
81
  "critters": "^0.0.19",
73
82
  "react": "^18.0.0",
74
83
  "react-dom": "^18.0.0",
@@ -77,6 +86,9 @@
77
86
  "vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0"
78
87
  },
79
88
  "peerDependenciesMeta": {
89
+ "@childrentime/devcert": {
90
+ "optional": true
91
+ },
80
92
  "critters": {
81
93
  "optional": true
82
94
  },
@@ -1,54 +0,0 @@
1
- 'use strict';
2
-
3
- const React = require('react');
4
- const reactHelmetAsync = require('react-helmet-async');
5
-
6
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
-
8
- const React__default = /*#__PURE__*/_interopDefaultCompat(React);
9
-
10
- const UNSAFE_CHARS_REGEXP = /[<>\/\u2028\u2029]/g;
11
- const ESCAPED_CHARS = {
12
- "<": "\\u003C",
13
- ">": "\\u003E",
14
- "/": "\\u002F",
15
- "\u2028": "\\u2028",
16
- "\u2029": "\\u2029"
17
- };
18
- function escapeUnsafeChars(unsafeChar) {
19
- return ESCAPED_CHARS[unsafeChar];
20
- }
21
- function serializeState(state) {
22
- if (state == null || Object.keys(state).length === 0)
23
- return null;
24
- try {
25
- return JSON.stringify(JSON.stringify(state || {})).replace(
26
- UNSAFE_CHARS_REGEXP,
27
- escapeUnsafeChars
28
- );
29
- } catch (error) {
30
- console.error("[SSG] On state serialization -", error, state);
31
- return null;
32
- }
33
- }
34
- function deserializeState(state) {
35
- try {
36
- return JSON.parse(state || "{}");
37
- } catch (error) {
38
- console.error("[SSG] On state deserialization -", error, state);
39
- return {};
40
- }
41
- }
42
-
43
- function Head(props) {
44
- return /* @__PURE__ */ React__default.createElement(reactHelmetAsync.Helmet, { ...props });
45
- }
46
-
47
- function SiteMetadataDefaults() {
48
- return /* @__PURE__ */ React__default.createElement(Head, null, /* @__PURE__ */ React__default.createElement("html", null), /* @__PURE__ */ React__default.createElement("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0" }));
49
- }
50
-
51
- exports.Head = Head;
52
- exports.SiteMetadataDefaults = SiteMetadataDefaults;
53
- exports.deserializeState = deserializeState;
54
- exports.serializeState = serializeState;