@@ -63,31 +63,29 @@ export const remixDevTools: (args?:RemixViteConfig) => Plugin[] = (args) => {
6363 port = server . config . server . port ?? 5173 ;
6464 } ) ;
6565 server . middlewares . use ( ( req , res , next ) =>
66- {
67- handleDevToolsViteRequest ( req , res , next , ( parsedData ) => {
68- const { type, data } = parsedData ;
69- const id = data . id ;
70- const existingData = routeInfo . get ( id ) ;
71- if ( existingData ) {
72- if ( type === "loader" ) {
73- existingData . loader = cutArrayToLastN ( [ ...existingData . loader , data ] , 30 ) ;
74- }
75- if ( type === "action" ) {
76- existingData . action = cutArrayToLastN ( [ ...existingData . action , data ] , 30 ) ;
77- }
78- } else {
79- if ( type === "loader" ) {
80- routeInfo . set ( id , { loader : [ data ] , action : [ ] } ) ;
81- }
82- if ( type === "action" ) {
83- routeInfo . set ( id , { loader : [ ] , action : [ data ] } ) ;
84- }
66+ handleDevToolsViteRequest ( req , res , next , ( parsedData ) => {
67+ const { type, data } = parsedData ;
68+ const id = data . id ;
69+ const existingData = routeInfo . get ( id ) ;
70+ if ( existingData ) {
71+ if ( type === "loader" ) {
72+ existingData . loader = cutArrayToLastN ( [ ...existingData . loader , data ] , 30 ) ;
8573 }
86- server . hot . channels . forEach ( ( client ) => {
87- client . send ( "route-info" , JSON . stringify ( { type, data } ) ) ;
88- } ) ;
89- } )
90- }
74+ if ( type === "action" ) {
75+ existingData . action = cutArrayToLastN ( [ ...existingData . action , data ] , 30 ) ;
76+ }
77+ } else {
78+ if ( type === "loader" ) {
79+ routeInfo . set ( id , { loader : [ data ] , action : [ ] } ) ;
80+ }
81+ if ( type === "action" ) {
82+ routeInfo . set ( id , { loader : [ ] , action : [ data ] } ) ;
83+ }
84+ }
85+ server . hot . channels . forEach ( ( client ) => {
86+ client . send ( "route-info" , JSON . stringify ( { type, data } ) ) ;
87+ } ) ;
88+ } )
9189 ) ;
9290 server . hot . on ( "all-route-info" , ( data , client ) => {
9391 client . send ( "all-route-info" , JSON . stringify ( {
@@ -98,17 +96,19 @@ export const remixDevTools: (args?:RemixViteConfig) => Plugin[] = (args) => {
9896
9997 if ( ! server . config . isProduction ) {
10098 const { exec } = await import ( "node:child_process" ) ;
101-
99+ const openInVsCode = ( path : string , lineNum : string ) => {
100+ exec ( `code -g "${ normalizePath ( path ) } ${ lineNum } "` ) ;
101+ }
102+
102103 server . hot . on ( "open-source" , ( { data } : OpenSourceData ) => {
103104 const { source, line, routeID } = data ;
104105 const lineNum = line ? `:${ line } ` : "" ;
105106
106- if ( source ) {
107- exec ( `code -g "${ normalizePath ( source ) } ${ lineNum } "` ) ;
108- return ;
107+ if ( source ) {
108+ return openInVsCode ( source , lineNum ) ;
109109 }
110110
111- if ( ! source && routeID ) {
111+ if ( routeID ) {
112112 const routePath = path . join ( remixDir , routeID ) ;
113113 const checkedPath = checkPath ( routePath ) ;
114114
@@ -118,30 +118,29 @@ export const remixDevTools: (args?:RemixViteConfig) => Plugin[] = (args) => {
118118 const reactExtensions = [ "tsx" , "jsx" ] ;
119119 const allExtensions = [ "ts" , "js" , ...reactExtensions ] ;
120120 const isRoot = routeID === "root" ;
121+ const findFileByExtension = ( prefix : string , filePaths : string [ ] ) => {
122+ const file = filePaths . find ( file => allExtensions . some ( ext => file === `${ prefix } .${ ext } ` ) ) ;
123+ return file
124+ }
121125
122126 if ( isRoot ) {
123127 if ( ! fs . existsSync ( remixDir ) ) return ;
124128 const filesInRemixPath = fs . readdirSync ( remixDir ) ;
125- const rootFile = filesInRemixPath . find ( ( file ) => reactExtensions . some ( ( ext ) => file === `root.${ ext } ` ) ) ;
126-
127- rootFile && exec ( `code -g "${ path . join ( remixDir , rootFile ) } ${ lineNum } "` ) ;
129+ const rootFile = findFileByExtension ( "root" , filesInRemixPath ) ;
130+ rootFile && openInVsCode ( path . join ( remixDir , rootFile ) , lineNum ) ;
128131 return ;
129132 }
130133
131134 // If its not the root route, then we find the file or folder in the routes folder
132135 // We know that the route ID is in the form of "routes/contact" or "routes/user.profile" when is not root
133- // so the ID alraedy contains the "routes" segment, so we just need to find the file or folder in the routes folder
136+ // so the ID already contains the "routes" segment, so we just need to find the file or folder in the routes folder
134137 if ( type === "directory" ) {
135138 const filesInFolderRoute = fs . readdirSync ( validPath ) ;
136- const routeFile = filesInFolderRoute . find ( ( file ) =>
137- allExtensions . some ( ( ext ) => file === `route.${ ext } ` )
138- ) ;
139- routeFile && exec ( `code -g "${ path . join ( remixDir , routeID , routeFile ) } ${ lineNum } "` ) ;
139+ const routeFile = findFileByExtension ( "route" , filesInFolderRoute ) ;
140+ routeFile && openInVsCode ( path . join ( remixDir , routeID , routeFile ) , lineNum ) ;
140141 return ;
141142 }
142-
143- exec ( `code -g "${ path . join ( validPath ) } ${ lineNum } "` ) ;
144- return ;
143+ return openInVsCode ( path . join ( validPath ) , lineNum ) ;
145144 }
146145 } ) ;
147146 }
0 commit comments