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

🟧 Sveltekit: Can't import Sveltekit modules in components (i.e. $path) #384

Closed
6 tasks done
oneezy opened this issue Dec 2, 2022 · 9 comments
Closed
6 tasks done
Labels
to triage This issue needs to be triaged

Comments

@oneezy
Copy link

oneezy commented Dec 2, 2022

Describe the bug

When trying to import Sveltekit Modules such as the $path store in a component,

❌ I get an error in Histoire (port 6006)
βœ… I don't get an error in Sveltekit (port 5173).

Stackblitz Repro:
https://stackblitz.com/edit/sveltejs-kit-template-default-9wclu1?file=src/lib/PageTitle/PageTitle.svelte

 
Example: a component that displays the <PageTitle />
Output: "histoire-dev/histoire/issues/384"
 

<!-- PageTitle.svelte -->
<script>
  import { page } from '$app/stores'
</script>

<h1>{$page.url.pathname}</h1>
sveltekit-histoire-bug.webm

Error

image

> oneezy@oneezy MINGW64 ~/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro (main)
$ pnpm story

> histoire-sveltekit@0.0.1 story C:\Users\oneezy\Desktop\www\10.Repros\indaco-histoire-sveltekit-repro
> svelte-kit sync && histoire dev

  ➜  Local:   http://localhost:6006/
  ➜  Network: use --host to expose
Collect stories start all
Failed to resolve import "$app/stores" from "src\lib\components\URLpath\URLpath.svelte". Does the file exist?
Error while collecting story C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/src/stories/URLpath/URLpath.story.svelte:
Error: Failed to resolve import "$app/stores" from "src\lib\components\URLpath\URLpath.svelte". Does the file exist?
    at formatError (file:///C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.pnpm/vite@3.1.4/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:40854:46)
    at TransformContext.error (file:///C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.pnpm/vite@3.1.4/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:40850:19)
    at normalizeUrl (file:///C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.pnpm/vite@3.1.4/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:37587:33)
    at async TransformContext.transform (file:///C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.pnpm/vite@3.1.4/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:37720:47)
    at async Object.transform (file:///C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.pnpm/vite@3.1.4/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:41103:30)
    at async loadAndTransform (file:///C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.pnpm/vite@3.1.4/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:37365:29)
Collect stories end 3952 ms
The request url "C:/Users/oneezy/Desktop/www/2.Starters/monorepo/node_modules/.pnpm/@histoire+app@0.11.7_vite@3.2.4/node_modules/@histoire/app/dist/bundle-main.js" is outside of Vite serving allow list.

- C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.pnpm/@histoire+app@0.11.0_vite@3.1.4/node_modules/@histoire/app/dist
- C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro/node_modules/.histoire
- C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro
- C:/Users/oneezy/Desktop/www/10.Repros/indaco-histoire-sveltekit-repro

Refer to docs https://vitejs.dev/config/server-options.html#server-fs-allow for configurations and more details.

Reproduction

Stackblitz Repro:
https://stackblitz.com/edit/sveltejs-kit-template-default-9wclu1?file=src/lib/PageTitle/PageTitle.svelte

image

System Info

OS: Windows 11 Pro (build - 22000.1219)
Browser: Chrome - Version 107.0.5304.122 (Official Build) (64-bit)
Node: 18.8.0
PNPM: 7.17.1
Terminal: Git Bash

Used Package Manager

pnpm

Validations

@oneezy oneezy added the to triage This issue needs to be triaged label Dec 2, 2022
@stackblitz
Copy link

stackblitz bot commented Dec 2, 2022

@oneezy
Copy link
Author

oneezy commented Dec 2, 2022

@oneezy oneezy changed the title 🟧 Sveltekit: Can't import built-in Sveltekit modules in components (i.e. $path) 🟧 Sveltekit: Can't import Sveltekit modules in components (i.e. $path) Dec 2, 2022
@benzara-tahar
Copy link

Am having the same issue, I am trying to resolve the $app/stores inside the histoire.config.js but I don't know where is the file exporting it
Any idea?

import { defineConfig, defaultColors } from 'histoire';
import { HstSvelte } from '@histoire/plugin-svelte';
import { resolve } from 'path';

export default defineConfig({
  setupFile: './histoire-setup.js',
  theme: {
    title: 'Qomino Design System',
    colors: {
      primary: defaultColors.cyan,
    },
    favicon: './static/favicon.svg',
  },
  vite: {
    resolve: {

      alias: {
         '$lib': resolve('.src/lib'),
         '@components': resolve('./src/lib/components'),
         '@utils': resolve('./src/lib/utils'),
         '@icons': resolve('./src/lib/icons'),
+        '$app/stores': resolve('./node_modules/@sveltejs/kit/types'), // <-- how to resove this?
      },

    }
  },

  plugins: [HstSvelte()],
});

@benzara-tahar
Copy link

One workaround is to mock the modules you need , for my cases i only used $page.url so I mocked that
image
image

@oneezy
Copy link
Author

oneezy commented Dec 10, 2022

@benzara-tahar Thanks for posting a possible workaround!

I was sifting through the docs earlier and saw this so figured I'd post it here

https://kit.svelte.dev/docs/packaging

Screenshot_20221210-000122

@benmccann
Copy link
Contributor

I hope this will be fixed if we leave SvelteKit's setup plugin in place and remove it's compile plugin. See my comment here: #393 (comment)

@oneezy
Copy link
Author

oneezy commented Dec 13, 2022

Thanks for giving this one attention @benmccann !

Sveltekit + Histoire are a match made in heaven :)

I'll follow that thread and help test where I can

@Akryum Akryum closed this as completed Dec 19, 2022
@oneezy
Copy link
Author

oneezy commented Dec 21, 2022

@Akryum , this bug still exists

@benmccann
Copy link
Contributor

Some SvelteKit modules will work and some won't. You can see a summary here: https://github.com/storybookjs/storybook/tree/next/code/frameworks/sveltekit#supported-features

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to triage This issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

4 participants