-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
lifecycle-methods (onMount/onDestroy) are failing when called inside an installed package in dev #2147
Comments
Seeing this as a blocker in the 'sswr' pkg |
#2137 could have fixed this. please update sveltekit to next.146 and try again |
@dominikg Sadly same error occurs in v146 :/ |
sswr does not set the "svelte" field in its package.json https://github.com/ConsoleTVs/sswr/blob/master/package.json which is probably fine as it doesn't export svelte components. |
@dominikg Oh, yeah! Just
@ConsoleTVs could you add the svelte-field to package.json for testing? If that's enough, it should be in the documentation and we are done here :) Edit: Added a "workaround"-branch to the repro: https://github.com/benbender/sveltekit-lifecycle-repro-ts/tree/workaround |
@benbender @dominikg Why is there the need for me to add a non standard key into the package.json? Would an empry object be enough or what am I expected to put into thst svelte field? |
@ConsoleTVs If I got this correctly, you'll have to publish the src with the package and have the svelte-field point to it. So it would be Basically the svelte-field is used by rollup-plugin-svelte and (if properly configured) svelte-loader to locate the source file, so that your third party components get compiled at the same time as the rest of your app (and import from the same internal library). See https://github.com/sveltejs/rollup-plugin-svelte#pkgsvelte |
I'm not exactly sure if setting the "svelte" field is the right thing to do for a library that does not export svelte components. |
@dominikg I'm thinking you are on the right track, but as Based on this statement and on my repro, I think it is needed to ship the source inside the package and it will, basically, automate my initial workaround of "copying" the source of the package into the actual app in dev-mode. This works as intended as proven by my repro and I think it would be the solution to this (even if it isn't, obviously, the most elegant solution in the world...) |
thats the thing, there are no components in sswr. It is a utility library with a peer dependency on svelte. In case you want to dive into the rabbit hole |
So not only I have to add that key but also the source code to the npm files???? I am starting to feel a bit bissed at this point... |
@ConsoleTVs no you don't, see my comment above. You could add it to enable the automatic workaround, or document somewhere that it needs to be added to optimizeDeps.exclude manually. At some point in the future this problem will be fixed, either in vite itself or in vite-plugin-svelte |
@dominikg Thanks for the explanation. You are probably right 👍 |
@dominikg @benbender Thanks for the hard work here, I will add this note to the docs and wait for official fixes! Great work to both of you! |
I think this fix it (for now): ConsoleTVs/sswr#11. |
@frederikhors not really as this issue isn't about It could be closed instead as a bug in vite or rollup-plugin-svelte. As I'm not sure how to handle this further and if this needs to be tracked here, I would like to delegate the decision to @dominikg, who is much deeper into the topic :) |
Note: Once sveltejs/vite-plugin-svelte#137 is released for |
vite-plugin-svelte 1.0.0-next.16 has been released: https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/CHANGELOG.md |
@dominikg I'm happy to report that this issue is fixed with the release. Awesome work! Thank you! |
Describe the bug
If I try to use
onMount()
/onDestroy()
inside an installed 3rd-party-module, it errors in dev-mode but works in prod/preview withError: Function called outside component initialization
.The exact same code works flawlessly if copied into a local module (see repro @ https://github.com/benbender/sveltekit-lifecycle-repro-ts/blob/master/src/routes/index.svelte#L2).
Noticed the problem at first while tinkering with a port of
react-query/core
to svelte, noticed it again today while looking intosswr
. So it's not a problem of a particular package.Update:
In next.146 the bug can be fixed by adding the problematic package to
vite.optimizeDeps.exclude
. Example in https://github.com/benbender/sveltekit-lifecycle-repro-ts/tree/workaroundReproduction
Logs
System Info
Severity
serious, as I think at least any slightly advanced data-fetching-library for svelte will need to hook into the component lifecycle. At the moment those libraries are practically unusable for endusers as they won't be able to develop with them. There are for sure more usecases where this will be a problem, but this is mine ;)
Additional Information
svelte
. See repl: https://svelte.dev/repl/9ac4442a292449aea8997584c0335de1?version=3sveltekit
with a ts- and js-setup. So typescrypt isn't the culprit.The text was updated successfully, but these errors were encountered: