Skip to content

Commit

Permalink
feat: support custom domains (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
gz65555 authored Jan 15, 2024
1 parent 17ddf64 commit 0a8665f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 8 additions & 3 deletions src/certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -108,15 +112,16 @@ export function createCertificate(): string {
{
type: 7,
ip: 'fe80::1'
}
},
...appendDomains
]
}
]

const attrs = [
{
name: 'commonName',
value: 'example.org'
value: name
},
{
name: 'countryName',
Expand Down
25 changes: 21 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Options>): 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())
Expand All @@ -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 {
Expand All @@ -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))
Expand Down

0 comments on commit 0a8665f

Please sign in to comment.