question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shim SvelteKit runtime import aliases / Importing `$app/*` fails

See original GitHub issue

Describe the bug As far as I can tell there’s no way to use sveltekit runtime imports (eg: $app/navigation) outside of sveltekit dev/build. Which makes testing virtually impossible. If there is a way to shim these imports outside of the main sveltekit context I haven’t found it, so perhaps documentation is needed.

My particular use-case is with Storybook, where UI components that rely on any sveltekit modules break the whole setup. I tried aliasing them with webpack (pointing to .svelte-kit/dev/...) but that didn’t work either.

Another use-case is publishing components for sveltekit that would need to rely on those imports.

To Reproduce

  1. Setup storybook with Sveltekit
  2. Create a component that imports a runtime module (eg: $app/env)
  3. Run storybook and see if fail (cannot resolve module $app/env)

Severity Not blocking, but makes building a component library with Storybook or other development/testing frameworks impossible. So, severe annoyance?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:30 (16 by maintainers)

github_iconTop GitHub Comments

5reactions
wallw-tealcommented, Sep 14, 2021

Here’s the test mock we’ve been using to handle $app/stores. It doesn’t handle things like import.meta.env.

<script>
  import {setContext} from 'svelte';
  import {writable} from 'svelte/store';

  export let Component;

  export let stores = {
    page: writable('/'),
    navigating: writable(null),
    session: writable(null),
  };

  setContext('__svelte__', stores);
</script>

<svelte:component this={Component} {...$$restProps} />

So you just pass it the actual component to be tested and (optionally) the stores you want to use for $app/stores.

EDIT: Added spreading of other props onto the component to be tested.

4reactions
benmccanncommented, Jul 6, 2022

https://github.com/michaelwooley/storybook-experimental-vite demonstrates setting up Storybook with the new vite.config.js. We still need to figure out how to handle $navigation and $stores

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add module import aliases in SvelteKit
resolve with an object of our aliases. Here is the approximate diff you should see when you're done: import preprocess from 'svelte-preprocess'; ...
Read more >
Learn How SvelteKit Works As a Framework - Joy of Code
The vite-plugin-svelte-kit is interesting because it's imported from @sveltejs/kit/vite as sveltekit and it's responsible for taking the SvelteKit config, ...
Read more >
Sveltekit aliased imports support #14952 - Issuehunt
I tried adding these aliases to webpack directly, eg: ... Mocking all the runtime imports of sveltekit ( $app/env is just one) would...
Read more >
Test Svelte Component Using Vitest & Playwright - David Peng
A great read here: Testing a Svelte app with Vitest ... Issues. Shim SvelteKit runtime import aliases / Importing $app/* fails #1485 ......
Read more >
Recommended - SlideShare
Adapters Before you can deploy your SvelteKit app, you need to ... Vite will automatically process imported assets for improved performance.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found