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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,23 @@ Filename of the generated certificate.

```ts
pluginBasicSsl({
filename: 'foo.pem',
filename: "foo.pem",
});
```

### outputPath

Output path of the generated certificate.

- **Type:** `string`
- **Default:** `__dirname`
- **Example:**

```ts
import path from "node:path";

pluginBasicSsl({
outputPath: path.join(__dirname, "node_modules/.cache/cert"),
});
```

Expand Down
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ export type PluginBasicSslOptions = {
* @default 'fake-cert.pem'
*/
filename?: string;
/**
* Output path of the generated certificate
* @default __dirname
*/
outputPath?: string;
};

export const pluginBasicSsl = (
options: PluginBasicSslOptions = {},
): RsbuildPlugin => ({
name: PLUGIN_BASIC_SSL_NAME,
setup(api) {
api.modifyRsbuildConfig((config) => {
api.modifyRsbuildConfig(async (config) => {
const httpsConfig = await resolveHttpsConfig(
config.server?.https,
options,
);

config.server = {
...config.server,
https: resolveHttpsConfig(config.server?.https, options),
https: httpsConfig,
};
});
},
Expand Down
26 changes: 22 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,32 @@ import type { PluginBasicSslOptions } from './index.js';

type HttpsConfig = ServerConfig['https'];

export const resolveHttpsConfig = (
async function ensureDir(dir: string) {
try {
await fs.promises.access(dir);
} catch (error) {
await ensureDir(path.dirname(dir));
await fs.promises.mkdir(dir);
}
}

export const resolveHttpsConfig = async (
config: HttpsConfig,
options: PluginBasicSslOptions,
): {
): Promise<{
key: NonNullable<HttpsConfig>['key'];
cert: NonNullable<HttpsConfig>['cert'];
} => {
}> => {
const { key, cert } = config ?? {};

if (key && cert) {
return { key, cert };
}

const certPath = path.join(__dirname, options.filename ?? 'fake-cert.pem');
const certPath = path.join(
options.outputPath ?? __dirname,
options.filename ?? 'fake-cert.pem',
);

if (fs.existsSync(certPath)) {
const stats = fs.statSync(certPath);
Expand All @@ -45,7 +57,13 @@ export const resolveHttpsConfig = (
);

const content = pem.private + pem.cert;

if (options.outputPath) {
await ensureDir(options.outputPath);
}

fs.writeFileSync(certPath, content, { encoding: 'utf-8' });

return {
key: content,
cert: content,
Expand Down
33 changes: 33 additions & 0 deletions test/output-path/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect, test } from '@playwright/test';
import { createRsbuild } from '@rsbuild/core';
import { pluginBasicSsl } from '../../dist';

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

test('should print HTTPS server URLs when custom output path', async () => {
const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
plugins: [
pluginBasicSsl({
outputPath: join(__dirname, 'dist'),
}),
],
server: {
port: 3200,
},
},
});

const { server, urls } = await rsbuild.startDevServer();

await new Promise((resolve) => {
rsbuild.onDevCompileDone(resolve);
});

expect(urls.every((url) => url.startsWith('https'))).toBeTruthy();

await server.close();
});
5 changes: 5 additions & 0 deletions test/output-path/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { pluginBasicSsl } from '@rsbuild/plugin-basic-ssl';

export default {
plugins: [pluginBasicSsl()],
};
1 change: 1 addition & 0 deletions test/output-path/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('1');