-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
70 lines (66 loc) · 2.74 KB
/
index.ts
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
import { promises as fs } from 'fs';
import * as path from 'path';
import * as uriJs from 'uri-js';
import * as http from 'http';
import { resolveUser } from './resolveUser';
import { getImportMap } from './dependency';
// TODO: Handle these types using rules specified in user's webpack.config
const handleTypes = {
js(content: string, request: http.ClientRequest, response: http.ServerResponse) {
response.writeHead(200, { 'Content-Type': 'application/javascript' });
return content;
},
css(content: string, request: http.IncomingMessage, response: http.ServerResponse) {
if (request.headers.accept.startsWith('text/css')) {
response.writeHead(200, { 'Content-Type': 'text/css' });
return content;
}
// TODO: use css-loader
response.writeHead(200, { 'Content-Type': 'application/javascript' });
return 'document.head.insertAdjacentHTML("beforeEnd", `<style>' + content + '</style>`)';
},
async html(content: string, request: http.IncomingMessage, response: http.ServerResponse) {
if (request.headers.accept.startsWith('text/html')) {
response.writeHead(200, { 'Content-Type': 'text/html' });
// TODO: Use HtmlWebpackPlugin
const imports = await getImportMap();
return content.replace('<!-- INJECT SCRIPTS HERE -->', `
<script type="importmap">${JSON.stringify({ imports }, null, 2)}</script>
<script type="module" src="${resolveUser.config.entry}"></script>`);
}
response.writeHead(200, { 'Content-Type': 'application/javascript' });
return 'export default `' + content + '`';
},
json(content: string, request: http.IncomingMessage, response: http.ServerResponse) {
if (request.headers.accept.startsWith('application/json')) {
response.writeHead(200, { 'Content-Type': 'application/json' });
return content;
}
// TODO: use json-loader
response.writeHead(200, { 'Content-Type': 'application/javascript' });
return 'export default ' + content;
},
};
function main() {
http.createServer(async (request, response) => {
try {
const urlData = uriJs.parse(request.url);
if (urlData.path === '/' && request.headers.accept.startsWith('text/html')) {
urlData.path = '@/index.html';
}
const requestFile = await resolveUser(urlData.path);
const [, fileExtension] = /\.([^.]+)$/.exec(requestFile);
const content = await fs.readFile(requestFile, { encoding: 'utf8' });
response.end(await handleTypes[fileExtension](content, request, response));
} catch (e) {
response.writeHead(404);
response.end();
}
}).listen(3000);
}
if (process.argv.length < 2) {
console.error('Usage: node . </path/to/project>');
} else {
resolveUser.init(path.resolve(__dirname, process.argv[process.argv.length - 1]));
main();
}