Closed elliott-with-the-longest-name-on-github closed 1 year ago
cc #99 and #107 -- this would allow both of these use cases to be zero-config.
This sounds like the way to go
Not sure what would happen to import.meta.env
when compiling for cjs tho
When running console.log(import.meta?.env?.VITE_KV_REST_API_URL);
inside the package, right before reading process.env.KV_REST_API_URL
, it logs undefined
(note that it's calling the index.js
file and not the .cjs
). The same log works inside a +page.server.ts
file.
The only workaround I found is to define the env variables inside the vite.config.ts
file.
export default ({ mode }) => {
process.env.VITE_KV_REST_API_URL = "..."
return defineConfig({
plugins: [sveltekit()],
});
};
I'm silly, we can't do this. The reason Vite only reads VITE_
variables into import.meta.env
is so that they don't accidentally get bundled into client code. This would inevitably expose variables at some point. Instead, I added docs to our packages:
https://github.com/vercel/storage/pull/117
@correttojs Not sure if there's a good place on front
to put some sort of setup instructions like this, but basically one of those two solutions is what people need to do. (The first one is pretty close to what you're doing here.)
In all of our packages, we support no-config on platforms that use
process.env
. Vite does not -- it usesimport.meta.env
, with a catch. To avoid exposing system env vars to your build process, Vite requires its env vars to be prefixed withVITE_
.This means, for example, that the following code in Postgres:
could support Vite by doing:
We could also potentially enhance the error by detecting if the user is running Vite by doing something like:
One note of caution: The ideal experience here would also require some level of integration with build & deploy + our examples in the storage dashboard. Ideally, connecting a store to a project that's running on Vite would automatically prefix its env vars with
VITE_
.