Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom domains #22

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,19 +4,33 @@ 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 })
config.server.https = Object.assign({}, config.server.https, https())
config.preview.https = Object.assign({}, config.preview.https, https())
}
}
}

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 @@ -31,7 +45,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