Skip to content

Commit 02a1c6d

Browse files
feat: allow the server option to be Function (#5275)
1 parent 530db07 commit 02a1c6d

26 files changed

+1089
-632
lines changed

.cspell.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@
6767
"subsubcomain",
6868
"noselect",
6969
"commitlint",
70-
"eslintcache"
70+
"eslintcache",
71+
"hono",
72+
"privkey"
7173
],
7274
"ignorePaths": [
7375
"CHANGELOG.md",

examples/app/connect/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `app` Option
2+
3+
Serve using [`connect`](https://github.com/senchalabs/connect) as an application.
4+
5+
**webpack.config.js**
6+
7+
```js
8+
const connect = require("connect");
9+
10+
module.exports = {
11+
// ...
12+
devServer: {
13+
server: {
14+
app: () => connect(),
15+
},
16+
},
17+
};
18+
```
19+
20+
## What Should Happen
21+
22+
1. The script should open `https://localhost:8080/` in your default browser.
23+
2. You should see the text on the page itself change to read `Success!`.

examples/app/connect/app.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML = "Success!";
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
// our setup function adds behind-the-scenes bits to the config that all of our
4+
// examples need
5+
const connect = require("connect");
6+
const { setup } = require("../../util");
7+
8+
module.exports = setup({
9+
context: __dirname,
10+
entry: "./app.js",
11+
devServer: {
12+
app: () => connect(),
13+
},
14+
});

examples/app/hono/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `app` Option
2+
3+
Serve using [`hono`](https://github.com/honojs/hono) as an application.
4+
5+
**webpack.config.js**
6+
7+
```js
8+
const connect = require("connect");
9+
10+
module.exports = {
11+
// ...
12+
devServer: {
13+
server: {
14+
app: () => connect(),
15+
},
16+
},
17+
};
18+
```
19+
20+
## What Should Happen
21+
22+
1. The script should open `https://localhost:8080/` in your default browser.
23+
2. You should see the text on the page itself change to read `Success!`.

examples/app/hono/app.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML = "Success!";
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDCTCCAfGgAwIBAgIUevWiuCfenWuq9KyC8aQ/tc1Io14wDQYJKoZIhvcNAQEL
3+
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI0MDQyNDE2MDYyMloXDTI0MDUy
4+
NDE2MDYyMlowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
5+
AAOCAQ8AMIIBCgKCAQEA1v/lb9u9WkqkF7zjIKe2R+b4S0sQnWIfBFZ0ggtaOL0a
6+
ntud/EuaGQgLtJgSwO2M2xIqKx+yoLhoM+273EJe0KmfJMxYNAkhwP9h6vrKnaQJ
7+
mpAhoalfEGyCrnHHMKISAAn4Rlc8NXnULoFhHzNm8bdqvP33rCmsJ+tNYC5kwzyt
8+
HvRNFyg9BOUfACiPW17opFH0rao3IfZrQ6yRbknef1pX1x2pbDAH14rCT/vXaTs6
9+
VGuqLE/wRsSt+7nMHy/PmXxMyb4G4/UflYtnKfmXpDRw+TDEGzvTZedtoOz+rrJC
10+
e989R9qYGrlPfyfZbI+O348FV66I+jcD+/EUQs+HkwIDAQABo1MwUTAdBgNVHQ4E
11+
FgQU6bk4LSwtVQEt7V/ev+Zj270zdAkwHwYDVR0jBBgwFoAU6bk4LSwtVQEt7V/e
12+
v+Zj270zdAkwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAUBgo
13+
E3CZrrc/MaadFg1meNk+eKACmTsIa5cT6zi7MsvoKakXEd4bGd+iLifUzlAa1ygj
14+
dQppfprb5t68I7oO9/lkh2DfKrXxW/RpdhB05KslUd8q/3XY5kyao5quzeiVoMHR
15+
u+XYjoy2mTwdUC2uzFy6rkHsAkJy2vJJoDdlNsrKn6AZmh+voHHKrAtOL4gnanQV
16+
wR1u8eBVfk2MKIl2pNSCA4bD16uZyp3+oqq097BEoVa1pR+l8nwbsh/YfALifq/d
17+
P3yiN5+EqgiOIF9b8PZORe+Ry1O7uvPnU2ZRkVWPJ1S17Ms0lnr7IY3qjSBTuK66
18+
5uYi7ojrb5Vf0UL5oQ==
19+
-----END CERTIFICATE-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEugIBADANBgkqhkiG9w0BAQEFAASCBKQwggSgAgEAAoIBAQDW/+Vv271aSqQX
3+
vOMgp7ZH5vhLSxCdYh8EVnSCC1o4vRqe2538S5oZCAu0mBLA7YzbEiorH7KguGgz
4+
7bvcQl7QqZ8kzFg0CSHA/2Hq+sqdpAmakCGhqV8QbIKucccwohIACfhGVzw1edQu
5+
gWEfM2bxt2q8/fesKawn601gLmTDPK0e9E0XKD0E5R8AKI9bXuikUfStqjch9mtD
6+
rJFuSd5/WlfXHalsMAfXisJP+9dpOzpUa6osT/BGxK37ucwfL8+ZfEzJvgbj9R+V
7+
i2cp+ZekNHD5MMQbO9Nl522g7P6uskJ73z1H2pgauU9/J9lsj47fjwVXroj6NwP7
8+
8RRCz4eTAgMBAAECggEAA+zbFv43iEj5kvdfXC7DrK9iVBmUPZNXhqA/c0paxNNr
9+
A4B182+76f4UHKF0IjKUEkHUJEJpY/bJ7DzIY76QdZXLMoRKjfSmuZvQAVa/0T33
10+
8Or1ujpZ4nZgsmegX9ptorOL5VjdYAqP3aN+DvBEzl/vYnDujyWZn4bzvDBMpaXS
11+
39qW1MkcZ8UiP1fRad76+S57WnieBV+NRHYEAiDdMFKXLuw/igX/xOSZgq5Jh3I2
12+
hLS49S41dN1P9l9H2bPMw0CthNvMPPaemwKHz+84hSS+P4VJOWJzlGnXEdIFuqBR
13+
GFBESQzcemfS9DDB22Yt06YujBCbwTVVAxj73lnKkQKBgQDvYXK36J9y/NphDAWi
14+
Cwti5oE3eSfV0YazQwm+rRwC64wbpBFAm9ujwjUmaYBg75lBLF5nOOe8s1n95g5I
15+
tLfFb+zuZh8NNLjhfNE9/SNmRnnMvbcaDHeIE2RMAz+PuLN/gFLmsVIwK2X1LRC2
16+
0vHjw9Yzh6JLiOajAchzhZiCEQKBgQDl7R6Wfggo8myETA8Uv5tWot3IcquRkEl/
17+
TRCyao2/79rAGexS7piwD7FPdSDOk1zfZFYUOMzyMjj60sGcPRPqRX6D0usEODLQ
18+
TwsTJSCNgPnIOkqKkccwtqlTimbRIrPUSQfFPj56RzKKWdrJ/P3LPRjzkK7i3vLV
19+
EGlAENaLYwKBgHKSOnzpWr+HY+IFBgErthRs7LWnSDifYxATauuXIQwIvvNP0G4S
20+
6snzHss2vZonszstSDWxV8DKOq052eZUkIxv6H+l4wDIFiDeQ6uep73Ax3UF7EgM
21+
ZX18gombGGXqagcBXSxK/GJPsynomtJWHi38Ql5BcZ0jdffY157q9zZxAoGAPZtD
22+
Tt+GIDKUkP4wLEcKwDPzaPoQrngSuWFUz/ls8bi6zC4l/DKiBsqtn7Sqja8+ezzP
23+
M6vkfiCm084UwmA7LdJhC8E/52mHc/k55m9UQZYFV3kG8AoPbSYESLYUxoSd2ouW
24+
4WrEIs9g42EgFm8LMaG1Rc3GjlNejWhQSzI3yjECf3v7VoAcUwVfuVkwbm9W24vR
25+
neFTF8QBl//fxIdxZwoj5SrSgMOjmZ3pXA/ZbFJ0pB4Rh5dmKTYqdpfXsOTiBuwB
26+
XlqPVpN8UZEl3edpufLDyPldNej/9kEAkK5FS3YVyIQEg75739bCTlfzzCX1HdMx
27+
q98XYm/n5LWYFezsAt0=
28+
-----END PRIVATE KEY-----

examples/app/hono/webpack.config.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"use strict";
2+
3+
const wdm = require("webpack-dev-middleware");
4+
const { Hono } = require("hono");
5+
const { serve } = require("@hono/node-server");
6+
// eslint-disable-next-line import/extensions, import/no-unresolved
7+
const { serveStatic } = require("@hono/node-server/serve-static");
8+
const { setup } = require("../../util");
9+
10+
// our setup function adds behind-the-scenes bits to the config that all of our
11+
// examples need
12+
module.exports = setup({
13+
context: __dirname,
14+
entry: "./app.js",
15+
devServer: {
16+
// WARNING:
17+
//
18+
// You always need to set up middlewares which you required for your app,
19+
// built-in middlewares (like `history-api-fallback`/etc) doesn't work by default with `hono`
20+
setupMiddlewares: (_, devServer) => [
21+
{
22+
name: "webpack-dev-middleware",
23+
middleware: wdm.honoWrapper(devServer.compiler),
24+
},
25+
{
26+
name: "static",
27+
path: "/.assets/*",
28+
middleware: serveStatic({
29+
root: "../../.assets",
30+
rewriteRequestPath: (item) => item.replace(/^\/\.assets\//, "/"),
31+
}),
32+
},
33+
],
34+
app: () => new Hono(),
35+
server: (_, app) =>
36+
serve({
37+
fetch: app.fetch,
38+
//
39+
// Uncomment for `https`
40+
// createServer: require('node:https').createServer,
41+
// serverOptions: {
42+
// key: fs.readFileSync("./ssl/localhost-privkey.pem"),
43+
// cert: fs.readFileSync("./ssl/localhost-cert.pem"),
44+
// },
45+
//
46+
// Uncomment for `http2`
47+
// createServer: require("node:http2").createSecureServer,
48+
// serverOptions: {
49+
// allowHTTP1: true,
50+
// key: require("fs").readFileSync("./ssl/localhost-privkey.pem"),
51+
// cert: require("fs").readFileSync("./ssl/localhost-cert.pem"),
52+
// },
53+
}),
54+
},
55+
});

examples/server/http2/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# HTTP2 server
2+
3+
**webpack.config.js**
4+
5+
```js
6+
const connect = require("connect");
7+
8+
module.exports = {
9+
// ...
10+
devServer: {
11+
server: {
12+
server: "http2",
13+
app: () => connect(),
14+
},
15+
},
16+
};
17+
```
18+
19+
## What Should Happen
20+
21+
1. The script should open `https://localhost:8080/` in your default browser.
22+
2. You should see the text on the page itself change to read `Success!`.

examples/server/http2/app.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML = "Success!";
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
3+
// our setup function adds behind-the-scenes bits to the config that all of our
4+
// examples need
5+
const connect = require("connect");
6+
const { setup } = require("../../util");
7+
8+
module.exports = setup({
9+
context: __dirname,
10+
entry: "./app.js",
11+
devServer: {
12+
server: "http2",
13+
// Only `connect` supports `http2`
14+
app: () => connect(),
15+
},
16+
});

examples/util.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const path = require("path");
66
const fs = require("graceful-fs");
7+
const mime = require("mime");
78
const HtmlWebpackPlugin = require("html-webpack-plugin");
89
const { marked } = require("marked");
910

@@ -25,9 +26,18 @@ module.exports = {
2526

2627
const result = { ...defaults, ...config };
2728
const onBeforeSetupMiddleware = ({ app }) => {
28-
app.get("/.assets/*", (req, res) => {
29-
const filename = path.join(__dirname, "/", req.path);
30-
res.sendFile(filename);
29+
app.use("/.assets/", (req, res, next) => {
30+
if (req.method !== "GET" && req.method !== "HEAD") {
31+
next();
32+
return;
33+
}
34+
35+
res.setHeader("Content-Type", mime.lookup(req.url));
36+
37+
const filename = path.join(__dirname, "/.assets/", req.url);
38+
const stream = fs.createReadStream(filename);
39+
40+
stream.pipe(res);
3141
});
3242
};
3343
const renderer = new marked.Renderer();

0 commit comments

Comments
 (0)