-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (87 loc) · 2.72 KB
/
index.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
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
import Fastify from 'fastify';
import {createServer as createViteServer} from 'vite';
import {getData} from './data.js';
import {renderPreact} from './preact/index.js';
import {renderInferno} from './inferno/index.js';
import {renderMithril} from './mithril/index.js';
import {renderHandlebars} from './handlebars/index.js';
import {renderArt} from './art-template/index.js';
import {renderVanilla} from './vanilla/index.js';
const DEV = process.env.DEV === 'true';
const fastify = Fastify({
logger: true
});
export let vite;
let renderSvelte, renderSolid;
if (DEV) {
await fastify.register(import('@fastify/middie'));
// generate the dev server
vite = await createViteServer({
server: {middlewareMode: true},
appType: 'custom'
});
fastify.use(vite.middlewares);
const module = await vite.ssrLoadModule('/svelte/index.js');
renderSvelte = module.render;
const moduleSolid = await vite.ssrLoadModule('/solid/index.jsx');
renderSolid = moduleSolid.render;
} else {
const module = await import('./vite/server/svelte/index.js');
renderSvelte = module.render;
const moduleSolid = await import('./vite/server/solid/index.js');
renderSolid = moduleSolid.render;
}
fastify.get('/', async function (request, reply) {
const data = getData();
const html = await renderVanilla(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.get('/svelte', async function (request, reply) {
const data = getData();
const html = await renderSvelte(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.get('/preact', async function (request, reply) {
const data = getData();
const html = renderPreact(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.get('/inferno', async function (request, reply) {
const data = getData();
const html = renderInferno(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.get('/solid', async function (request, reply) {
const data = getData();
const html = renderSolid(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.get('/mithril', async function (request, reply) {
const data = getData();
const html = renderMithril(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.get('/handlebars', async function (request, reply) {
const data = getData();
const html = renderHandlebars(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.get('/art', async function (request, reply) {
const data = getData();
const html = renderArt(data);
reply.header('content-type', 'text/html');
reply.send(html);
});
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
});