File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 11import fs from 'fs'
22import path from 'path'
3+ import type { AddressInfo } from 'net'
34import {
45 type InlineConfig ,
56 type ResolvedConfig ,
67 type Plugin ,
8+ type ViteDevServer ,
79 mergeConfig ,
810 normalizePath ,
911} from 'vite'
@@ -150,3 +152,43 @@ checkPkgMain.buildElectronMainPlugin = function buildElectronMainPlugin(runtime:
150152 } ,
151153 }
152154}
155+
156+ /**
157+ * @see https://github.com/vitejs/vite/blob/c3f6731bafeadd310efa4325cb8dcc639636fe48/packages/vite/src/node/constants.ts#L131-L141
158+ */
159+ export function resolveHostname ( hostname : string ) {
160+ const loopbackHosts = new Set ( [
161+ 'localhost' ,
162+ '127.0.0.1' ,
163+ '::1' ,
164+ '0000:0000:0000:0000:0000:0000:0000:0001'
165+ ] )
166+ const wildcardHosts = new Set ( [
167+ '0.0.0.0' ,
168+ '::' ,
169+ '0000:0000:0000:0000:0000:0000:0000:0000'
170+ ] )
171+
172+ return loopbackHosts . has ( hostname ) || wildcardHosts . has ( hostname ) ? 'localhost' : hostname
173+ }
174+
175+ export function resolveEnv ( server : ViteDevServer ) {
176+ const addressInfo = server . httpServer . address ( )
177+ const isAddressInfo = ( x : any ) : x is AddressInfo => x ?. address
178+
179+ if ( isAddressInfo ( addressInfo ) ) {
180+ const { address, port } = addressInfo
181+ const host = resolveHostname ( address )
182+
183+ const options = server . config . server
184+ const protocol = options . https ? 'https' : 'http'
185+ const devBase = server . config . base
186+
187+ const path = typeof options . open === 'string' ? options . open : devBase
188+ const url = path . startsWith ( 'http' )
189+ ? path
190+ : `${ protocol } ://${ host } :${ port } ${ path } `
191+
192+ return { url, host, port }
193+ }
194+ }
You can’t perform that action at this time.
0 commit comments