-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssr.mjs
39 lines (30 loc) · 993 Bytes
/
ssr.mjs
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
import { JSDOM } from 'jsdom';
import app from './ssr-bundle.mjs';
import fs from 'fs';
const index_html = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<script defer src='/bundle.js'></script>
{head}
</head>
<body>
{body}
<div style="color: red; margin-top: 40px;">loading...</div>
</body>
</html>`;
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
global.document = dom.window.document;
app(dom.window.document.body);
setTimeout(() => {
document.body.querySelectorAll('input').forEach(input => {
if(input.checked) input.setAttribute('checked', 'checked');
if(input.selected) input.setAttribute('selected', 'selected');
if(input.value) input.setAttribute('value', input.value);
});
let result = index_html.replace('{head}', dom.window.document.head.innerHTML)
.replace('{body}', dom.window.document.body.innerHTML);
console.log(result);
fs.writeFileSync('./public/index.html', result);
}, 10);