Skip to content

Commit 7d31353

Browse files
authored
enhanced browser logs (#205)
* enhanced browser logs * fix? * fix * version bump
1 parent f63ed09 commit 7d31353

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-router-devtools",
33
"description": "Devtools for React Router - debug, trace, find hydration errors, catch bugs and inspect server/client data with react-router-devtools",
44
"author": "Alem Tuzlak",
5-
"version": "5.0.1",
5+
"version": "5.0.2",
66
"license": "MIT",
77
"keywords": [
88
"react-router",

src/client/components/RouteSegmentInfo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export const RouteSegmentInfo = ({ route, i }: { route: UIMatch<unknown, unknown
142142
<button
143143
type="button"
144144
data-testid={`${route.id}-show-route-boundaries`}
145-
className="rounded border border-green-600 rounded border border-[#1F9CF0] px-2.5 py-0.5 text-sm font-medium text-green-600"
145+
className="border-green-600 rounded border px-2.5 py-0.5 text-sm font-medium text-green-600"
146146
onClick={() => {
147147
const routeId = route.id === "root" ? "root" : i.toString()
148148
if (routeId !== settings.hoveredRoute) {

src/vite/plugin.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
6262
const includeClient = args?.includeInProd?.client ?? false
6363
const includeServer = args?.includeInProd?.server ?? false
6464
const includeDevtools = args?.includeInProd?.devTools ?? false
65-
65+
let port = 5173
6666
let routes: Route[] = []
6767
let flatRoutes: Route[] = []
6868
const appDir = args?.appDir || "./app"
@@ -235,15 +235,30 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
235235
if (server.config.appType !== "custom") {
236236
return
237237
}
238+
if (server.config.server.port) {
239+
process.rdt_port = server.config.server.port ?? 5173
240+
port = process.rdt_port
241+
}
242+
238243
server.httpServer?.on("listening", () => {
239244
process.rdt_port = server.config.server.port ?? 5173
245+
port = process.rdt_port
240246
})
241247
//@ts-ignore - vite 5/6 compat
242248
const channel = server.hot.channels.find((channel) => channel.name === "ws") ?? server.environments?.client.hot
243-
249+
const editor = args?.editor ?? DEFAULT_EDITOR_CONFIG
250+
const openInEditor = async (path: string | undefined, lineNum: string | undefined) => {
251+
if (!path) {
252+
return
253+
}
254+
editor.open(path, lineNum)
255+
}
244256
server.middlewares.use((req, res, next) =>
245257
handleDevToolsViteRequest(req, res, next, (parsedData) => {
246258
const { type, data, routine } = parsedData
259+
if (routine === "open-source") {
260+
return handleOpenSource({ data: { type: data.type, data }, openInEditor, appDir })
261+
}
247262
if (routine === "request-event") {
248263
unusedEvents.set(parsedData.id + parsedData.startTime, parsedData)
249264
for (const client of server.hot.channels) {
@@ -295,7 +310,7 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
295310
})
296311

297312
if (!server.config.isProduction) {
298-
channel?.on("remove-event", (data, client) => {
313+
channel?.on("remove-event", (data) => {
299314
const parsedData = data
300315
const { id, startTime } = parsedData
301316

@@ -319,13 +334,6 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
319334
})
320335
)
321336
})
322-
const editor = args?.editor ?? DEFAULT_EDITOR_CONFIG
323-
const openInEditor = async (path: string | undefined, lineNum: string | undefined) => {
324-
if (!path) {
325-
return
326-
}
327-
editor.open(path, lineNum)
328-
}
329337

330338
server.hot.on("open-source", (data: OpenSourceData) => handleOpenSource({ data, openInEditor, appDir }))
331339
server.hot.on("add-route", (data: WriteFileData) => handleWriteFile({ ...data, openInEditor }))
@@ -362,7 +370,7 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
362370
}
363371

364372
const column = line.indexOf("console.")
365-
const logMessage = `"${chalk.magenta("LOG")} ${chalk.blueBright(`${id.replace(normalizePath(process.cwd()), "")}:${lineNumber + 1}:${column + 1}`)}\\n → "`
373+
const logMessage = `'${chalk.magenta("LOG")} ${chalk.blueBright(`http://localhost:${port}/open-source?source=${encodeURIComponent(id.replace(normalizePath(process.cwd()), ""))}&line=${lineNumber + 1}&column=${column + 1}`)}\\n → '`
366374
if (line.includes("console.log(")) {
367375
const newLine = `console.log(${logMessage},`
368376
return line.replace("console.log(", newLine)

src/vite/utils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IncomingMessage, ServerResponse } from "node:http"
2-
import type { Connect } from "vite"
2+
import { type Connect, normalizePath } from "vite"
33

44
export async function processPlugins(pluginDirectoryPath: string) {
55
const fs = await import("node:fs")
@@ -28,6 +28,27 @@ export const handleDevToolsViteRequest = (
2828
next: Connect.NextFunction,
2929
cb: (data: any) => void
3030
) => {
31+
if (req.url?.includes("open-source")) {
32+
const searchParams = new URLSearchParams(req.url.split("?")[1])
33+
const source = searchParams.get("source")
34+
const line = searchParams.get("line")
35+
const column = searchParams.get("column")
36+
cb({
37+
type: "open-source",
38+
routine: "open-source",
39+
data: {
40+
source: source ? normalizePath(`${process.cwd()}/${source}`) : undefined,
41+
line,
42+
column,
43+
},
44+
})
45+
res.setHeader("Content-Type", "text/html")
46+
res.write(`<script>
47+
window.close();
48+
</script>`)
49+
res.end()
50+
return
51+
}
3152
if (!req.url?.includes("react-router-devtools-request")) {
3253
return next()
3354
}

test-apps/react-router-vite/app/routes/_index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default function Index() {
7777
data.append("person.name", "test1");
7878
data.append("person.surname", "test1");
7979
data.append("obj", JSON.stringify({ test: "test" }));
80+
console.log(data);
8081
return (
8182
<div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.8" }}>
8283
<h1>Welcome to Remix</h1>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function Index() {
2+
console.log("Epic test")
3+
return <div>
4+
Epic test
5+
</div>
6+
}

0 commit comments

Comments
 (0)