@@ -4,11 +4,21 @@ import type { Plugin } from 'vite'
4
4
5
5
const defaultCacheDir = 'node_modules/.vite'
6
6
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 {
8
14
return {
9
15
name : 'vite:basic-ssl' ,
10
16
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
+ )
12
22
const https = ( ) => ( { cert : certificate , key : certificate } )
13
23
if ( config . server . https === undefined || ! ! config . server . https ) {
14
24
config . server . https = Object . assign ( { } , config . server . https , https ( ) )
@@ -20,7 +30,11 @@ function viteBasicSslPlugin(): Plugin {
20
30
}
21
31
}
22
32
23
- export async function getCertificate ( cacheDir : string ) {
33
+ export async function getCertificate (
34
+ cacheDir : string ,
35
+ name ?: string ,
36
+ domains ?: string [ ]
37
+ ) {
24
38
const cachePath = path . join ( cacheDir , '_cert.pem' )
25
39
26
40
try {
@@ -35,7 +49,10 @@ export async function getCertificate(cacheDir: string) {
35
49
36
50
return content
37
51
} catch {
38
- const content = ( await import ( './certificate' ) ) . createCertificate ( )
52
+ const content = ( await import ( './certificate' ) ) . createCertificate (
53
+ name ,
54
+ domains
55
+ )
39
56
fsp
40
57
. mkdir ( cacheDir , { recursive : true } )
41
58
. then ( ( ) => fsp . writeFile ( cachePath , content ) )
0 commit comments