@@ -21,7 +21,10 @@ import { generateConsolePipeCode } from './virtual-console'
2121import type { ServerResponse } from 'node:http'
2222import type { Plugin } from 'vite'
2323import type { EditorConfig } from './editor'
24- import type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'
24+ import type {
25+ HttpServerLike ,
26+ ServerEventBusConfig ,
27+ } from '@tanstack/devtools-event-bus/server'
2528
2629export type ConsoleLevel = 'log' | 'warn' | 'error' | 'info' | 'debug'
2730
@@ -113,6 +116,8 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
113116
114117 let devtoolsFileId : string | null = null
115118 let devtoolsPort : number | null = null
119+ let devtoolsHost : string | null = null
120+ let devtoolsProtocol : 'http' | 'https' | null = null
116121
117122 return [
118123 {
@@ -170,9 +175,24 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
170175 async configureServer ( server ) {
171176 if ( serverBusEnabled ) {
172177 const preferredPort = args ?. eventBusConfig ?. port ?? 4206
178+ const isHttps = ! ! server . config . server . https
179+ const serverHost =
180+ typeof server . config . server . host === 'string'
181+ ? server . config . server . host
182+ : 'localhost'
183+
184+ devtoolsProtocol = isHttps ? 'https' : 'http'
185+ devtoolsHost = serverHost
186+
173187 const bus = new ServerEventBus ( {
174188 ...args ?. eventBusConfig ,
175189 port : preferredPort ,
190+ host : serverHost ,
191+ // When HTTPS is enabled, piggyback on Vite's server
192+ // so WebSocket/SSE connections share the same TLS certificate
193+ ...( isHttps && server . httpServer
194+ ? { httpServer : server . httpServer as HttpServerLike }
195+ : { } ) ,
176196 } )
177197 // start() now handles EADDRINUSE and returns the actual port
178198 devtoolsPort = await bus . start ( )
@@ -608,22 +628,42 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
608628 } ,
609629 } ,
610630 {
611- name : '@tanstack/devtools:port -injection' ,
631+ name : '@tanstack/devtools:connection -injection' ,
612632 apply ( config , { command } ) {
613633 return config . mode === 'development' && command === 'serve'
614634 } ,
615635 transform ( code , id ) {
616- // Only transform @tanstack packages that contain the port placeholder
617- if ( ! code . includes ( '__TANSTACK_DEVTOOLS_PORT__' ) ) return
636+ // Only transform @tanstack packages that contain the connection placeholders
637+ const hasPlaceholder =
638+ code . includes ( '__TANSTACK_DEVTOOLS_PORT__' ) ||
639+ code . includes ( '__TANSTACK_DEVTOOLS_HOST__' ) ||
640+ code . includes ( '__TANSTACK_DEVTOOLS_PROTOCOL__' )
641+ if ( ! hasPlaceholder ) return
618642 if (
619643 ! id . includes ( '@tanstack/devtools' ) &&
620644 ! id . includes ( '@tanstack/event-bus' )
621645 )
622646 return
623647
624- // Replace placeholder with actual port (or fallback to 4206 if not resolved yet )
648+ // Replace placeholders with actual values (or fallback defaults )
625649 const portValue = devtoolsPort ?? 4206
626- return code . replace ( / _ _ T A N S T A C K _ D E V T O O L S _ P O R T _ _ / g, String ( portValue ) )
650+ const hostValue = devtoolsHost ?? 'localhost'
651+ const protocolValue = devtoolsProtocol ?? 'http'
652+
653+ let result = code
654+ result = result . replace (
655+ / _ _ T A N S T A C K _ D E V T O O L S _ P O R T _ _ / g,
656+ String ( portValue ) ,
657+ )
658+ result = result . replace (
659+ / _ _ T A N S T A C K _ D E V T O O L S _ H O S T _ _ / g,
660+ JSON . stringify ( hostValue ) ,
661+ )
662+ result = result . replace (
663+ / _ _ T A N S T A C K _ D E V T O O L S _ P R O T O C O L _ _ / g,
664+ JSON . stringify ( protocolValue ) ,
665+ )
666+ return result
627667 } ,
628668 } ,
629669 ]
0 commit comments