Skip to content

Commit 0519d46

Browse files
committed
feat: basic implementation
1 parent 2993dda commit 0519d46

8 files changed

Lines changed: 107 additions & 29 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# rsbuild-plugin-example
1+
# @rsbuild/plugin-basic-ssl
22

3-
rsbuild-plugin-example is a Rsbuild plugin to do something.
3+
@rsbuild/plugin-basic-ssl is a Rsbuild plugin to do something.
44

55
<p>
6-
<a href="https://npmjs.com/package/rsbuild-plugin-example">
7-
<img src="https://img.shields.io/npm/v/rsbuild-plugin-example?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
6+
<a href="https://npmjs.com/package/@rsbuild/plugin-basic-ssl">
7+
<img src="https://img.shields.io/npm/v/@rsbuild/plugin-basic-ssl?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
88
</a>
99
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
1010
</p>
@@ -14,17 +14,17 @@ rsbuild-plugin-example is a Rsbuild plugin to do something.
1414
Install:
1515

1616
```bash
17-
npm add rsbuild-plugin-example -D
17+
npm add @rsbuild/plugin-basic-ssl -D
1818
```
1919

2020
Add plugin to your `rsbuild.config.ts`:
2121

2222
```ts
2323
// rsbuild.config.ts
24-
import { pluginExample } from "rsbuild-plugin-example";
24+
import { pluginBasicSsl } from "@rsbuild/plugin-basic-ssl";
2525

2626
export default {
27-
plugins: [pluginExample()],
27+
plugins: [pluginBasicSsl()],
2828
};
2929
```
3030

@@ -39,7 +39,7 @@ Some description.
3939
- Example:
4040

4141
```js
42-
pluginExample({
42+
pluginBasicSsl({
4343
foo: "bar",
4444
});
4545
```

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "rsbuild-plugin-example",
2+
"name": "@rsbuild/plugin-basic-ssl",
33
"version": "0.0.0",
4-
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-template",
4+
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-basic-ssl",
55
"license": "MIT",
66
"type": "module",
77
"exports": {
@@ -31,6 +31,9 @@
3131
"biome check --write --no-errors-on-unmatched"
3232
]
3333
},
34+
"dependencies": {
35+
"selfsigned": "^2.4.1"
36+
},
3437
"devDependencies": {
3538
"@biomejs/biome": "^1.8.3",
3639
"@playwright/test": "^1.44.1",

playground/rsbuild.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from '@rsbuild/core';
2-
import { pluginExample } from '../src';
2+
import { pluginBasicSsl } from '../src';
33

44
export default defineConfig({
5-
plugins: [pluginExample()],
5+
plugins: [pluginBasicSsl()],
66
});

pnpm-lock.yaml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import type { RsbuildPlugin } from '@rsbuild/core';
2+
import { resolveHttpsConfig } from './util.js';
23

3-
export type PluginExampleOptions = {
4-
foo?: string;
5-
bar?: boolean;
6-
};
4+
export const PLUGIN_BASIC_SSL_NAME = 'rsbuild:basic-ssl';
75

8-
export const pluginExample = (
9-
options: PluginExampleOptions = {},
10-
): RsbuildPlugin => ({
11-
name: 'plugin-example',
12-
13-
setup() {
14-
console.log('Hello Rsbuild!', options);
15-
},
16-
});
6+
export function pluginBasicSsl(): RsbuildPlugin {
7+
return {
8+
name: PLUGIN_BASIC_SSL_NAME,
9+
setup(api) {
10+
api.modifyRsbuildConfig((config) => {
11+
config.server = {
12+
...config.server,
13+
https: resolveHttpsConfig(config.server?.https),
14+
};
15+
});
16+
},
17+
};
18+
}

src/util.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import type { ServerConfig } from '@rsbuild/core';
4+
import selfsigned from 'selfsigned';
5+
6+
type HttpsConfig = ServerConfig['https'];
7+
8+
export const resolveHttpsConfig = (
9+
config: HttpsConfig,
10+
): {
11+
key: NonNullable<HttpsConfig>['key'];
12+
cert: NonNullable<HttpsConfig>['cert'];
13+
} => {
14+
const { key, cert } = config ?? {};
15+
16+
if (key && cert) {
17+
return { key, cert };
18+
}
19+
20+
const certPath = path.join(__dirname, 'fake-cert.pem');
21+
if (fs.existsSync(certPath)) {
22+
const stats = fs.statSync(certPath);
23+
// Default validity period is 30 days
24+
if (stats.mtimeMs <= Date.now() - 1000 * 60 * 60 * 24 * 30) {
25+
const content = fs.readFileSync(certPath, { encoding: 'utf-8' });
26+
return {
27+
key: content,
28+
cert: content,
29+
};
30+
}
31+
}
32+
33+
const pem = selfsigned.generate(
34+
[{ name: 'commonName', value: 'localhost' }],
35+
{
36+
days: 30,
37+
keySize: 2048,
38+
},
39+
);
40+
41+
const content = pem.private + pem.cert;
42+
fs.writeFileSync(certPath, content, { encoding: 'utf-8' });
43+
return {
44+
key: content,
45+
cert: content,
46+
};
47+
};

test/basic/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { dirname } from 'node:path';
22
import { fileURLToPath } from 'node:url';
33
import { expect, test } from '@playwright/test';
44
import { createRsbuild } from '@rsbuild/core';
5-
import { pluginExample } from '../../src';
5+
import { pluginBasicSsl } from '../../src';
66

77
const __dirname = dirname(fileURLToPath(import.meta.url));
88

99
test('should render page as expected', async ({ page }) => {
1010
const rsbuild = await createRsbuild({
1111
cwd: __dirname,
1212
rsbuildConfig: {
13-
plugins: [pluginExample()],
13+
plugins: [pluginBasicSsl()],
1414
},
1515
});
1616

@@ -26,7 +26,7 @@ test('should build succeed', async ({ page }) => {
2626
const rsbuild = await createRsbuild({
2727
cwd: __dirname,
2828
rsbuildConfig: {
29-
plugins: [pluginExample()],
29+
plugins: [pluginBasicSsl()],
3030
},
3131
});
3232

tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from 'tsup';
22

33
export default defineConfig({
4-
entry: ['./src/*.ts'],
4+
entry: ['./src/index.ts'],
55
format: ['esm', 'cjs'],
66
target: 'node18',
77
dts: true,

0 commit comments

Comments
 (0)