-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
136 lines (116 loc) · 3.16 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import chalk from 'chalk';
import connect from 'connect';
import history from 'connect-history-api-fallback';
import cors from 'cors';
import esbuild from 'esbuild';
import http from 'http';
import https from 'https';
import open from 'open';
export interface serverOption {
port: number;
open?: boolean;
servedir: string;
proxy?: {
[key: string]: {
target: string;
pathRewrite?: {
[key: string]: string;
};
changeOrigin?: boolean;
};
};
}
export async function createDevServer(
buildOption: esbuild.BuildOptions,
serveOption: serverOption
) {
const {
port,
open: isOpenInDefaultBrowser = false,
servedir,
proxy,
} = serveOption;
// esbuild context
const context = await esbuild.context(buildOption);
// esbuild watch files change
await context.watch();
// esbuild serve
const { host, port: actualPort } = await context.serve({
port: port,
servedir: servedir,
});
const app = connect()
.use(cors())
.use(history())
.use((req, res) => {
let path = req.url;
const filterProxy = Object.keys(proxy ?? {}).filter((prefix) =>
new RegExp(prefix).test(path)
);
filterProxy.forEach((prefix) => {
const { target, pathRewrite, changeOrigin } = proxy![prefix];
Object.keys(pathRewrite ?? {}).forEach((prefix) => {
const regexp = new RegExp(prefix);
if (regexp.test(path)) {
path = path.replace(regexp, pathRewrite![prefix]);
}
});
const url = new URL(target + path);
const host = changeOrigin ? url.host : req.headers.host;
const httpOrHttps = url.protocol === 'http:' ? http : https;
req.pipe(
httpOrHttps.request(
url.href,
{
method: req.method,
headers: {
...req.headers,
host: host,
origin: url.protocol + '//' + url.host,
},
},
(proxy) => {
res.writeHead(proxy.statusCode!, proxy.headers);
proxy.pipe(res, { end: true });
}
),
{ end: true }
);
console.log(
`${chalk.hex('#62bb35').bold('[HTTP Proxy]')}: ${chalk
.hex('#eecc16')
.bold(path)} -> ${chalk.hex('#eecc16').bold(url.href)}`
);
return;
});
// no proxy required
if (filterProxy.length === 0) {
req.pipe(
http.request(
{
hostname: host,
port: actualPort,
path,
method: req.method,
headers: req.headers,
},
(proxy) => {
res.writeHead(proxy.statusCode!, proxy.headers);
proxy.pipe(res, { end: true });
}
),
{ end: true }
);
}
});
const server = http.createServer(app);
server.on('error', (err) => console.log(err));
process.on('uncaughtException', function (err) {
console.log(err);
});
server.listen(actualPort);
if (isOpenInDefaultBrowser) {
open(`http://localhost:${actualPort}/`);
}
return context;
}