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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pluginBasicSsl({
Output path of the generated certificate.

- **Type:** `string`
- **Default:** `__dirname`
- **Default:** `<project>/node_modules/.cache/basic-ssl`
- **Example:**

```ts
Expand Down
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'node:path';
import type { RsbuildPlugin } from '@rsbuild/core';
import type { SelfsignedOptions, generate } from 'selfsigned';
import { resolveHttpsConfig } from './util.js';
Expand All @@ -12,7 +13,7 @@ export type PluginBasicSslOptions = {
filename?: string;
/**
* Output path of the generated certificate
* @default __dirname
* @default '<project>/node_modules/.cache/basic-ssl'
*/
outputPath?: string;
/**
Expand All @@ -31,10 +32,11 @@ export const pluginBasicSsl = (
name: PLUGIN_BASIC_SSL_NAME,
setup(api) {
api.modifyRsbuildConfig(async (config) => {
const httpsConfig = await resolveHttpsConfig(
config.server?.https,
options,
);
const httpsConfig = await resolveHttpsConfig(config.server?.https, {
...options,
outputPath:
options.outputPath ?? path.join(api.context.cachePath, 'basic-ssl'),
Comment thread
chenjiahan marked this conversation as resolved.
Resolved
});

config.server = {
...config.server,
Expand Down
11 changes: 3 additions & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { X509Certificate } from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { ServerConfig } from '@rsbuild/core';
import selfsigned from 'selfsigned';
import type { PluginBasicSslOptions } from './index.js';

type HttpsConfig = ServerConfig['https'];

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

async function ensureDir(dir: string) {
try {
await fs.promises.access(dir);
Expand All @@ -30,7 +27,7 @@ function isCertValid(content: string) {

export const resolveHttpsConfig = async (
config: HttpsConfig,
options: PluginBasicSslOptions,
options: PluginBasicSslOptions & { outputPath: string },
): Promise<{
key: NonNullable<HttpsConfig>['key'];
cert: NonNullable<HttpsConfig>['cert'];
Expand All @@ -42,7 +39,7 @@ export const resolveHttpsConfig = async (
}

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

Expand Down Expand Up @@ -73,9 +70,7 @@ export const resolveHttpsConfig = async (

const content = pem.private + pem.cert;

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

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

Expand Down
Loading