Skip to content
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

First class support for Svelte 5's $inspect() method #361

Open
kaleabmelkie opened this issue Dec 9, 2024 · 4 comments
Open

First class support for Svelte 5's $inspect() method #361

kaleabmelkie opened this issue Dec 9, 2024 · 4 comments

Comments

@kaleabmelkie
Copy link

kaleabmelkie commented Dec 9, 2024

In Svelte 5, we can't do $: console.log(...) anymore (invalid syntax). This used to work with Console Ninja before Svelte 5. It has changed to the $inspect rune now.

This means that console.log now needs to be chained with $inspect(...).with. For example:

<script>
  let count = $state(0)

  $inspect(count).with((_type, count) => {
    console.log(count);
  });
</script>

<button type="button" onclick={() => count++}>
  {count}
</button>

Ideally, would like to be able to use $inspect(...) without having to nest console.log inside the with function.

Docs: https://svelte.dev/docs/svelte/$inspect

@smcenlly smcenlly transferred this issue from wallabyjs/public Dec 9, 2024
@smcenlly
Copy link
Member

smcenlly commented Dec 9, 2024

You mentioned that you can't use console.log anymore. Can you please provide us with an example of where it's not working for you anymore? I guess we're missing something?

We created a sample repo and used console.log and it's working for us:

  1. npx sv create my-svelte-app
  2. Open my-svelte-app in VS Code
  3. Start with npm run dev
  4. Add console.log to the Counter.svelte modulo function
image

Our svelte depdendencies are:

{
  "devDependencies": {
    "@sveltejs/adapter-auto": "^3.0.0",
    "@sveltejs/kit": "^2.9.0",
    "@sveltejs/vite-plugin-svelte": "^5.0.0",
    "svelte": "^5.0.0",
    "svelte-check": "^4.0.0"
  }
}

@kaleabmelkie
Copy link
Author

kaleabmelkie commented Dec 9, 2024

I meant for the logs that update when a state changes (not inside functions).

Example (Counter.svelte):

<script>
  let count = $state(0)

  // this gets logged on terminal initially and when `count` changes
  $inspect(count) // [want CN log here]

  // this was possible in svelte 4 & below (not anymore)
  $: console.log(count) // [CN output was here, but this errors in svelte 5]
</script>

<button type="button" onclick={() => count++}>
  {count}
</button>
image

In Svelte 5, the $inspect "rune" is the preferred way to log values.

This function is defined here in Svelte source code: https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/internal/client/dev/inspect.js

@smcenlly
Copy link
Member

Thanks for the sample.

For now, you may work around the limitation by using the .with function on $inspect. This will work for you:

<script>
  let count = $state(0)

  $inspect(count).with((_type, count) => {
    console.log(count);
  });
</script>

<button type="button" onclick={() => count++}>
  {count}
</button>

We’ll keep this issue open as a feature request. To help us prioritize it, please like the original issue comment. This helps us gauge interest and understand demand for the feature.

@smcenlly smcenlly changed the title Support Svelte v5+ (using $inspect(...)) First class support for Svelte 5's $inspect() method Dec 10, 2024
@kaleabmelkie
Copy link
Author

We’ll keep this issue open as a feature request. To help us prioritize it, please like the original issue comment. This helps us gauge interest and understand demand for the feature.

Seems like about a few more people want this too 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants