dominikg / svite

svelte integration for vite
MIT License
373 stars 21 forks source link

Error for libraries that use context API inside a store #72

Closed JustinVoitel closed 4 years ago

JustinVoitel commented 4 years ago

Description

It seems like something is broken when using stores that rely on getContext() in some way. Currently in my project the goto helper of routify and mutate() function of @urql/svelte are getting both the same error: grafik

Reproduction

  1. npm svite create -t routify-mdsvex -ts
  2. add this to src/store.ts:
import { get, writable } from "svelte/store";
import { goto } from "@roxi/routify";

function createTestStore() {
  const store = writable({});

  return {
    subscribe: store.subscribe,
    gotoUrl: (url: string) => {
      const gotoRoutify = get(goto);
      gotoRoutify(url);
    },
  };
}

export const testStore = createTestStore();
  1. add this to src/pages/index.svelte:

    
    <script lang="ts">
    import { testStore } from "./../store";
    
    function handleGoto() {
    testStore.gotoUrl("/bla");
    }
    </script>

Welcome to the routify example

Check out the link in the nav


4. click button in dev

---

1. clone same repo 
2. add dependency `npm i @urql/svelte`
3. add this to `src/App.svelte`:
```svelte
<script lang="ts">
  import { Router } from "@roxi/routify/runtime";
  import { routes } from "../.routify/routes";

  import { initClient } from "@urql/svelte";

  initClient({ url: "/graphql" });
</script>

<Router {routes} />
  1. add this to src/pages/index.ts:

    
    <script lang="ts">
    import { mutate } from "@urql/svelte";
    
    function handleClick() {
    mutate(SOME_QUERY).then((e) => {
      console.log(e);
    });
    }
    </script>

Welcome to the routify example

Check out the link in the nav



## Additional Information

- operating system: windows 10 wsl2
- versions for
  - node v14.8.0
  - npm 6.14.7
  - svite 0.7.1
  - vite 
  - svelte 3.26.0
- logs with debug enabled
  - start svite with `svite --debug` flag
JustinVoitel commented 4 years ago

will try to downgrade svite tomorrow till its working again and comment here

JustinVoitel commented 4 years ago

can confirm when using the latest ts template for routify-mdsvex (svite 0.7.1), downgrading svelte to version 3.24.1 will make my first reproduction example work again (and I guess my second one aswell)

dominikg commented 4 years ago

As discussed on discord, not caused by svite. See here for a brief description https://github.com/sveltejs/svelte/issues/4934

For routify goto, please follow the documented approach: https://routify.dev/docs/helpers#goto See here for an example with the url helper: https://github.com/dominikg/svite/blob/master/examples/routify-mdsvex/src/pages/_folder.svelte