-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (65 loc) · 2.06 KB
/
server.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
// @ts-check
// required until Deno v2
// https://github.com/denoland/deno/issues/13367
import 'data:text/javascript,delete globalThis.window;';
import { renderToWebStream } from 'vue/server-renderer';
import { createRouter, createMemoryHistory } from 'vue-router';
import app, { routes } from './src/app.js';
// minify js
import minify from 'npm:@node-minify/core';
import uglifyjs from 'npm:@node-minify/uglify-js';
// preload headers
import resolveLinkRelations from 'npm:modulepreload-link-relations/resolveLinkRelations.mjs';
import formatLinkHeaderRelations from 'npm:modulepreload-link-relations/formatLinkHeaderRelations.mjs';
// use importmap from deno.json
const config = await Deno.readTextFile('./deno.json');
const importmap = { imports: JSON.parse(config).imports };
const router = createRouter({
history: createMemoryHistory(),
routes,
});
app.use(router);
app.provide('importmap', JSON.stringify(importmap));
const appPath = './src';
Deno.serve(async (request) => {
const url = new URL(request.url, 'http://localhost');
// quick js file server
const static_path = './src';
const filePath = static_path + url.pathname;
let file;
try {
file = await Deno.readTextFile(filePath);
} catch {
// ignore
}
if (file) {
// @ts-ignore missing types from minify lib
file = await minify({
compressor: uglifyjs,
content: file,
});
// build modulepreload headers
const linkRelations = await resolveLinkRelations({
appPath,
url: filePath.replace(appPath, ''),
});
/** @type {Object.<string, string>} */
const headers = {
'content-type': 'text/javascript',
};
if (linkRelations) {
headers.link = formatLinkHeaderRelations(linkRelations);
}
return new Response(file, { headers });
}
if (url.pathname === '/favicon.ico') {
return new Response(null, { status: 404 });
}
await router.push(new URL(request.url).pathname);
const stream = renderToWebStream(app);
return new Response(stream, {
headers: {
'content-type': 'text/html; charset=utf-8',
},
});
});