diff --git a/README.md b/README.md index d976db7..577f373 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,18 @@ import basicSsl from '@vitejs/plugin-basic-ssl' export default { plugins: [ - basicSsl() + basicSsl({ + /** name of certification */ + name: 'test', + /** custom trust domains */ + domains: ['*.custom.com'], + /** custom certification directory */ + certDir: '/Users/.../.devServer/cert' + }) ] } ``` - + ## License MIT diff --git a/src/certificate.ts b/src/certificate.ts index d8ce82d..71c730b 100644 --- a/src/certificate.ts +++ b/src/certificate.ts @@ -52,10 +52,14 @@ function toPositiveHex(hexString: string) { return mostSignificativeHexAsInt.toString() + hexString.substring(1) } -export function createCertificate(): string { +export function createCertificate(name: string = 'example.org', domains?: string[]): string { const days = 30 const keySize = 2048 + const appendDomains = domains + ? domains.map(item => ({ type: 2, value: item })) + : [] + const extensions = [ // { // name: 'basicConstraints', @@ -108,7 +112,8 @@ export function createCertificate(): string { { type: 7, ip: 'fe80::1' - } + }, + ...appendDomains ] } ] @@ -116,7 +121,7 @@ export function createCertificate(): string { const attrs = [ { name: 'commonName', - value: 'example.org' + value: name }, { name: 'countryName', diff --git a/src/index.ts b/src/index.ts index 5f80cae..39abefc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,11 +4,21 @@ import type { Plugin } from 'vite' const defaultCacheDir = 'node_modules/.vite' -function viteBasicSslPlugin(): Plugin { +interface Options { + certDir: string + domains: string[] + name: string +} + +function viteBasicSslPlugin(options?: Partial): Plugin { return { name: 'vite:basic-ssl', async configResolved(config) { - const certificate = await getCertificate((config.cacheDir ?? defaultCacheDir) + '/basic-ssl') + const certificate = await getCertificate( + options?.certDir ?? (config.cacheDir ?? defaultCacheDir) + '/basic-ssl', + options?.name, + options?.domains + ) const https = () => ({ cert: certificate, key: certificate }) if (config.server.https === undefined || !!config.server.https) { config.server.https = Object.assign({}, config.server.https, https()) @@ -20,7 +30,11 @@ function viteBasicSslPlugin(): Plugin { } } -export async function getCertificate(cacheDir: string) { +export async function getCertificate( + cacheDir: string, + name?: string, + domains?: string[] +) { const cachePath = path.join(cacheDir, '_cert.pem') try { @@ -35,7 +49,10 @@ export async function getCertificate(cacheDir: string) { return content } catch { - const content = (await import('./certificate')).createCertificate() + const content = (await import('./certificate')).createCertificate( + name, + domains + ) fsp .mkdir(cacheDir, { recursive: true }) .then(() => fsp.writeFile(cachePath, content))