| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { createLogger, type PluginOption } from 'vite'
- import { PLUGIN_NAME } from './lib/constant'
- import { getDefaultHosts } from './lib/util'
- import Mkcert, { MkcertOptions } from './mkcert'
- export { BaseSource, type SourceInfo } from './mkcert/source'
- export type ViteCertificateOptions = MkcertOptions & {
- /**
- * The hosts that needs to generate the certificate.
- */
- hosts?: string[]
- }
- const plugin = (options: ViteCertificateOptions = {}): PluginOption => {
- return {
- name: PLUGIN_NAME,
- apply: 'serve',
- config: async ({ server = {}, logLevel }) => {
- if (server.https === false) {
- return
- }
- const { hosts = [], ...mkcertOptions } = options
- const logger = createLogger(logLevel, {
- prefix: PLUGIN_NAME
- })
- const mkcert = Mkcert.create({
- logger,
- ...mkcertOptions
- })
- await mkcert.init()
- const allHosts = [...getDefaultHosts(), ...hosts]
- if (typeof server.host === 'string') {
- allHosts.push(server.host)
- }
- const uniqueHosts = Array.from(new Set(allHosts)).filter(item => !!item)
- const certificate = await mkcert.install(uniqueHosts)
- return {
- server: {
- https: {
- ...certificate
- }
- },
- preview: {
- https: {
- ...certificate
- }
- }
- }
- }
- }
- }
- export default plugin
|