Skip to content

Commit b671be7

Browse files
authored
Merge pull request #56 from jbraband/different-hmr-protocol
Account for an hmr protocol different than the vite server's protocol
2 parents cb54075 + c0d9b39 commit b671be7

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ interface LaravelPlugin extends Plugin {
8383
config: (config: UserConfig, env: ConfigEnv) => UserConfig
8484
}
8585

86+
type DevServerUrl = `${'http'|'https'}://${string}:${number}`
87+
8688
let exitHandlersBound = false
8789

8890
export const refreshPaths = [
@@ -109,7 +111,7 @@ export default function laravel(config: string|string[]|PluginConfig): [LaravelP
109111
* Resolve the Laravel Plugin configuration.
110112
*/
111113
function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlugin {
112-
let viteDevServerUrl: string
114+
let viteDevServerUrl: DevServerUrl
113115
let resolvedConfig: ResolvedConfig
114116
const cssManifest: Manifest = {}
115117

@@ -178,12 +180,7 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
178180

179181
const isAddressInfo = (x: string|AddressInfo|null|undefined): x is AddressInfo => typeof x === 'object'
180182
if (isAddressInfo(address)) {
181-
const protocol = server.config.server.https ? 'https' : 'http'
182-
const configHmrHost = typeof server.config.server.hmr === 'object' ? server.config.server.hmr.host : null
183-
const configHost = typeof server.config.server.host === 'string' ? server.config.server.host : null
184-
const serverAddress = address.family === 'IPv6' ? `[${address.address}]` : address.address
185-
const host = configHmrHost ?? configHost ?? serverAddress
186-
viteDevServerUrl = `${protocol}://${host}:${address.port}`
183+
viteDevServerUrl = resolveDevServerUrl(address, server.config)
187184
fs.writeFileSync(hotFile, viteDevServerUrl)
188185

189186
const envDir = resolvedConfig.envDir || process.cwd()
@@ -400,6 +397,23 @@ function resolveFullReloadConfig({ refresh: config }: Required<PluginConfig>): P
400397
})
401398
}
402399

400+
/**
401+
* Resolve the dev server URL from the server address and configuration.
402+
*/
403+
function resolveDevServerUrl(address: AddressInfo, config: ResolvedConfig): DevServerUrl {
404+
const configHmrProtocol = typeof config.server.hmr === 'object' ? config.server.hmr.protocol : null
405+
const clientProtocol = configHmrProtocol ? (configHmrProtocol === 'wss' ? 'https' : 'http') : null
406+
const serverProtocol = config.server.https ? 'https' : 'http'
407+
const protocol = clientProtocol ?? serverProtocol
408+
409+
const configHmrHost = typeof config.server.hmr === 'object' ? config.server.hmr.host : null
410+
const configHost = typeof config.server.host === 'string' ? config.server.host : null
411+
const serverAddress = address.family === 'IPv6' ? `[${address.address}]` : address.address
412+
const host = configHmrHost ?? configHost ?? serverAddress
413+
414+
return `${protocol}://${host}:${address.port}`
415+
}
416+
403417
/**
404418
* Add the Interia helpers to the list of SSR dependencies that aren't externalized.
405419
*

0 commit comments

Comments
 (0)