@@ -12,6 +12,7 @@ import { DEFAULT_EDITOR_CONFIG, handleOpenSource } from './editor'
1212import { removeDevtools } from './remove-devtools'
1313import { addSourceToJsx } from './inject-source'
1414import { enhanceConsoleLog } from './enhance-logs'
15+ import type { OutdatedDeps } from '@tanstack/devtools-client'
1516import type { Plugin } from 'vite'
1617import type { EditorConfig } from './editor'
1718import type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'
@@ -24,7 +25,13 @@ export type TanStackDevtoolsViteConfig = {
2425 /**
2526 * The configuration options for the server event bus
2627 */
27- eventBusConfig ?: ServerEventBusConfig
28+ eventBusConfig ?: ServerEventBusConfig & {
29+ /**
30+ * Should the server event bus be enabled or not
31+ * @default true
32+ */
33+ enabled ?: boolean // defaults to true
34+ }
2835 /**
2936 * Configuration for enhanced logging.
3037 */
@@ -61,33 +68,33 @@ export type TanStackDevtoolsViteConfig = {
6168export const defineDevtoolsConfig = ( config : TanStackDevtoolsViteConfig ) =>
6269 config
6370
64- export const devtools = async (
65- args ?: TanStackDevtoolsViteConfig ,
66- ) : Promise < Array < Plugin > > => {
71+ const emitOutdatedDeps = async ( ) => {
72+ return await new Promise < OutdatedDeps | null > ( ( resolve ) => {
73+ exec ( 'npm outdated --json' , ( _ , stdout ) => {
74+ // npm outdated exits with code 1 if there are outdated packages, but still outputs valid JSON
75+ if ( stdout ) {
76+ const newOutdatedDeps = tryParseJson < OutdatedDeps > ( stdout )
77+ if ( ! newOutdatedDeps ) {
78+ return
79+ }
80+ devtoolsEventClient . emit ( 'outdated-deps-read' , {
81+ outdatedDeps : newOutdatedDeps ,
82+ } )
83+ resolve ( newOutdatedDeps )
84+ }
85+ } )
86+ } )
87+ }
88+
89+ export const devtools = ( args ?: TanStackDevtoolsViteConfig ) : Array < Plugin > => {
6790 let port = 5173
6891 const logging = args ?. logging ?? true
6992 const enhancedLogsConfig = args ?. enhancedLogs ?? { enabled : true }
7093 const injectSourceConfig = args ?. injectSource ?? { enabled : true }
7194 const removeDevtoolsOnBuild = args ?. removeDevtoolsOnBuild ?? true
95+ const serverBusEnabled = args ?. eventBusConfig ?. enabled ?? true
7296 const bus = new ServerEventBus ( args ?. eventBusConfig )
73- const packageJson = await readPackageJson ( )
74- let outdatedDeps : any = null
75- exec ( 'npm outdated --json' , ( _ , stdout ) => {
76- // npm outdated exits with code 1 if there are outdated packages, but still outputs valid JSON
77- if ( stdout ) {
78- outdatedDeps = tryParseJson ( stdout )
79- devtoolsEventClient . emit ( 'ready' , {
80- packageJson,
81- outdatedDeps,
82- } )
83- }
84- } )
85- devtoolsEventClient . on ( 'mounted' , ( ) => {
86- devtoolsEventClient . emit ( 'ready' , {
87- packageJson,
88- outdatedDeps,
89- } )
90- } )
97+
9198 return [
9299 {
93100 enforce : 'pre' ,
@@ -142,7 +149,9 @@ export const devtools = async (
142149 return config . mode === 'development'
143150 } ,
144151 configureServer ( server ) {
145- bus . start ( )
152+ if ( serverBusEnabled ) {
153+ bus . start ( )
154+ }
146155
147156 server . middlewares . use ( ( req , _res , next ) => {
148157 if ( req . socket . localPort && req . socket . localPort !== port ) {
@@ -208,12 +217,37 @@ export const devtools = async (
208217 } ,
209218 } ,
210219 {
211- name : '@tanstack/devtools:listener' ,
220+ name : '@tanstack/devtools:event-client-setup' ,
221+ apply ( config , { command } ) {
222+ if (
223+ process . env . CI ||
224+ process . env . NODE_ENV !== 'development' ||
225+ command !== 'serve'
226+ )
227+ return false
228+ return config . mode === 'development'
229+ } ,
230+ async configureServer ( ) {
231+ const packageJson = await readPackageJson ( )
232+ const outdatedDeps = emitOutdatedDeps ( ) . then ( ( deps ) => deps )
233+
234+ // whenever a client mounts we send all the current info to the subscribers
235+ devtoolsEventClient . on ( 'mounted' , async ( ) => {
236+ devtoolsEventClient . emit ( 'outdated-deps-read' , {
237+ outdatedDeps : await outdatedDeps ,
238+ } )
239+ devtoolsEventClient . emit ( 'package-json-read' , {
240+ packageJson,
241+ } )
242+ } )
243+ } ,
212244 async handleHotUpdate ( { file } ) {
213245 if ( file . endsWith ( 'package.json' ) ) {
214- console . log ( packageJson )
215246 const newPackageJson = await readPackageJson ( )
216- console . log ( newPackageJson )
247+ devtoolsEventClient . emit ( 'package-json-read' , {
248+ packageJson : newPackageJson ,
249+ } )
250+ emitOutdatedDeps ( )
217251 }
218252 } ,
219253 } ,
0 commit comments