-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerateStatic.js
47 lines (38 loc) · 1.04 KB
/
generateStatic.js
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
40
41
42
43
44
45
46
47
/* eslint-disable */
const fs = require('fs');
const path = require('path');
const vite = require('vite');
const EXAMPLE_PATH = path.resolve(__dirname, 'static');
const TEMPLATE_PATH = path.resolve(__dirname, 'static/index.html');
async function generateStatic() {
// Example bundle
await vite.build({
root: './example',
build: {
outDir: EXAMPLE_PATH,
},
configFile: false,
emptyOutDir: true,
});
// SSR entry-point
await vite.build({
build: {
ssr: './example/ssr.tsx',
outDir: EXAMPLE_PATH,
emptyOutDir: false,
minify: false,
},
configFile: false,
});
// Load template
const template = fs.readFileSync(TEMPLATE_PATH, 'utf-8');
// Get render function from compiled SSR entry-point
const { render } = require('./static/ssr.js');
// Render <App />
const appHtml = await render();
// Inject rendered html into template
const html = template.replace('<!--app-html-->', appHtml);
// Write template to bundle
fs.writeFileSync(TEMPLATE_PATH, html);
}
void generateStatic();