11import fs from 'fs'
22import { AddressInfo } from 'net'
3+ import os from 'os'
34import path from 'path'
45import colors from 'picocolors'
56import { Plugin , loadEnv , UserConfig , ConfigEnv , ResolvedConfig , SSROptions , PluginOption } from 'vite'
@@ -51,6 +52,13 @@ interface PluginConfig {
5152 * @default false
5253 */
5354 refresh ?: boolean | string | string [ ] | RefreshConfig | RefreshConfig [ ]
55+
56+ /**
57+ * Utilise the valet TLS certificates.
58+ *
59+ * @default false
60+ */
61+ valetTls ?: string | boolean ,
5462}
5563
5664interface RefreshConfig {
@@ -106,6 +114,9 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
106114 const ssr = ! ! userConfig . build ?. ssr
107115 const env = loadEnv ( mode , userConfig . envDir || process . cwd ( ) , '' )
108116 const assetUrl = env . ASSET_URL ?? ''
117+ const valetServerConfig = command === 'serve'
118+ ? resolveValetServerConfig ( pluginConfig . valetTls )
119+ : undefined
109120
110121 ensureCommandShouldRunInEnvironment ( command , env )
111122
@@ -126,7 +137,18 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
126137 host : userConfig . server ?. host ?? '0.0.0.0' ,
127138 port : userConfig . server ?. port ?? ( env . VITE_PORT ? parseInt ( env . VITE_PORT ) : 5173 ) ,
128139 strictPort : userConfig . server ?. strictPort ?? true ,
129- } : undefined )
140+ } : undefined ) ,
141+ ...( valetServerConfig ? {
142+ host : userConfig . server ?. host ?? valetServerConfig . host ,
143+ hmr : userConfig . server ?. hmr === false ? false : {
144+ ...valetServerConfig . hmr ,
145+ ...( userConfig . server ?. hmr === true ? { } : userConfig . server ?. hmr ) ,
146+ } ,
147+ https : userConfig . server ?. https === false ? false : {
148+ ...valetServerConfig . https ,
149+ ...( userConfig . server ?. https === true ? { } : userConfig . server ?. https ) ,
150+ } ,
151+ } : undefined ) ,
130152 } ,
131153 resolve : {
132154 alias : Array . isArray ( userConfig . resolve ?. alias )
@@ -303,7 +325,8 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required<Plu
303325 ssr : config . ssr ?? config . input ,
304326 ssrOutputDirectory : config . ssrOutputDirectory ?? 'bootstrap/ssr' ,
305327 refresh : config . refresh ?? false ,
306- hotFile : config . hotFile ?? path . join ( ( config . publicDirectory ?? 'public' ) , 'hot' )
328+ hotFile : config . hotFile ?? path . join ( ( config . publicDirectory ?? 'public' ) , 'hot' ) ,
329+ valetTls : config . valetTls ?? false ,
307330 }
308331}
309332
@@ -417,3 +440,49 @@ function noExternalInertiaHelpers(config: UserConfig): true|Array<string|RegExp>
417440 ...pluginNoExternal ,
418441 ]
419442}
443+
444+ /**
445+ * Resolve the valet server config for the given host.
446+ */
447+ function resolveValetServerConfig ( host : string | boolean ) : {
448+ hmr ?: { host : string }
449+ host ?: string ,
450+ https ?: { cert : Buffer , key : Buffer }
451+ } | undefined {
452+ if ( host === false ) {
453+ return
454+ }
455+
456+ host = host === true ? resolveValetHost ( ) : host
457+
458+ const keyPath = path . resolve ( os . homedir ( ) , `.config/valet/Certificates/${ host } .key` )
459+ const certPath = path . resolve ( os . homedir ( ) , `.config/valet/Certificates/${ host } .crt` )
460+
461+ if ( ! fs . existsSync ( keyPath ) || ! fs . existsSync ( certPath ) ) {
462+ throw Error ( `Unable to find Valet certificate files for your host [${ host } ]. Ensure you have run "valet secure".` )
463+ }
464+
465+ return {
466+ hmr : { host } ,
467+ host,
468+ https : {
469+ key : fs . readFileSync ( keyPath ) ,
470+ cert : fs . readFileSync ( certPath ) ,
471+ } ,
472+ }
473+ }
474+
475+ /**
476+ * Resolve the valet valet host for the current directory.
477+ */
478+ function resolveValetHost ( ) : string {
479+ const configPath = os . homedir ( ) + `/.config/valet/config.json`
480+
481+ if ( ! fs . existsSync ( configPath ) ) {
482+ throw Error ( 'Unable to find the Valet configuration file. You will need to manually specify the host in the `valetTls` configuration option.' )
483+ }
484+
485+ const config : { tld : string } = JSON . parse ( fs . readFileSync ( configPath , 'utf-8' ) )
486+
487+ return path . basename ( process . cwd ( ) ) + '.' + config . tld
488+ }
0 commit comments