Skip to content

Commit 3e9a010

Browse files
authored
feat: add outputPath option (#6)
1 parent 4e4d5e7 commit 3e9a010

6 files changed

Lines changed: 90 additions & 7 deletions

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,23 @@ Filename of the generated certificate.
4646

4747
```ts
4848
pluginBasicSsl({
49-
filename: 'foo.pem',
49+
filename: "foo.pem",
50+
});
51+
```
52+
53+
### outputPath
54+
55+
Output path of the generated certificate.
56+
57+
- **Type:** `string`
58+
- **Default:** `__dirname`
59+
- **Example:**
60+
61+
```ts
62+
import path from "node:path";
63+
64+
pluginBasicSsl({
65+
outputPath: path.join(__dirname, "node_modules/.cache/cert"),
5066
});
5167
```
5268

src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@ export type PluginBasicSslOptions = {
99
* @default 'fake-cert.pem'
1010
*/
1111
filename?: string;
12+
/**
13+
* Output path of the generated certificate
14+
* @default __dirname
15+
*/
16+
outputPath?: string;
1217
};
1318

1419
export const pluginBasicSsl = (
1520
options: PluginBasicSslOptions = {},
1621
): RsbuildPlugin => ({
1722
name: PLUGIN_BASIC_SSL_NAME,
1823
setup(api) {
19-
api.modifyRsbuildConfig((config) => {
24+
api.modifyRsbuildConfig(async (config) => {
25+
const httpsConfig = await resolveHttpsConfig(
26+
config.server?.https,
27+
options,
28+
);
29+
2030
config.server = {
2131
...config.server,
22-
https: resolveHttpsConfig(config.server?.https, options),
32+
https: httpsConfig,
2333
};
2434
});
2535
},

src/util.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@ import type { PluginBasicSslOptions } from './index.js';
66

77
type HttpsConfig = ServerConfig['https'];
88

9-
export const resolveHttpsConfig = (
9+
async function ensureDir(dir: string) {
10+
try {
11+
await fs.promises.access(dir);
12+
} catch (error) {
13+
await ensureDir(path.dirname(dir));
14+
await fs.promises.mkdir(dir);
15+
}
16+
}
17+
18+
export const resolveHttpsConfig = async (
1019
config: HttpsConfig,
1120
options: PluginBasicSslOptions,
12-
): {
21+
): Promise<{
1322
key: NonNullable<HttpsConfig>['key'];
1423
cert: NonNullable<HttpsConfig>['cert'];
15-
} => {
24+
}> => {
1625
const { key, cert } = config ?? {};
1726

1827
if (key && cert) {
1928
return { key, cert };
2029
}
2130

22-
const certPath = path.join(__dirname, options.filename ?? 'fake-cert.pem');
31+
const certPath = path.join(
32+
options.outputPath ?? __dirname,
33+
options.filename ?? 'fake-cert.pem',
34+
);
2335

2436
if (fs.existsSync(certPath)) {
2537
const stats = fs.statSync(certPath);
@@ -45,7 +57,13 @@ export const resolveHttpsConfig = (
4557
);
4658

4759
const content = pem.private + pem.cert;
60+
61+
if (options.outputPath) {
62+
await ensureDir(options.outputPath);
63+
}
64+
4865
fs.writeFileSync(certPath, content, { encoding: 'utf-8' });
66+
4967
return {
5068
key: content,
5169
cert: content,

test/output-path/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { dirname, join } 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 output path', async () => {
10+
const rsbuild = await createRsbuild({
11+
cwd: __dirname,
12+
rsbuildConfig: {
13+
plugins: [
14+
pluginBasicSsl({
15+
outputPath: join(__dirname, 'dist'),
16+
}),
17+
],
18+
server: {
19+
port: 3200,
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/output-path/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/output-path/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)