@@ -4,11 +4,21 @@ import type { Plugin } from 'vite'
44
55const defaultCacheDir = 'node_modules/.vite'
66
7- function viteBasicSslPlugin ( ) : Plugin {
7+ interface Options {
8+ certDir : string
9+ domains : string [ ]
10+ name : string
11+ }
12+
13+ function viteBasicSslPlugin ( options ?: Partial < Options > ) : Plugin {
814 return {
915 name : 'vite:basic-ssl' ,
1016 async configResolved ( config ) {
11- const certificate = await getCertificate ( ( config . cacheDir ?? defaultCacheDir ) + '/basic-ssl' )
17+ const certificate = await getCertificate (
18+ options ?. certDir ?? ( config . cacheDir ?? defaultCacheDir ) + '/basic-ssl' ,
19+ options ?. name ,
20+ options ?. domains
21+ )
1222 const https = ( ) => ( { cert : certificate , key : certificate } )
1323 if ( config . server . https === undefined || ! ! config . server . https ) {
1424 config . server . https = Object . assign ( { } , config . server . https , https ( ) )
@@ -20,7 +30,11 @@ function viteBasicSslPlugin(): Plugin {
2030 }
2131}
2232
23- export async function getCertificate ( cacheDir : string ) {
33+ export async function getCertificate (
34+ cacheDir : string ,
35+ name ?: string ,
36+ domains ?: string [ ]
37+ ) {
2438 const cachePath = path . join ( cacheDir , '_cert.pem' )
2539
2640 try {
@@ -35,7 +49,10 @@ export async function getCertificate(cacheDir: string) {
3549
3650 return content
3751 } catch {
38- const content = ( await import ( './certificate' ) ) . createCertificate ( )
52+ const content = ( await import ( './certificate' ) ) . createCertificate (
53+ name ,
54+ domains
55+ )
3956 fsp
4057 . mkdir ( cacheDir , { recursive : true } )
4158 . then ( ( ) => fsp . writeFile ( cachePath , content ) )
0 commit comments