vite-react-ssg 0.0.1 → 0.0.3
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 -21
- package/README.md +238 -131
- package/bin/vite-react-ssg.js +3 -3
- package/dist/chunks/jsdomGlobal.cjs +79 -79
- package/dist/chunks/jsdomGlobal.mjs +79 -79
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +3 -2
- package/dist/index.mjs +3 -3
- package/dist/node/cli.cjs +1 -1
- package/dist/node/cli.mjs +1 -1
- package/dist/node.cjs +1 -1
- package/dist/node.d.ts +1 -2
- package/dist/node.mjs +1 -1
- package/dist/shared/{vite-react-ssg.c2c344c3.mjs → vite-react-ssg.524ba00d.mjs} +70 -15
- package/dist/shared/{vite-react-ssg.1128b41a.cjs → vite-react-ssg.a5dfecac.cjs} +71 -16
- package/dist/{types-fa5fe9c2.d.ts → types-82f34756.d.ts} +17 -5
- package/package.json +8 -7
package/LICENSE
CHANGED
|
@@ -1,21 +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.
|
|
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
CHANGED
|
@@ -1,132 +1,239 @@
|
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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 createRoot = 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
|
+
```tsx
|
|
45
|
+
// src/App.tsx
|
|
46
|
+
import React from 'react'
|
|
47
|
+
import type { RouteRecord } from 'vite-react-ssg'
|
|
48
|
+
import './App.css'
|
|
49
|
+
|
|
50
|
+
const pages = import.meta.glob<any>('./pages/**/*.tsx')
|
|
51
|
+
|
|
52
|
+
const children: RouteRecord[] = Object.entries(pages).map(([filepath, component]) => {
|
|
53
|
+
let path = filepath.split('/pages')[1]
|
|
54
|
+
path = path.split('.')[0].replace('index', '')
|
|
55
|
+
const entry = `src${filepath.slice(1)}`
|
|
56
|
+
|
|
57
|
+
if (path.endsWith('/')) {
|
|
58
|
+
return {
|
|
59
|
+
index: true,
|
|
60
|
+
Component: React.lazy(component),
|
|
61
|
+
entry,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
path,
|
|
66
|
+
Component: React.lazy(component),
|
|
67
|
+
// Used to obtain static resources through manifest
|
|
68
|
+
entry,
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const Layout = React.lazy(() => import('./Layout'))
|
|
73
|
+
export const routes: RouteRecord[] = [
|
|
74
|
+
{
|
|
75
|
+
path: '/',
|
|
76
|
+
element: <Layout />,
|
|
77
|
+
children,
|
|
78
|
+
// Used to obtain static resources through manifest
|
|
79
|
+
entry: 'src/Layout.tsx',
|
|
80
|
+
},
|
|
81
|
+
]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Document head
|
|
85
|
+
|
|
86
|
+
You can use `<Head/>` to manage all of your changes to the document head. It takes plain HTML tags and outputs plain HTML tags. It is a wrapper around [React Helmet](https://github.com/nfl/react-helmet).
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
import { Head } from 'vite-react-ssg'
|
|
90
|
+
|
|
91
|
+
const MyHead = () => (
|
|
92
|
+
<Head>
|
|
93
|
+
<meta property="og:description" content="My custom description" />
|
|
94
|
+
<meta charSet="utf-8" />
|
|
95
|
+
<title>My Title</title>
|
|
96
|
+
<link rel="canonical" href="http://mysite.com/example" />
|
|
97
|
+
</Head>
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Nested or latter components will override duplicate usages:
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
import { Head } from 'vite-react-ssg'
|
|
105
|
+
|
|
106
|
+
const MyHead = () => (
|
|
107
|
+
<parent>
|
|
108
|
+
<Head>
|
|
109
|
+
<title>My Title</title>
|
|
110
|
+
<meta name="description" content="Helmet application" />
|
|
111
|
+
</Head>
|
|
112
|
+
<child>
|
|
113
|
+
<Head>
|
|
114
|
+
<title>Nested Title</title>
|
|
115
|
+
<meta name="description" content="Nested component" />
|
|
116
|
+
</Head>
|
|
117
|
+
</child>
|
|
118
|
+
</parent>
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Outputs:
|
|
123
|
+
```html
|
|
124
|
+
<head>
|
|
125
|
+
<title>Nested Title</title>
|
|
126
|
+
<meta name="description" content="Nested component" />
|
|
127
|
+
</head>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Reactive head
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
import { useState } from 'react'
|
|
134
|
+
import { Head } from 'vite-react-ssg'
|
|
135
|
+
|
|
136
|
+
export default function MyHead() {
|
|
137
|
+
const [state, setState] = useState(false)
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<Head>
|
|
141
|
+
<meta charSet="UTF-8" />
|
|
142
|
+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
|
143
|
+
<title>head test {state ? 'A' : 'B'}</title>
|
|
144
|
+
{/* You can also set the 'body' attributes here */}
|
|
145
|
+
<body className={`body-class-in-head-${state ? 'a' : 'b'}`} />
|
|
146
|
+
</Head>
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Critical CSS
|
|
152
|
+
|
|
153
|
+
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.
|
|
154
|
+
Install it with:
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
npm i -D critters
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Critical CSS generation will automatically be enabled for you.
|
|
161
|
+
|
|
162
|
+
To configure `critters`, pass [its options](https://github.com/GoogleChromeLabs/critters#usage) into `ssgOptions.crittersOptions` in `vite.config.ts`:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
// vite.config.ts
|
|
166
|
+
export default defineConfig({
|
|
167
|
+
ssgOptions: {
|
|
168
|
+
crittersOptions: {
|
|
169
|
+
// E.g., change the preload strategy
|
|
170
|
+
preload: 'media',
|
|
171
|
+
// Other options: https://github.com/GoogleChromeLabs/critters#usage
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Configuration
|
|
178
|
+
|
|
179
|
+
You can pass options to Vite SSG in the `ssgOptions` field of your `vite.config.js`
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
// vite.config.js
|
|
183
|
+
|
|
184
|
+
export default {
|
|
185
|
+
plugins: [],
|
|
186
|
+
ssgOptions: {
|
|
187
|
+
script: 'async',
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
See [src/types.ts](./src/types.ts). for more options available.
|
|
193
|
+
|
|
194
|
+
### Custom Routes to Render
|
|
195
|
+
|
|
196
|
+
You can use the `includedRoutes` hook to include or exclude route paths to render, or even provide some completely custom ones.
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
// vite.config.js
|
|
200
|
+
|
|
201
|
+
export default {
|
|
202
|
+
plugins: [],
|
|
203
|
+
ssgOptions: {
|
|
204
|
+
includedRoutes(paths, routes) {
|
|
205
|
+
// exclude all the route paths that contains 'foo'
|
|
206
|
+
return paths.filter(i => !i.includes('foo'))
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
```js
|
|
212
|
+
// vite.config.js
|
|
213
|
+
|
|
214
|
+
export default {
|
|
215
|
+
plugins: [],
|
|
216
|
+
ssgOptions: {
|
|
217
|
+
includedRoutes(paths, routes) {
|
|
218
|
+
// use original route records
|
|
219
|
+
return routes.flatMap((route) => {
|
|
220
|
+
return route.name === 'Blog'
|
|
221
|
+
? myBlogSlugs.map(slug => `/blog/${slug}`)
|
|
222
|
+
: route.path
|
|
223
|
+
})
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Roadmap
|
|
230
|
+
|
|
231
|
+
- [x] Preload assets
|
|
232
|
+
- [x] Document head
|
|
233
|
+
- [ ] SSR under dev
|
|
234
|
+
- [ ] Initial State
|
|
235
|
+
- [ ] More Client components, such as `<ClientOnly />`
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
132
239
|
[MIT](./LICENSE) License © 2023 [Riri](https://github.com/Daydreamer-riri)
|
package/bin/vite-react-ssg.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
'use strict'
|
|
3
|
-
import('../dist/node/cli.mjs')
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict'
|
|
3
|
+
import('../dist/node/cli.mjs')
|
|
@@ -6,85 +6,85 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
6
6
|
|
|
7
7
|
const JSDOM__default = /*#__PURE__*/_interopDefaultCompat(JSDOM);
|
|
8
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
|
|
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
88
|
}
|
|
89
89
|
|
|
90
90
|
exports.jsdomGlobal = jsdomGlobal;
|