Skip to content

Commit 6ce866d

Browse files
committed
feature: add more types that get logged via console methods
This PR adds more special logic for some data types that get logged via `console` methods. Types like `Promise`, `Date`, `WeakMaps`, and some more, now get logged correctly (or at least, better than they used to). This PR also fixes a sinister bug - the `type` of the `ConsoleAPICalled` events don't match 1:1 with actual console methods (eg: `console.warn` message type is `warning`). This PR adds a mapping between those types and method names. Some methods don't seem to have a message type, I'm not sure why, but we'll get to them later.
1 parent 5b7f46b commit 6ce866d

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

.changeset/strange-melons-provide.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feature: add more types that get logged via `console` methods
6+
7+
This PR adds more special logic for some data types that get logged via `console` methods. Types like `Promise`, `Date`, `WeakMaps`, and some more, now get logged correctly (or at least, better than they used to).
8+
9+
This PR also fixes a sinister bug - the `type` of the `ConsoleAPICalled` events don't match 1:1 with actual console methods (eg: `console.warn` message type is `warning`). This PR adds a mapping between those types and method names. Some methods don't seem to have a message type, I'm not sure why, but we'll get to them later.

packages/wrangler/src/dev.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ function useWorker(props: {
569569
if (!startedRef.current) {
570570
startedRef.current = true;
571571
} else {
572-
console.log("⎔ Detected changes, restarting server...");
572+
console.log("⎔ Detected changes, restarted server.");
573573
}
574574

575575
const assets = await syncAssets(

packages/wrangler/src/inspect.ts

+50-12
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,30 @@ function randomId(): string {
432432
* we're just doing a little bit of the work of the devtools console,
433433
* directly in the terminal.
434434
*/
435+
436+
export const mapConsoleAPIMessageTypeToConsoleMethod: {
437+
[key in Protocol.Runtime.ConsoleAPICalledEvent["type"]]: keyof typeof console;
438+
} = {
439+
log: "log",
440+
debug: "debug",
441+
info: "info",
442+
warning: "warn",
443+
error: "error",
444+
dir: "dir",
445+
dirxml: "dirxml",
446+
table: "table",
447+
trace: "trace",
448+
clear: "clear",
449+
count: "count",
450+
assert: "assert",
451+
profile: "profile",
452+
profileEnd: "profileEnd",
453+
timeEnd: "timeEnd",
454+
startGroup: "group",
455+
startGroupCollapsed: "groupCollapsed",
456+
endGroup: "groupEnd",
457+
};
458+
435459
function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
436460
const args: string[] = [];
437461
for (const ro of evt.args) {
@@ -449,7 +473,11 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
449473
break;
450474
case "object":
451475
if (!ro.preview) {
452-
args.push(ro.description ?? "<no-description>");
476+
args.push(
477+
ro.subtype === "null"
478+
? "null"
479+
: ro.description ?? "<no-description>"
480+
);
453481
} else {
454482
args.push(ro.preview.description ?? "<no-description>");
455483

@@ -467,6 +495,7 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
467495
);
468496

469497
break;
498+
case "weakmap":
470499
case "map":
471500
args.push(
472501
"{\n" +
@@ -484,6 +513,7 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
484513
);
485514

486515
break;
516+
case "weakset":
487517
case "set":
488518
args.push(
489519
"{ " +
@@ -497,27 +527,33 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
497527
(ro.preview.overflow ? ", ..." : "") +
498528
" }"
499529
);
500-
501530
break;
502-
case "null":
503-
args.push("null");
504-
break;
505-
case "node":
506531
case "regexp":
532+
break;
507533
case "date":
508-
case "weakmap":
509-
case "weakset":
510-
case "iterator":
534+
break;
511535
case "generator":
512-
case "error":
513-
case "proxy":
536+
args.push(ro.preview.properties[0].value || "");
537+
break;
514538
case "promise":
539+
if (ro.preview.properties[0].value === "pending") {
540+
args.push(`{<${ro.preview.properties[0].value}>}`);
541+
} else {
542+
args.push(
543+
`{<${ro.preview.properties[0].value}>: ${ro.preview.properties[1].value}}`
544+
);
545+
}
546+
break;
547+
case "node":
548+
case "iterator":
549+
case "proxy":
515550
case "typedarray":
516551
case "arraybuffer":
517552
case "dataview":
518553
case "webassemblymemory":
519554
case "wasmvalue":
520555
break;
556+
case "error":
521557
default:
522558
// just a pojo
523559
args.push(
@@ -539,5 +575,7 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
539575
}
540576
}
541577

542-
console[evt.type](...args);
578+
const method = mapConsoleAPIMessageTypeToConsoleMethod[evt.type];
579+
//@ts-expect-error I dunno how to make this type pass lol
580+
console[method](...args);
543581
}

0 commit comments

Comments
 (0)