-
Notifications
You must be signed in to change notification settings - Fork 150
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
v4.9 broke accessing stores with $ prefix when imported in module context #401
Comments
Could you provide a minimum reproducible / code snippet ? |
I'm using SvelteKit v1.0.0-next.164: <script lang="ts">
import { session, signOut } from "$lib/session"
</script>
{#if $session}
<button on:click|once={signOut}>Sair</button>
{:else}
<a href="/entrar">Entrar</a>
{/if}
import { writable } from "svelte/store"
export function createLocalStorage(key: string) {
const { subscribe, set } = writable<string | null>(
localStorage.getItem(key),
(set) => {
function handleStorageEvent(event: StorageEvent) {
if (event.key === key) {
set(event.newValue)
}
}
window.addEventListener("storage", handleStorageEvent)
return () => window.removeEventListener("storage", handleStorageEvent)
}
)
return {
subscribe,
set: (value: string | null) => {
set(value)
if (value) {
localStorage.setItem(key, value)
} else {
localStorage.removeItem(key)
}
},
get: () => localStorage.getItem(key)
}
}
export const session = createLocalStorage("session") |
Given your code snippet and using the latest version of SvelteKit I'm unable to reproduce this (I added |
Ah I think I found the actual issue. You need to import the store in a I'm sorry for this sloppy bug report, I was in a hurry and assumed any code snippet containing the store would do. |
Still can't reproduce it. I addded |
I came across the same issue. This is how I import and use the store in my page component in sapper <script lang="ts" context="module">
import store from "./store"
</script>
<script lang="ts">
// using $store here causes the error "ReferenceError: store is not defined"
console.log($store)
</script>
|
❌ When I add <script lang="ts" context="module">
import { session, signOut } from "$lib/session"
</script>
{#if $session}
<button on:click|once={signOut}>Sair</button>
{:else}
<a href="/entrar">Entrar</a>
{/if} ✔️ If I use the store in the component script everything works: <script lang="ts" context="module">
import { session, signOut } from "$lib/session"
</script>
<script lang="ts">
console.log($session)
</script>
{#if $session}
<button on:click|once={signOut}>Sair</button>
{:else}
<a href="/entrar">Entrar</a>
{/if} ❌ But then if I don't use the store in the template, I get an error: <script lang="ts" context="module">
import { session } from "$lib/session"
</script>
<script lang="ts">
console.log($session)
</script>
<div>Some template</div> ✔️ Finally if I move the import to the component script it works again: <script lang="ts">
import { session } from "$lib/session"
console.log($session)
</script>
<div>Some template</div> |
Thanks, can reproduce this now. The error occurs when there's a The IDE error of the first error case is unrelated to this, I'll look into this in the context of language-tools. |
* (fix) handle $store imported in module script Fixes #401 * lint Co-authored-by: Simon Holthausen <[email protected]>
Hey, everywhere in my code where I access store values using the $ prefix now cause an
is not defined
error similar to #396.I'm using
"svelte-preprocess": "^4.9.2"
and"typescript": "^4.4.2"
.I have to set the svelte-preprocess version back to 4.8.0 to fix it.
The text was updated successfully, but these errors were encountered: