Skip to content

Commit 4e4d5e7

Browse files
authored
feat: add filename option (#5)
1 parent 4ca3d0e commit 4e4d5e7

6 files changed

Lines changed: 81 additions & 14 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ export default {
3232

3333
Then visit the https URL of the page, and confirm in your browser.
3434

35+
## Options
36+
37+
If you need to customize the compilation behavior of Sass, you can use the following configs.
38+
39+
### filename
40+
41+
Filename of the generated certificate.
42+
43+
- **Type:** `string`
44+
- **Default:** `'fake-cert.pem'`
45+
- **Example:**
46+
47+
```ts
48+
pluginBasicSsl({
49+
filename: 'foo.pem',
50+
});
51+
```
52+
3553
## License
3654

3755
[MIT](./LICENSE).

src/index.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@ import { resolveHttpsConfig } from './util.js';
33

44
export const PLUGIN_BASIC_SSL_NAME = 'rsbuild:basic-ssl';
55

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-
}
6+
export type PluginBasicSslOptions = {
7+
/**
8+
* Filename of the generated certificate
9+
* @default 'fake-cert.pem'
10+
*/
11+
filename?: string;
12+
};
13+
14+
export const pluginBasicSsl = (
15+
options: PluginBasicSslOptions = {},
16+
): RsbuildPlugin => ({
17+
name: PLUGIN_BASIC_SSL_NAME,
18+
setup(api) {
19+
api.modifyRsbuildConfig((config) => {
20+
config.server = {
21+
...config.server,
22+
https: resolveHttpsConfig(config.server?.https, options),
23+
};
24+
});
25+
},
26+
});

src/util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import type { ServerConfig } from '@rsbuild/core';
44
import selfsigned from 'selfsigned';
5+
import type { PluginBasicSslOptions } from './index.js';
56

67
type HttpsConfig = ServerConfig['https'];
78

89
export const resolveHttpsConfig = (
910
config: HttpsConfig,
11+
options: PluginBasicSslOptions,
1012
): {
1113
key: NonNullable<HttpsConfig>['key'];
1214
cert: NonNullable<HttpsConfig>['cert'];
@@ -17,7 +19,7 @@ export const resolveHttpsConfig = (
1719
return { key, cert };
1820
}
1921

20-
const certPath = path.join(__dirname, 'fake-cert.pem');
22+
const certPath = path.join(__dirname, options.filename ?? 'fake-cert.pem');
2123

2224
if (fs.existsSync(certPath)) {
2325
const stats = fs.statSync(certPath);

test/filename/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { expect, test } from '@playwright/test';
4+
import { createRsbuild } from '@rsbuild/core';
5+
import { pluginBasicSsl } from '../../dist';
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
9+
test('should print HTTPS server URLs when custom filename', async () => {
10+
const rsbuild = await createRsbuild({
11+
cwd: __dirname,
12+
rsbuildConfig: {
13+
plugins: [
14+
pluginBasicSsl({
15+
filename: 'foo.pem',
16+
}),
17+
],
18+
server: {
19+
port: 3100,
20+
},
21+
},
22+
});
23+
24+
const { server, urls } = await rsbuild.startDevServer();
25+
26+
await new Promise((resolve) => {
27+
rsbuild.onDevCompileDone(resolve);
28+
});
29+
30+
expect(urls.every((url) => url.startsWith('https'))).toBeTruthy();
31+
32+
await server.close();
33+
});

test/filename/rsbuild.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { pluginBasicSsl } from '@rsbuild/plugin-basic-ssl';
2+
3+
export default {
4+
plugins: [pluginBasicSsl()],
5+
};

test/filename/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('1');

0 commit comments

Comments
 (0)