Skip to content

Commit c0d9b39

Browse files
committed
Refactoring
1 parent db87736 commit c0d9b39

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/index.ts

Lines changed: 21 additions & 12 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
/**
@@ -100,7 +102,7 @@ export default function laravel(config: string|string[]|PluginConfig): [LaravelP
100102
}
101103

102104
function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlugin {
103-
let viteDevServerUrl: string
105+
let viteDevServerUrl: DevServerUrl
104106
let resolvedConfig: ResolvedConfig
105107
const cssManifest: Manifest = {}
106108

@@ -169,17 +171,7 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
169171

170172
const isAddressInfo = (x: string|AddressInfo|null|undefined): x is AddressInfo => typeof x === 'object'
171173
if (isAddressInfo(address)) {
172-
const configHmrProtocol = typeof server.config.server.hmr === 'object' ? server.config.server.hmr.protocol : null
173-
const clientHmrProtocol = configHmrProtocol ? (configHmrProtocol === 'wss' ? 'https' : 'http') : null
174-
const configProtocol = server.config.server.https ? 'https' : 'http'
175-
const protocol = clientHmrProtocol ?? configProtocol
176-
177-
const configHmrHost = typeof server.config.server.hmr === 'object' ? server.config.server.hmr.host : null
178-
const configHost = typeof server.config.server.host === 'string' ? server.config.server.host : null
179-
const serverAddress = address.family === 'IPv6' ? `[${address.address}]` : address.address
180-
const host = configHmrHost ?? configHost ?? serverAddress
181-
182-
viteDevServerUrl = `${protocol}://${host}:${address.port}`
174+
viteDevServerUrl = resolveDevServerUrl(address, server.config)
183175
fs.writeFileSync(hotFile, viteDevServerUrl)
184176

185177
const envDir = resolvedConfig.envDir || process.cwd()
@@ -396,6 +388,23 @@ function resolveFullReloadConfig({ refresh: config }: Required<PluginConfig>): P
396388
})
397389
}
398390

391+
/**
392+
* Resolve the dev server URL from the server address and configuration.
393+
*/
394+
function resolveDevServerUrl(address: AddressInfo, config: ResolvedConfig): DevServerUrl {
395+
const configHmrProtocol = typeof config.server.hmr === 'object' ? config.server.hmr.protocol : null
396+
const clientProtocol = configHmrProtocol ? (configHmrProtocol === 'wss' ? 'https' : 'http') : null
397+
const serverProtocol = config.server.https ? 'https' : 'http'
398+
const protocol = clientProtocol ?? serverProtocol
399+
400+
const configHmrHost = typeof config.server.hmr === 'object' ? config.server.hmr.host : null
401+
const configHost = typeof config.server.host === 'string' ? config.server.host : null
402+
const serverAddress = address.family === 'IPv6' ? `[${address.address}]` : address.address
403+
const host = configHmrHost ?? configHost ?? serverAddress
404+
405+
return `${protocol}://${host}:${address.port}`
406+
}
407+
399408
/**
400409
* Add the Interia helpers to the list of SSR dependencies that aren't externalized.
401410
*

0 commit comments

Comments
 (0)