Skip to content

feat: custom devtools formatters #13070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed

feat: custom devtools formatters #13070

wants to merge 6 commits into from

Conversation

Rich-Harris
Copy link
Member

A few people have been tripped up by the extremely useless way in which Proxy objects are logged to the console — rather than logging what the proxy itself reports about its shape, they print the original target object (albeit behind several clicks), along with (for reasons I cannot fathom) the handler. Since Svelte doesn't mutate the original object (for good reason) this makes the logs unhelpful.

The proper fix is to snapshot the object (console.log($state.snapshot(object))) or, better yet, use $inspect. But for people as-yet-unfamiliar with those tools, it would be nice if console.log gave a better experience.

I tried monkey-patching console methods so that we could at least print a warning ('use $state.snapshot, it's better') but that means you no longer get a clickable link to the place where the log happened, which sucks.

It turns out, via @trueadm, that custom formatters are a thing. This PR does two things — if you haven't enabled custom formatters, it prints a warning suggesting that you do so...

image

...and if you have, it prints this when you log state:

image

Annoyingly, there's no straightforward way to generate a preview (i.e. $state ▸ Object { count: 1 }), you always have to expand it to see the properties.

More annoyingly, this only really works in Chrome. Firefox 'supports' custom formatters, but it's so broken (it seems to just log a non-expandable Object { }, ignoring what the formatter returned and hiding the object contents) that once again I find myself wondering why they bother at all. Safari doesn't support it at all AFAICT.

Is this worth it? I can't tell.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Aug 29, 2024

⚠️ No Changeset found

Latest commit: e936ff3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

'div',
{},
['span', { style: 'font-weight: bold' }, '$state'],
['object', { object: snapshot(object), config }]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is why it's not working in FF. The "object" type isn't supported it seems, so we'd have to manually do it with own own styled divs/spans?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Surprising that I learned about "object" from firefox-source-docs.mozilla.org of all places. I guess this is why Chrome has 174% market share and Firefox has -42%.

We could construct our own representation but it might be a challenge to make it look consistent with normal object logging across browsers and light/dark modes. We might have to settle for unstyled text if we attempt this.

@trueadm
Copy link
Contributor

trueadm commented Aug 29, 2024

There's another downside to this – the console.log doesn't actually produce the object until you click the arrow. So it's a bit pointless if you want to know the value of something at a given time? Surely there's a way around this though.

@dummdidumm
Copy link
Member

dummdidumm commented Aug 29, 2024

I think that's fine somewhat - it certainly is in line with how it works for console logging POJOs as well. If you need a snapshot, well, use $state.snapshot

@trueadm
Copy link
Contributor

trueadm commented Aug 29, 2024

I think that's fine somewhat - it certainly is in line with how it works for console logging POJOs as well. If you need a snapshot, well, use $state.snapshot

Ah it appears to be fine actually. I was messing with some other setting and forgot to remove it.

@Rich-Harris
Copy link
Member Author

Yeah, I called that out upthread ('you always have to expand it to see the properties') but if we were to generate string representations that worked across browsers then maybe it would be okay. It's not the same as POJOs — they get a preview before you expand them

@dummdidumm
Copy link
Member

"String representation" as in like JSON.stringify? Because that could amount to walls of text, where I would prefer the "not expanded by default" nature of POJOs. Maybe there's a sensible default here (under 100 characters, just raw text, over that, do more elaborate stuff). Or "string representation" as in "construct our own div soup which browsers will render fine"?

@trueadm
Copy link
Contributor

trueadm commented Aug 29, 2024

We can look at how the Chrome dev tools handles it and copy that, as I assume they use the same API :P

@Rich-Harris
Copy link
Member Author

"String representation" as in like JSON.stringify?

More like how devtools print the first n properties (in Chrome it's 5, in Firefox it's 10) followed by an ellipsis, just without the styling

We can look at how the Chrome dev tools handles it and copy that

Do you know where the code lives?

Even if we could, it's subject to change at Chrome's whim and it would look out of place in Firefox — plain text might end up being simpler

@dummdidumm
Copy link
Member

I think it's somewhere here - from a quick looks it's not that straightforward. So it would different in Firefox anyway, which I think would be fine. I certainly don't want my potentially giant objects always be printed in full.

@trueadm
Copy link
Contributor

trueadm commented Aug 29, 2024

Yeah, let's do plain text

@dummdidumm
Copy link
Member

dummdidumm commented Aug 29, 2024

I meant this as in "I don't want plain text in the sense of JSON.stringify(...)", because I don't want to clutter my console. I want this...

{ foo: ... }
{ bar: ... }

...not this...

{ foo: { a: ..., b: ..., c: ..., --thousands of entries more -- }
{ bar: { a: ..., b: ..., c: ..., --thousands of entries more -- }

... but I would be in favor of concatenating a poor man's preview ourselves, if that helps firefox.

dummdidumm added a commit that referenced this pull request Sep 5, 2024
Wrap console.log/warn/error statements in DEV mode with a check whether or not they contain state objects. Closes #13123

This is an alternative or enhancement to #13070. Alternative if we deem it the better solution. Enhancement because it's not as robust as a custom formatter: We only check the top level of each entry (though we could maybe traverse a few levels), and if you're logging class instances, snapshot currently stops at the boundaries there and so you don't get snapshotted values for these (arguably this is a more general problem of $inspect and $state.snapshot), whereas with custom formatter it doesn't matter at which level you come across it.
@dummdidumm
Copy link
Member

Opened #13142 as an alternative/enhancement to this

@Rich-Harris
Copy link
Member Author

I'm going to close this, because #13142 is a far more consistent and reliable experience that will nudge the community towards better patterns. Having to click the collapsed object to see the snapshotted value sucks, and the alternative (attempting to format it ourselves) is just a recipe for sadness and regret

Rich-Harris added a commit that referenced this pull request Sep 16, 2024
…ts (#13142)

* feat: provide guidance in browser console when logging `$state` objects

Wrap console.log/warn/error statements in DEV mode with a check whether or not they contain state objects. Closes #13123

This is an alternative or enhancement to #13070. Alternative if we deem it the better solution. Enhancement because it's not as robust as a custom formatter: We only check the top level of each entry (though we could maybe traverse a few levels), and if you're logging class instances, snapshot currently stops at the boundaries there and so you don't get snapshotted values for these (arguably this is a more general problem of $inspect and $state.snapshot), whereas with custom formatter it doesn't matter at which level you come across it.

* lint

* use normal warning mechanism, so we can link to docs etc

* add a few more methods

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants