-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 aliased imports support #14952
Comments
FWIW also opened an issue for this in Svelte/kit because it sounds like it might need a solution upstream, unless there's something about webpack aliases and storybook's babel runtime I'm missing sveltejs/kit#1485 |
@j3rem1e any chance you can take a quick look? |
vite/snowpack uses 'import.meta', which is not supported by webpack 4. Sadly, webpack5 supports 'import.meta' but not 'import.meta.env' . You should probably mock '$app/env' (ie, aliasing $app/env to a file you own), or try to use the vite-builder. svelte-kit is more and more dependent of vite. |
@j3rem1e can we figure out how to get it running in |
ah that makes sense then. Mocking all the runtime imports of sveltekit ( |
@madeleineostoja Ooh those aliases sound like a good workaround! I think we can get the vite builder working, but I suspect it will be awhile before that is officially supported, and in the meantime it would be good to have a solution for webpack. Any chance you can help add that? |
By the sounds of it webpack 5 won’t support the full |
FYI seems the problem with the vite builder is |
I can confirm that with You can add a manual alias to Sveltekit's runtime modules in module.exports = {
core: { builder: 'storybook-builder-vite' },
async viteFinal(config) {
config.resolve.alias = {
$app: path.resolve('./.svelte-kit/dev/runtime/app'),
};
return config;
}
}; I think this is worth documenting somewhere, probably in the docs for |
@jonniebigodes @j3rem1e can you two please figure out the best way to document this? it's weird because SvelteKit depends on vite community builder, which is not part of core. But we're going to have to figure this out and I don't want to lose more Svelte users. |
Also just in case you wanted to document the other option for the runtime aliases, this would be the path based on path.resolve('./.svelte-kit/build/runtime/app') If you used ^ you'd probably want to make your storybook run script svelte-kit build && start-storybook Might be a better default for documentation, doesn't have the gotcha of having to have had run |
Not sure if this was ever documented. However, just got this working and thought I'd share the slight tweaks that have probably come up in last few months:
For everything else, following along with what @madeleineostoja 's did here works well. Nothing spectacular but it did take some time to work out the right set of tweaks! Oh well. |
Hmmm, that seems odd. Would you be willing to open an issue in https://github.com/eirslett/storybook-builder-vite with the details of what you are seeing, @michaelwooley? |
A way to work around the issue is to mock the functions yourself. If the component uses I created a
Then we can link the $app alias to the .storybook app folder by adding this to the webpack4 config:
Full webpack config:
At least this solution will allow you to run a storybook and create the components with webpack 4. |
https://github.com/michaelwooley/storybook-experimental-vite demonstrates setting up Storybook with the new |
Do folks have use cases for the other aliases besides |
I think that's a fairly broad and opinionated assumption. Taking your own example of a button that needs in-app navigatation, unless you're distributing that component as part of a consumable design system, making app navigation a seperate action on every instance of that button sounds a lot more like a workaround for these kind of issues than a good design pattern. It's like saying you should always pass react router's In any case Tbh I've seen this kind of rationale a bunch of times from the Svelte team in different discussions and it irks me — "what you're all doing that svelte can't support right now is bad practice, do it our way". Idk just feels off, and very different from just flagging that something doesn't work for x technical reason and has y as a workaround/alternate pattern |
The // .storybook/main.js
module.exports = {
//...
async viteFinal(config, { configType }) {
config.resolve.alias = {
$app: path.resolve("./.svelte-kit/runtime/app"),
};
return config;
},
}; But with this we get the following error, that the module can't be found:
|
This should be supported in Storybook 7.0 using the sveltekit framework ( |
I'd have guessed it's blocked by #20239? |
Most aliases are now supported out-of-the-box with the latest Storybook 7. You can see a summary of what is supported and not supported here: https://github.com/storybookjs/storybook/tree/next/code/frameworks/sveltekit There are a few that are not yet supported and make more sense to support as mocks. I've created a new issue to track that: #20999. PRs for it would be very welcome! |
Describe the bug
The new
@storybook/svelte
setup breaks when you try and integrate with Sveltekit, because it relies on using aliased imports generated at runtime like$app/env
.I tried adding these aliases to webpack directly, eg:
But then I get parsing errors, I assume because it's an ES module and storybook needs to transpile it
System
Additional context
I imagine using the upcoming pluggable builders with Vite (with ES build support, which sveltekit uses under the hood) might fix this issue, but I couldn't get the alpha
storybook-vite-builder
working either.The text was updated successfully, but these errors were encountered: