-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
get/set/hasContext doesn't work in production code outside a component #429
Comments
This sounds like Vite does somehow create a separate Svelte runtime instance for the library. Your package is using |
If you need more information, or can point me in some direction for testing this in more depth, let me know! |
I thought this was already fixed via #360. A workaround for now is to add this in
|
Tested right away, confirmed that it works with that addition! Can this be fixed (added automatically/programatically?), so I don't have to ask library users to add this to their config, thereby increasing the friction? |
It should be added automatically but it's not, which sounds like a bug. I'll transfer this over to |
When I run your example I don't see the log message you provided. Can you provide an actual reproduction of this issue? |
Strange, I got it immediately using the latest sveltekit, both locally and on stackblitz. Here it is on stackblitz again: https://stackblitz.com/edit/sveltejs-kit-template-default-qwph8i?file=vite.config.js,src%2Froutes%2FHeader.svelte&terminal=dev |
Having the same issue and the only way to resolve it is to add the package in |
Are you using the latest SvelteKit? What happens if you set |
Is this correct? const config = {
kit: {
adapter: adapter(),
},
vitePlugin: {
prebundleSvelteLibraries: false
}
}; It's still causing errors. Something interesting is that my package works inside a monorepo (imported via |
unfortunately prebundleSvelteLibraries is forced true in sveltekit right now as it is passed as an inline option. these have precedence over options specified in the config file. https://github.com/sveltejs/kit/blob/edd815ed7bba7ab88461f9291f924c05a01d1328/packages/kit/src/exports/vite/index.js#L64 The only workaround for that i see is using something like cc @benmccann |
@pilcrowonpaper can you provide a repository with a reproduction?. |
vite-plugin-svelte 1.2.0 improved how prebundleSvelteLibraries works and i cannot reproduce the stacktrace from the original post with that. Unfortunately i'm not sure what is supposed to happen instead of that error so i would appreciate if @ciscoheat could confirm it works now. And @pilcrowonpaper if you still find issues with 1.2.0 and prebundleSvelteLibraries, a reproduction would be greatly appreciated. thanks (: |
@dominikg I just tested it with the latest sveltekit, but still the same "Function called outside component initialization" problem. It works immediately when I add this to ssr: {
noExternal: ['sveltekit-flash-message']
} |
@ciscoheat looking at https://publint.bjornlu.com/[email protected] , there is no peerDependency on svelte. This peerDependency declaration is not needed for ui libraries the only distribute .svelte source code, but it is needed if you make use of svelte apis in the library itself. |
If that still doesn't help, please create an updated reproduction with step-by-step instructions on how to produce the error message (preferably from scratch with the sveltekit skeleton or vite+svelte template, not the sveltekit demo app) |
update, seems you're using sveltekit directly too, so that also needs to be a peerDependency https://github.com/ciscoheat/sveltekit-flash-message/blob/master/src/lib/server.ts#L2 |
Thank you, the peer dependency fixes solved the issue. It's quite intricate, so I appreciate your help! |
@dominikg |
Describe the bug
I'm trying to use the context functions in library code. They work when using both
vite dev
andvite preview
when linking to the library directly. But when publishing to npm and including the library from there, an exception is thrown (see the logs).Reproduction
Explanation
new Flash(page)
callshasContext
in its constructor, throwing the error sincehasContext
cannot findcurrent_component
at that point. Code:https://github.com/ciscoheat/sveltekit-flash-message/blob/c5198a35b724d53e0eab780e16d09c7c5c1499fb/src/lib/client.ts#L61-L66
Stackblitz reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-ask2u7?file=src%2Froutes%2F%2Blayout.svelte&terminal=dev
Logs
System Info
Severity
blocking all usage of SvelteKit
Additional Information
No response
The text was updated successfully, but these errors were encountered: