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

v4.9 broke accessing stores with $ prefix when imported in module context #401

Closed
ghost opened this issue Sep 7, 2021 · 8 comments · Fixed by #404
Closed

v4.9 broke accessing stores with $ prefix when imported in module context #401

ghost opened this issue Sep 7, 2021 · 8 comments · Fixed by #404

Comments

@ghost
Copy link

ghost commented Sep 7, 2021

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.

@dummdidumm
Copy link
Member

Could you provide a minimum reproducible / code snippet ?

@ghost
Copy link
Author

ghost commented Sep 8, 2021

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}

session is a custom store actually:

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")

@ghost ghost changed the title v4.9 broke accessing stores with $ prefix v4.9 broke accessing custom stores with $ prefix Sep 8, 2021
@dummdidumm
Copy link
Member

Given your code snippet and using the latest version of SvelteKit I'm unable to reproduce this (I added signOut to your code snippet because it was missing).

@ghost
Copy link
Author

ghost commented Sep 9, 2021

Ah I think I found the actual issue. You need to import the store in a context="module" script.
Try changing <script lang="ts"> to <script lang="ts" context="module">.

I'm sorry for this sloppy bug report, I was in a hurry and assumed any code snippet containing the store would do.

@ghost ghost changed the title v4.9 broke accessing custom stores with $ prefix v4.9 broke accessing stores with $ prefix when imported in module context Sep 9, 2021
@dummdidumm
Copy link
Member

Still can't reproduce it. I addded context="module" and the code compiles and works.

@janaz
Copy link

janaz commented Sep 9, 2021

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>

@ghost
Copy link
Author

ghost commented Sep 9, 2021

❌ When I add context="module" I get an error:

<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}

20210909060513

✔️ 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>

20210909060917

✔️ 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>

dummdidumm pushed a commit to dummdidumm/svelte-preprocess that referenced this issue Sep 9, 2021
@dummdidumm
Copy link
Member

dummdidumm commented Sep 9, 2021

Thanks, can reproduce this now. The error occurs when there's a $store subscription where store is imported in the module script.

The IDE error of the first error case is unrelated to this, I'll look into this in the context of language-tools.

kaisermann pushed a commit that referenced this issue Sep 9, 2021
* (fix) handle $store imported in module script

Fixes #401

* lint

Co-authored-by: Simon Holthausen <simon.holthausen@accso.de>
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 a pull request may close this issue.

2 participants