Skip to content

Commit aff14b6

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 71b0fab commit aff14b6

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-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

+59-12
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,33 @@ 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"]]: Exclude<
438+
keyof Console,
439+
"Console"
440+
>;
441+
} = {
442+
log: "log",
443+
debug: "debug",
444+
info: "info",
445+
warning: "warn",
446+
error: "error",
447+
dir: "dir",
448+
dirxml: "dirxml",
449+
table: "table",
450+
trace: "trace",
451+
clear: "clear",
452+
count: "count",
453+
assert: "assert",
454+
profile: "profile",
455+
profileEnd: "profileEnd",
456+
timeEnd: "timeEnd",
457+
startGroup: "group",
458+
startGroupCollapsed: "groupCollapsed",
459+
endGroup: "groupEnd",
460+
};
461+
435462
function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
436463
const args: string[] = [];
437464
for (const ro of evt.args) {
@@ -449,7 +476,11 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
449476
break;
450477
case "object":
451478
if (!ro.preview) {
452-
args.push(ro.description ?? "<no-description>");
479+
args.push(
480+
ro.subtype === "null"
481+
? "null"
482+
: ro.description ?? "<no-description>"
483+
);
453484
} else {
454485
args.push(ro.preview.description ?? "<no-description>");
455486

@@ -467,6 +498,7 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
467498
);
468499

469500
break;
501+
case "weakmap":
470502
case "map":
471503
args.push(
472504
"{\n" +
@@ -484,6 +516,7 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
484516
);
485517

486518
break;
519+
case "weakset":
487520
case "set":
488521
args.push(
489522
"{ " +
@@ -497,27 +530,33 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
497530
(ro.preview.overflow ? ", ..." : "") +
498531
" }"
499532
);
500-
501-
break;
502-
case "null":
503-
args.push("null");
504533
break;
505-
case "node":
506534
case "regexp":
535+
break;
507536
case "date":
508-
case "weakmap":
509-
case "weakset":
510-
case "iterator":
537+
break;
511538
case "generator":
512-
case "error":
513-
case "proxy":
539+
args.push(ro.preview.properties[0].value || "");
540+
break;
514541
case "promise":
542+
if (ro.preview.properties[0].value === "pending") {
543+
args.push(`{<${ro.preview.properties[0].value}>}`);
544+
} else {
545+
args.push(
546+
`{<${ro.preview.properties[0].value}>: ${ro.preview.properties[1].value}}`
547+
);
548+
}
549+
break;
550+
case "node":
551+
case "iterator":
552+
case "proxy":
515553
case "typedarray":
516554
case "arraybuffer":
517555
case "dataview":
518556
case "webassemblymemory":
519557
case "wasmvalue":
520558
break;
559+
case "error":
521560
default:
522561
// just a pojo
523562
args.push(
@@ -539,5 +578,13 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
539578
}
540579
}
541580

542-
console[evt.type](...args);
581+
const method = mapConsoleAPIMessageTypeToConsoleMethod[evt.type];
582+
583+
if (method in console) {
584+
// eslint-disable-next-line prefer-spread
585+
console[method].apply(console, args);
586+
} else {
587+
console.warn(`Unsupported console method: ${method}`);
588+
console.log("console event:", evt);
589+
}
543590
}

0 commit comments

Comments
 (0)