Skip to content

Commit 0a8665f

Browse files
authored
feat: support custom domains (#22)
1 parent 17ddf64 commit 0a8665f

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ import basicSsl from '@vitejs/plugin-basic-ssl'
1212

1313
export default {
1414
plugins: [
15-
basicSsl()
15+
basicSsl({
16+
/** name of certification */
17+
name: 'test',
18+
/** custom trust domains */
19+
domains: ['*.custom.com'],
20+
/** custom certification directory */
21+
certDir: '/Users/.../.devServer/cert'
22+
})
1623
]
1724
}
1825
```
19-
26+
2027
## License
2128

2229
MIT

src/certificate.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ function toPositiveHex(hexString: string) {
5252
return mostSignificativeHexAsInt.toString() + hexString.substring(1)
5353
}
5454

55-
export function createCertificate(): string {
55+
export function createCertificate(name: string = 'example.org', domains?: string[]): string {
5656
const days = 30
5757
const keySize = 2048
5858

59+
const appendDomains = domains
60+
? domains.map(item => ({ type: 2, value: item }))
61+
: []
62+
5963
const extensions = [
6064
// {
6165
// name: 'basicConstraints',
@@ -108,15 +112,16 @@ export function createCertificate(): string {
108112
{
109113
type: 7,
110114
ip: 'fe80::1'
111-
}
115+
},
116+
...appendDomains
112117
]
113118
}
114119
]
115120

116121
const attrs = [
117122
{
118123
name: 'commonName',
119-
value: 'example.org'
124+
value: name
120125
},
121126
{
122127
name: 'countryName',

src/index.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ import type { Plugin } from 'vite'
44

55
const 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

Comments
 (0)