vite-react-ssg 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +132 -0
- package/bin/vite-react-ssg.js +3 -0
- package/dist/chunks/jsdomGlobal.cjs +90 -0
- package/dist/chunks/jsdomGlobal.mjs +84 -0
- package/dist/index.cjs +74 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.mjs +68 -0
- package/dist/node/cli.cjs +50 -0
- package/dist/node/cli.d.ts +2 -0
- package/dist/node/cli.mjs +44 -0
- package/dist/node.cjs +19 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.mjs +13 -0
- package/dist/shared/vite-react-ssg.1128b41a.cjs +1226 -0
- package/dist/shared/vite-react-ssg.4ca822c0.cjs +54 -0
- package/dist/shared/vite-react-ssg.9d005d5e.mjs +45 -0
- package/dist/shared/vite-react-ssg.c2c344c3.mjs +1219 -0
- package/dist/types-fa5fe9c2.d.ts +141 -0
- package/package.json +112 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Riri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Vite React SSG
|
|
2
|
+
|
|
3
|
+
Static-site generation for React on Vite.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/vite-react-ssg)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
> **This library requires Node.js version >= 17**
|
|
10
|
+
> or `Request` is available
|
|
11
|
+
<pre>
|
|
12
|
+
<b>npm i -D vite-react-ssg</b> <em>react-router-dom</em>
|
|
13
|
+
</pre>
|
|
14
|
+
|
|
15
|
+
```diff
|
|
16
|
+
// package.json
|
|
17
|
+
{
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "vite",
|
|
20
|
+
- "build": "vite build"
|
|
21
|
+
+ "build": "vite-react-ssg build"
|
|
22
|
+
|
|
23
|
+
// OR if you want to use another vite config file
|
|
24
|
+
+ "build": "vite-react-ssg build -c another-vite.config.ts"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// src/main.ts
|
|
31
|
+
import { ViteReactSSG } from 'vite-react-ssg'
|
|
32
|
+
import routes from './App.tsx'
|
|
33
|
+
|
|
34
|
+
export const createApp = ViteReactSSG(
|
|
35
|
+
// react-router-dom data routes
|
|
36
|
+
{ routes },
|
|
37
|
+
// function to have custom setups
|
|
38
|
+
({ router, routes, isClient, initialState }) => {
|
|
39
|
+
// do something.
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Critical CSS
|
|
45
|
+
|
|
46
|
+
Vite SSG has built-in support for generating [Critical CSS](https://web.dev/extract-critical-css/) inlined in the HTML via the [`critters`](https://github.com/GoogleChromeLabs/critters) package.
|
|
47
|
+
Install it with:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm i -D critters
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Critical CSS generation will automatically be enabled for you.
|
|
54
|
+
|
|
55
|
+
To configure `critters`, pass [its options](https://github.com/GoogleChromeLabs/critters#usage) into `ssgOptions.crittersOptions` in `vite.config.ts`:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// vite.config.ts
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
ssgOptions: {
|
|
61
|
+
crittersOptions: {
|
|
62
|
+
// E.g., change the preload strategy
|
|
63
|
+
preload: 'media',
|
|
64
|
+
// Other options: https://github.com/GoogleChromeLabs/critters#usage
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Configuration
|
|
71
|
+
|
|
72
|
+
You can pass options to Vite SSG in the `ssgOptions` field of your `vite.config.js`
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
// vite.config.js
|
|
76
|
+
|
|
77
|
+
export default {
|
|
78
|
+
plugins: [],
|
|
79
|
+
ssgOptions: {
|
|
80
|
+
script: 'async',
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
See [src/types.ts](./src/types.ts). for more options available.
|
|
86
|
+
|
|
87
|
+
### Custom Routes to Render
|
|
88
|
+
|
|
89
|
+
You can use the `includedRoutes` hook to include or exclude route paths to render, or even provide some completely custom ones.
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
// vite.config.js
|
|
93
|
+
|
|
94
|
+
export default {
|
|
95
|
+
plugins: [],
|
|
96
|
+
ssgOptions: {
|
|
97
|
+
includedRoutes(paths, routes) {
|
|
98
|
+
// exclude all the route paths that contains 'foo'
|
|
99
|
+
return paths.filter(i => !i.includes('foo'))
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
```js
|
|
105
|
+
// vite.config.js
|
|
106
|
+
|
|
107
|
+
export default {
|
|
108
|
+
plugins: [],
|
|
109
|
+
ssgOptions: {
|
|
110
|
+
includedRoutes(paths, routes) {
|
|
111
|
+
// use original route records
|
|
112
|
+
return routes.flatMap((route) => {
|
|
113
|
+
return route.name === 'Blog'
|
|
114
|
+
? myBlogSlugs.map(slug => `/blog/${slug}`)
|
|
115
|
+
: route.path
|
|
116
|
+
})
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Roadmap
|
|
123
|
+
|
|
124
|
+
- [ ] SSR under dev
|
|
125
|
+
- [ ] Preload assets
|
|
126
|
+
- [ ] Initial State
|
|
127
|
+
- [ ] Document head
|
|
128
|
+
- [ ] More Client components, such as `<ClientOnly />`
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
[MIT](./LICENSE) License © 2023 [Riri](https://github.com/Daydreamer-riri)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const JSDOM = require('jsdom');
|
|
4
|
+
|
|
5
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
6
|
+
|
|
7
|
+
const JSDOM__default = /*#__PURE__*/_interopDefaultCompat(JSDOM);
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
MIT License
|
|
11
|
+
|
|
12
|
+
Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
|
|
13
|
+
jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
|
|
14
|
+
|
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
16
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
17
|
+
in the Software without restriction, including without limitation the rights
|
|
18
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
20
|
+
furnished to do so, subject to the following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included in all
|
|
23
|
+
copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
|
|
36
|
+
|
|
37
|
+
// define this here so that we only ever dynamically populate KEYS once.
|
|
38
|
+
|
|
39
|
+
const KEYS = [];
|
|
40
|
+
|
|
41
|
+
function jsdomGlobal(html = defaultHtml, options = {}) {
|
|
42
|
+
// Idempotency
|
|
43
|
+
if (global.navigator
|
|
44
|
+
&& global.navigator.userAgent
|
|
45
|
+
&& global.navigator.userAgent.includes('Node.js')
|
|
46
|
+
&& global.document
|
|
47
|
+
&& typeof global.document.destroy === 'function')
|
|
48
|
+
return global.document.destroy
|
|
49
|
+
|
|
50
|
+
// set a default url if we don't get one - otherwise things explode when we copy localstorage keys
|
|
51
|
+
if (!('url' in options))
|
|
52
|
+
Object.assign(options, { url: 'http://localhost:3000' });
|
|
53
|
+
|
|
54
|
+
// enable pretendToBeVisual by default since react needs
|
|
55
|
+
// window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
|
|
56
|
+
if (!('pretendToBeVisual' in options))
|
|
57
|
+
Object.assign(options, { pretendToBeVisual: true });
|
|
58
|
+
|
|
59
|
+
const jsdom = new JSDOM__default.JSDOM(html, options);
|
|
60
|
+
const { window } = jsdom;
|
|
61
|
+
const { document } = window;
|
|
62
|
+
|
|
63
|
+
// generate our list of keys by enumerating document.window - this list may vary
|
|
64
|
+
// based on the jsdom version. filter out internal methods as well as anything
|
|
65
|
+
// that node already defines
|
|
66
|
+
|
|
67
|
+
if (KEYS.length === 0) {
|
|
68
|
+
KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
|
|
69
|
+
// going to add our jsdom instance, see below
|
|
70
|
+
KEYS.push('$jsdom');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
KEYS.forEach(key => global[key] = window[key]);
|
|
74
|
+
|
|
75
|
+
// setup document / window / window.console
|
|
76
|
+
global.document = document;
|
|
77
|
+
global.window = window;
|
|
78
|
+
window.console = global.console;
|
|
79
|
+
|
|
80
|
+
// add access to our jsdom instance
|
|
81
|
+
global.$jsdom = jsdom;
|
|
82
|
+
|
|
83
|
+
const cleanup = () => KEYS.forEach(key => delete global[key]);
|
|
84
|
+
|
|
85
|
+
document.destroy = cleanup;
|
|
86
|
+
|
|
87
|
+
return cleanup
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
exports.jsdomGlobal = jsdomGlobal;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import JSDOM from 'jsdom';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
MIT License
|
|
5
|
+
|
|
6
|
+
Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
|
|
7
|
+
jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
|
|
30
|
+
|
|
31
|
+
// define this here so that we only ever dynamically populate KEYS once.
|
|
32
|
+
|
|
33
|
+
const KEYS = [];
|
|
34
|
+
|
|
35
|
+
function jsdomGlobal(html = defaultHtml, options = {}) {
|
|
36
|
+
// Idempotency
|
|
37
|
+
if (global.navigator
|
|
38
|
+
&& global.navigator.userAgent
|
|
39
|
+
&& global.navigator.userAgent.includes('Node.js')
|
|
40
|
+
&& global.document
|
|
41
|
+
&& typeof global.document.destroy === 'function')
|
|
42
|
+
return global.document.destroy
|
|
43
|
+
|
|
44
|
+
// set a default url if we don't get one - otherwise things explode when we copy localstorage keys
|
|
45
|
+
if (!('url' in options))
|
|
46
|
+
Object.assign(options, { url: 'http://localhost:3000' });
|
|
47
|
+
|
|
48
|
+
// enable pretendToBeVisual by default since react needs
|
|
49
|
+
// window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
|
|
50
|
+
if (!('pretendToBeVisual' in options))
|
|
51
|
+
Object.assign(options, { pretendToBeVisual: true });
|
|
52
|
+
|
|
53
|
+
const jsdom = new JSDOM.JSDOM(html, options);
|
|
54
|
+
const { window } = jsdom;
|
|
55
|
+
const { document } = window;
|
|
56
|
+
|
|
57
|
+
// generate our list of keys by enumerating document.window - this list may vary
|
|
58
|
+
// based on the jsdom version. filter out internal methods as well as anything
|
|
59
|
+
// that node already defines
|
|
60
|
+
|
|
61
|
+
if (KEYS.length === 0) {
|
|
62
|
+
KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
|
|
63
|
+
// going to add our jsdom instance, see below
|
|
64
|
+
KEYS.push('$jsdom');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
KEYS.forEach(key => global[key] = window[key]);
|
|
68
|
+
|
|
69
|
+
// setup document / window / window.console
|
|
70
|
+
global.document = document;
|
|
71
|
+
global.window = window;
|
|
72
|
+
window.console = global.console;
|
|
73
|
+
|
|
74
|
+
// add access to our jsdom instance
|
|
75
|
+
global.$jsdom = jsdom;
|
|
76
|
+
|
|
77
|
+
const cleanup = () => KEYS.forEach(key => delete global[key]);
|
|
78
|
+
|
|
79
|
+
document.destroy = cleanup;
|
|
80
|
+
|
|
81
|
+
return cleanup
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export { jsdomGlobal };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const React = require('react');
|
|
4
|
+
const client = require('react-dom/client');
|
|
5
|
+
const reactHelmetAsync = require('react-helmet-async');
|
|
6
|
+
const reactRouterDom = require('react-router-dom');
|
|
7
|
+
const SiteMetadataDefaults = require('./shared/vite-react-ssg.4ca822c0.cjs');
|
|
8
|
+
|
|
9
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
10
|
+
|
|
11
|
+
const React__default = /*#__PURE__*/_interopDefaultCompat(React);
|
|
12
|
+
|
|
13
|
+
function documentReady(_passThrough) {
|
|
14
|
+
if (document.readyState === "loading") {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
document.addEventListener("DOMContentLoaded", () => resolve(_passThrough));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return Promise.resolve(_passThrough);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function ViteReactSSG(routerOptions, fn, options = {}) {
|
|
23
|
+
const {
|
|
24
|
+
transformState,
|
|
25
|
+
rootContainer = "#root"
|
|
26
|
+
} = options;
|
|
27
|
+
const isClient = typeof window !== "undefined";
|
|
28
|
+
async function createApp(client = false, routePath) {
|
|
29
|
+
const browserRouter = client ? reactRouterDom.createBrowserRouter(routerOptions.routes) : void 0;
|
|
30
|
+
const appRenderCallbacks = [];
|
|
31
|
+
const onSSRAppRendered = client ? () => {
|
|
32
|
+
} : (cb) => appRenderCallbacks.push(cb);
|
|
33
|
+
const triggerOnSSRAppRendered = () => {
|
|
34
|
+
return Promise.all(appRenderCallbacks.map((cb) => cb()));
|
|
35
|
+
};
|
|
36
|
+
const context = {
|
|
37
|
+
// app: App,
|
|
38
|
+
isClient,
|
|
39
|
+
routes: routerOptions.routes,
|
|
40
|
+
router: browserRouter,
|
|
41
|
+
routerOptions,
|
|
42
|
+
onSSRAppRendered,
|
|
43
|
+
triggerOnSSRAppRendered,
|
|
44
|
+
initialState: {},
|
|
45
|
+
transformState,
|
|
46
|
+
routePath
|
|
47
|
+
};
|
|
48
|
+
if (client) {
|
|
49
|
+
await documentReady();
|
|
50
|
+
context.initialState = transformState?.(window.__INITIAL_STATE__ || {}) || SiteMetadataDefaults.deserializeState(window.__INITIAL_STATE__);
|
|
51
|
+
}
|
|
52
|
+
await fn?.(context);
|
|
53
|
+
if (!client) {
|
|
54
|
+
context.routePath ?? "/";
|
|
55
|
+
context.initialState = {};
|
|
56
|
+
}
|
|
57
|
+
const initialState = context.initialState;
|
|
58
|
+
return {
|
|
59
|
+
...context,
|
|
60
|
+
initialState
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (isClient) {
|
|
64
|
+
(async () => {
|
|
65
|
+
const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
|
|
66
|
+
const { router } = await createApp(true);
|
|
67
|
+
client.hydrateRoot(container, /* @__PURE__ */ React__default.createElement(reactHelmetAsync.HelmetProvider, null, /* @__PURE__ */ React__default.createElement(SiteMetadataDefaults.SiteMetadataDefaults, null), /* @__PURE__ */ React__default.createElement(reactRouterDom.RouterProvider, { router })));
|
|
68
|
+
})();
|
|
69
|
+
}
|
|
70
|
+
return createApp;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
exports.Head = SiteMetadataDefaults.Head;
|
|
74
|
+
exports.ViteReactSSG = ViteReactSSG;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { R as RouterOptions, V as ViteReactSSGContext, a as ViteReactSSGClientOptions } from './types-fa5fe9c2.js';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { HelmetProps } from 'react-helmet-async';
|
|
4
|
+
import 'critters';
|
|
5
|
+
import 'react-router-dom';
|
|
6
|
+
|
|
7
|
+
type Props = HelmetProps & {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
};
|
|
10
|
+
declare function Head(props: Props): JSX.Element;
|
|
11
|
+
|
|
12
|
+
declare function ViteReactSSG(routerOptions: RouterOptions, fn?: (context: ViteReactSSGContext<true>) => Promise<void> | void, options?: ViteReactSSGClientOptions): (client?: boolean, routePath?: string) => Promise<ViteReactSSGContext<true>>;
|
|
13
|
+
|
|
14
|
+
export { Head, ViteReactSSG };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { hydrateRoot } from 'react-dom/client';
|
|
3
|
+
import { HelmetProvider } from 'react-helmet-async';
|
|
4
|
+
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
|
|
5
|
+
import { S as SiteMetadataDefaults, d as deserializeState } from './shared/vite-react-ssg.9d005d5e.mjs';
|
|
6
|
+
export { H as Head } from './shared/vite-react-ssg.9d005d5e.mjs';
|
|
7
|
+
|
|
8
|
+
function documentReady(_passThrough) {
|
|
9
|
+
if (document.readyState === "loading") {
|
|
10
|
+
return new Promise((resolve) => {
|
|
11
|
+
document.addEventListener("DOMContentLoaded", () => resolve(_passThrough));
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return Promise.resolve(_passThrough);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function ViteReactSSG(routerOptions, fn, options = {}) {
|
|
18
|
+
const {
|
|
19
|
+
transformState,
|
|
20
|
+
rootContainer = "#root"
|
|
21
|
+
} = options;
|
|
22
|
+
const isClient = typeof window !== "undefined";
|
|
23
|
+
async function createApp(client = false, routePath) {
|
|
24
|
+
const browserRouter = client ? createBrowserRouter(routerOptions.routes) : void 0;
|
|
25
|
+
const appRenderCallbacks = [];
|
|
26
|
+
const onSSRAppRendered = client ? () => {
|
|
27
|
+
} : (cb) => appRenderCallbacks.push(cb);
|
|
28
|
+
const triggerOnSSRAppRendered = () => {
|
|
29
|
+
return Promise.all(appRenderCallbacks.map((cb) => cb()));
|
|
30
|
+
};
|
|
31
|
+
const context = {
|
|
32
|
+
// app: App,
|
|
33
|
+
isClient,
|
|
34
|
+
routes: routerOptions.routes,
|
|
35
|
+
router: browserRouter,
|
|
36
|
+
routerOptions,
|
|
37
|
+
onSSRAppRendered,
|
|
38
|
+
triggerOnSSRAppRendered,
|
|
39
|
+
initialState: {},
|
|
40
|
+
transformState,
|
|
41
|
+
routePath
|
|
42
|
+
};
|
|
43
|
+
if (client) {
|
|
44
|
+
await documentReady();
|
|
45
|
+
context.initialState = transformState?.(window.__INITIAL_STATE__ || {}) || deserializeState(window.__INITIAL_STATE__);
|
|
46
|
+
}
|
|
47
|
+
await fn?.(context);
|
|
48
|
+
if (!client) {
|
|
49
|
+
context.routePath ?? "/";
|
|
50
|
+
context.initialState = {};
|
|
51
|
+
}
|
|
52
|
+
const initialState = context.initialState;
|
|
53
|
+
return {
|
|
54
|
+
...context,
|
|
55
|
+
initialState
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (isClient) {
|
|
59
|
+
(async () => {
|
|
60
|
+
const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
|
|
61
|
+
const { router } = await createApp(true);
|
|
62
|
+
hydrateRoot(container, /* @__PURE__ */ React.createElement(HelmetProvider, null, /* @__PURE__ */ React.createElement(SiteMetadataDefaults, null), /* @__PURE__ */ React.createElement(RouterProvider, { router })));
|
|
63
|
+
})();
|
|
64
|
+
}
|
|
65
|
+
return createApp;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { ViteReactSSG };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const kolorist = require('kolorist');
|
|
4
|
+
const yargs = require('yargs');
|
|
5
|
+
const helpers = require('yargs/helpers');
|
|
6
|
+
const build = require('../shared/vite-react-ssg.1128b41a.cjs');
|
|
7
|
+
require('node:path');
|
|
8
|
+
require('node:module');
|
|
9
|
+
require('fs-extra');
|
|
10
|
+
require('vite');
|
|
11
|
+
require('jsdom');
|
|
12
|
+
require('../shared/vite-react-ssg.4ca822c0.cjs');
|
|
13
|
+
require('react');
|
|
14
|
+
require('react-helmet-async');
|
|
15
|
+
require('react-router-dom/server.js');
|
|
16
|
+
require('node:stream');
|
|
17
|
+
require('react-dom/server');
|
|
18
|
+
|
|
19
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
20
|
+
|
|
21
|
+
const yargs__default = /*#__PURE__*/_interopDefaultCompat(yargs);
|
|
22
|
+
|
|
23
|
+
yargs__default(helpers.hideBin(process.argv)).scriptName("vite-ssg").usage("$0 [args]").command(
|
|
24
|
+
"build",
|
|
25
|
+
"Build SSG",
|
|
26
|
+
(args) => args.option("script", {
|
|
27
|
+
choices: ["sync", "async", "defer", "async defer"],
|
|
28
|
+
describe: "Rewrites script loading timing"
|
|
29
|
+
}).option("mock", {
|
|
30
|
+
type: "boolean",
|
|
31
|
+
describe: "Mock browser globals (window, document, etc.) for SSG"
|
|
32
|
+
}).option("config", {
|
|
33
|
+
alias: "c",
|
|
34
|
+
type: "string",
|
|
35
|
+
describe: "The vite config file to use"
|
|
36
|
+
}).option("base", {
|
|
37
|
+
alias: "b",
|
|
38
|
+
type: "string",
|
|
39
|
+
describe: "The base path to render"
|
|
40
|
+
}),
|
|
41
|
+
async (args) => {
|
|
42
|
+
const { config: configFile = void 0, ...ssgOptions } = args;
|
|
43
|
+
await build.build(ssgOptions, { configFile });
|
|
44
|
+
}
|
|
45
|
+
).fail((msg, err, yargs2) => {
|
|
46
|
+
console.error(`
|
|
47
|
+
${kolorist.gray("[vite-ssg]")} ${kolorist.bold(kolorist.red("An internal error occurred."))}`);
|
|
48
|
+
console.error(`${kolorist.gray("[vite-ssg]")} ${kolorist.reset(`Please report an issue, if none already exists: ${kolorist.underline("https://github.com/antfu/vite-ssg/issues")}`)}`);
|
|
49
|
+
yargs2.exit(1, err);
|
|
50
|
+
}).showHelpOnFail(false).help().argv;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { gray, bold, red, reset, underline } from 'kolorist';
|
|
2
|
+
import yargs from 'yargs';
|
|
3
|
+
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
import { b as build } from '../shared/vite-react-ssg.c2c344c3.mjs';
|
|
5
|
+
import 'node:path';
|
|
6
|
+
import 'node:module';
|
|
7
|
+
import 'fs-extra';
|
|
8
|
+
import 'vite';
|
|
9
|
+
import 'jsdom';
|
|
10
|
+
import '../shared/vite-react-ssg.9d005d5e.mjs';
|
|
11
|
+
import 'react';
|
|
12
|
+
import 'react-helmet-async';
|
|
13
|
+
import 'react-router-dom/server.js';
|
|
14
|
+
import 'node:stream';
|
|
15
|
+
import 'react-dom/server';
|
|
16
|
+
|
|
17
|
+
yargs(hideBin(process.argv)).scriptName("vite-ssg").usage("$0 [args]").command(
|
|
18
|
+
"build",
|
|
19
|
+
"Build SSG",
|
|
20
|
+
(args) => args.option("script", {
|
|
21
|
+
choices: ["sync", "async", "defer", "async defer"],
|
|
22
|
+
describe: "Rewrites script loading timing"
|
|
23
|
+
}).option("mock", {
|
|
24
|
+
type: "boolean",
|
|
25
|
+
describe: "Mock browser globals (window, document, etc.) for SSG"
|
|
26
|
+
}).option("config", {
|
|
27
|
+
alias: "c",
|
|
28
|
+
type: "string",
|
|
29
|
+
describe: "The vite config file to use"
|
|
30
|
+
}).option("base", {
|
|
31
|
+
alias: "b",
|
|
32
|
+
type: "string",
|
|
33
|
+
describe: "The base path to render"
|
|
34
|
+
}),
|
|
35
|
+
async (args) => {
|
|
36
|
+
const { config: configFile = void 0, ...ssgOptions } = args;
|
|
37
|
+
await build(ssgOptions, { configFile });
|
|
38
|
+
}
|
|
39
|
+
).fail((msg, err, yargs2) => {
|
|
40
|
+
console.error(`
|
|
41
|
+
${gray("[vite-ssg]")} ${bold(red("An internal error occurred."))}`);
|
|
42
|
+
console.error(`${gray("[vite-ssg]")} ${reset(`Please report an issue, if none already exists: ${underline("https://github.com/antfu/vite-ssg/issues")}`)}`);
|
|
43
|
+
yargs2.exit(1, err);
|
|
44
|
+
}).showHelpOnFail(false).help().argv;
|
package/dist/node.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const build = require('./shared/vite-react-ssg.1128b41a.cjs');
|
|
4
|
+
require('node:path');
|
|
5
|
+
require('node:module');
|
|
6
|
+
require('kolorist');
|
|
7
|
+
require('fs-extra');
|
|
8
|
+
require('vite');
|
|
9
|
+
require('jsdom');
|
|
10
|
+
require('./shared/vite-react-ssg.4ca822c0.cjs');
|
|
11
|
+
require('react');
|
|
12
|
+
require('react-helmet-async');
|
|
13
|
+
require('react-router-dom/server.js');
|
|
14
|
+
require('node:stream');
|
|
15
|
+
require('react-dom/server');
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
exports.build = build.build;
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InlineConfig } from 'vite';
|
|
2
|
+
import { b as ViteReactSSGOptions } from './types-fa5fe9c2.js';
|
|
3
|
+
import 'critters';
|
|
4
|
+
import 'react';
|
|
5
|
+
import 'react-router-dom';
|
|
6
|
+
|
|
7
|
+
declare function build(ssgOptions?: Partial<ViteReactSSGOptions>, viteConfig?: InlineConfig): Promise<void>;
|
|
8
|
+
|
|
9
|
+
export { build };
|
package/dist/node.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { b as build } from './shared/vite-react-ssg.c2c344c3.mjs';
|
|
2
|
+
import 'node:path';
|
|
3
|
+
import 'node:module';
|
|
4
|
+
import 'kolorist';
|
|
5
|
+
import 'fs-extra';
|
|
6
|
+
import 'vite';
|
|
7
|
+
import 'jsdom';
|
|
8
|
+
import './shared/vite-react-ssg.9d005d5e.mjs';
|
|
9
|
+
import 'react';
|
|
10
|
+
import 'react-helmet-async';
|
|
11
|
+
import 'react-router-dom/server.js';
|
|
12
|
+
import 'node:stream';
|
|
13
|
+
import 'react-dom/server';
|