Closed acarl005 closed 2 years ago
Ah, I think you may be hosed here.
Dynamic vars are populated by the adapter calling server.init
, meaning the adapter has to at least start up in order for you to see them. vite-node
runs your plugins, but not your adapter.
It sounds like $env/dynamic/public
would also not be compatible with Vitest, Storybook, etc. at the moment. If we can't define these another way, we might have to document that. We should probably have a section in the docs about running components in isolation for testing, etc.
I wonder if there's a simple way to allow testing frameworks to populate it, similar to how server.init
does?
I haven't used vite-node
but from reading the README I think it's a dev time thing? Could we just populate env
in the virtual module during dev, and only require you to await server.init({ env })
with a built app?
But we're already populating it in dev
...
We're initialising it in dev...
...but I'm suggesting that we actually populate this module instead of re-exporting the env
object that gets set during that initialisation:
It feels there might be some timing issues that could be resolved with this approach? Not sure
Interesting. I'm going to self-assign this and see what I can do...
It sounds like
$env/dynamic/public
would also not be compatible with Vitest
@benmccann I can confirm that the same thing is happening with vitest
, which I am also using. vitest
is probably the more commonly used tool compared to vite-node
.
For me, I find that $env/dynamic/public
in dev mode, get's populated in the client but not in the server. However, in a production build it works fine.
Edit: I tried $env/dynamic/public
on StackBlitz and was wondering why it was working there, but not when I ran a new create-svelte app... and I've confirmed that it works on Linux and not Windows.
and I've confirmed that it works on Linux and not Windows.
I am also facing the same issue on windows. env
returning an empty array.
env
shouldn't be returning an array at all. Did you mean an object
? If not, there's likely some strange other issue involved.
Sorry, yes it is an empty object.
Describe the bug
Context
This is a followup to #5886. I began using
vite-node
in order to make my SvelteKit code (which depends on Vite's transforms and plugins) importable to backend/database/sysadmin-related scripts. In particular, I need access to the$env
store from my shared code. Running my backend scripts withvite-node
correctly populates$env/static/private
for those scripts 🙌Problem
HOWEVER, confusingly
$env/dynamic/private
is not getting populated throughvite-node
. It ends up being an empty object at runtime even though my.env
file is set up correctly.$env/dynamic/private
is getting properly populated when I run my sveltekit app throughvite dev
though.Reproduction
See my stackblitz. To prove that my env setup is correct, I am serving both static and dynamic env vars from my page endpoint, and rendering on the page (not a "realistic" example, I only want to show that my .env is hooked up correctly). Now, to show the actual bug, run
npm run bug
. This goes throughvite-node
. The static env is printing BUT NOT the dynamic env.I'm not sure if this is a sveltekit or vite-node issue. The vite config knows about the sveltekit plugin, so it should be able to load sveltekit's stores.
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-9six26?file=src%2Froutes%2F%2Bpage.svelte,src%2Froutes%2F%2Bpage.server.js,.env,package.json,bug.js&terminal=dev
Logs
No response
System Info
Severity
annoyance
Additional Information
No response