Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions rsbuild/express-https/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Custom [Express](https://expressjs.com/) **HTTPS** server integrated with Rsbuild's dev middlewares and HMR.

The plain HTTP version lives in [`../express`](../express). Setting only `server.https` in `rsbuild.config` is not enough for a custom Express server: `app.listen()` still creates an HTTP server. This example creates the TLS socket with `https.createServer` and passes the same certificate into Rsbuild so the client uses `https` / `wss`.
The plain HTTP version lives in [`../express`](../express). Setting only `server.https` in `rsbuild.config` is not enough for a custom Express server: `app.listen()` still creates an HTTP server. This example uses [`@rsbuild/plugin-basic-ssl`](https://github.com/rstackjs/rsbuild-plugin-basic-ssl) to generate and cache a self-signed certificate. After `createDevServer()`, it reads `server.https` from `rsbuild.getNormalizedConfig()` and passes the HTTPS options to `https.createServer`.

## Setup

Expand All @@ -20,15 +20,15 @@ Start the HTTPS dev server:
pnpm dev
```

Open the printed `https://localhost:<port>/` URL. The certificate is self-signed (`selfsigned`), so the browser will show a warning — continue once for local development.
Open the printed `https://localhost:<port>/` URL. The certificate is self-signed, so the browser will show a warning — continue once for local development.

Build the app for production:

```bash
pnpm build
```

Preview the production build locally (Rsbuild preview server; use `server.https` or `@rsbuild/plugin-basic-ssl` if you need HTTPS for preview):
Preview the production build locally with Rsbuild's HTTPS preview server:

```bash
pnpm preview
Expand Down
2 changes: 1 addition & 1 deletion rsbuild/express-https/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
},
"devDependencies": {
"@rsbuild/core": "2.2.0",
"@rsbuild/plugin-basic-ssl": "^2.0.0",
"@types/express": "^5.0.6",
"@types/node": "^24.12.4",
"express": "^5.2.1",
"selfsigned": "^5.5.0",
"typescript": "^5.9.3"
}
}
2 changes: 2 additions & 0 deletions rsbuild/express-https/rsbuild.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig } from '@rsbuild/core';
import { pluginBasicSsl } from '@rsbuild/plugin-basic-ssl';

export default defineConfig({
plugins: [pluginBasicSsl()],
// Custom Express owns the HTTPS listen socket; keep Rsbuild in middleware mode.
server: {
middlewareMode: true,
Expand Down
22 changes: 3 additions & 19 deletions rsbuild/express-https/server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import https from 'node:https';
import express from 'express';
import { createRsbuild, loadConfig } from '@rsbuild/core';
import selfsigned from 'selfsigned';

export async function startDevServer() {
const { content } = await loadConfig({});

// Generate an untrusted self-signed certificate for local HTTPS.
// Your browser will warn once; confirm to continue (same as plugin-basic-ssl).
const pems = await selfsigned.generate([{ name: 'commonName', value: 'localhost' }]);
const httpsOptions = {
key: pems.private,
cert: pems.cert,
};

// Pass the same HTTPS options into Rsbuild so asset/HMR URLs use https/wss.
// Do not rely on `server.https` alone with `app.listen()` — that stays HTTP.
const rsbuild = await createRsbuild({
rsbuildConfig: {
...content,
server: {
...content.server,
middlewareMode: true,
https: httpsOptions,
},
},
rsbuildConfig: content,
});

const app = express();
Expand All @@ -35,6 +17,8 @@ export async function startDevServer() {
// Apply Rsbuild’s built-in middlewares
app.use(rsbuildServer.middlewares);

// Read the HTTPS options after plugin-basic-ssl has generated the certificate.
const { https: httpsOptions } = rsbuild.getNormalizedConfig().server;
const httpsServer = https.createServer(httpsOptions, app);

httpsServer.listen(rsbuildServer.port, async () => {
Expand Down
Loading