Last active
June 2, 2025 21:06
-
-
Save soukoku/40ac2b6574b757aba3b8afb834fcca4e to your computer and use it in GitHub Desktop.
Use dotnet cert for vite dev server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
declare module '*.vue' { | |
import type { DefineComponent } from 'vue' | |
const component: DefineComponent<{}, {}, any> | |
export default component | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs' | |
import path from 'path' | |
import child_process from 'child_process' | |
import { env } from 'process' | |
import pkg from './package.json' with { type: 'json' } | |
const isDev = process.env.NODE_ENV === 'development' | |
let keyFilePath = '' | |
let certFilePath = '' | |
if (isDev) { | |
const baseFolder = | |
env.APPDATA !== undefined && env.APPDATA !== '' | |
? `${env.APPDATA}/ASP.NET/https` | |
: `${env.HOME}/.aspnet/https` | |
const certificateName = pkg.name || 'appname' | |
certFilePath = path.join(baseFolder, `${certificateName}.pem`) | |
keyFilePath = path.join(baseFolder, `${certificateName}.key`) | |
if (!fs.existsSync(certFilePath) || !fs.existsSync(keyFilePath)) { | |
fs.mkdirSync(baseFolder, { recursive: true }) | |
if ( | |
0 !== | |
child_process.spawnSync( | |
'dotnet', | |
['dev-certs', 'https', '--export-path', certFilePath, '--format', 'Pem', '--no-password', '--trust'], | |
{ stdio: 'inherit' } | |
).status | |
) { | |
throw new Error('Could not create certificate.') | |
} | |
} | |
} | |
// https://vitejs.dev/config/ | |
export default defineConfig({ | |
server: { | |
https: { | |
key: keyFilePath ? fs.readFileSync(keyFilePath) : undefined, | |
cert: certFilePath ? fs.readFileSync(certFilePath) : undefined | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment