Runtime Env vars in vite

Hey all, I'm trying to figure out a way to run environment variables at runtime rather than at compile time. I've been looking at https://iendeavor.github.io/import-meta-env/ , but I can't seem to get it to work. Specifically, in transformMode: "runtime" , trying to use this code in a script called by index.html (in fact, a script tag in index.html) just makes global.import_meta_env to be the placeholder, it doesn't get replaced.
<script>
globalThis.import_meta_env = JSON.parse('"import_meta_env_placeholder"');
console.log(import_meta_env); // import_meta_env_placeholder
</script>
<script>
globalThis.import_meta_env = JSON.parse('"import_meta_env_placeholder"');
console.log(import_meta_env); // import_meta_env_placeholder
</script>
I need this because our build system builds once and then distributes to other servers for various regions. We also have staging/beta/dev servers built the same way. Anything I'm missing? (This isn't necessarily related to solidjs, but, it's a thing that could be used with solidjs 😉 )
Import-meta-env | Startup/Runtime environment variables solution fo...
Build once, deploy anywhere. Import-meta-env helps to developing applications following the 12-factor principles.
5 Replies
belst
belst•2y ago
just use nodes process.env?
<Alterion.Dev>
<Alterion.Dev>OP•2y ago
at runtime after build? thonk don't think you understood the question. dammit. Turns out, I was missing the last step - you need to inject the vars, and that requires a script... or a dockerfile that calls an sh script that does the replacement. which we already had. smh.
mdynnl
mdynnl•2y ago
https://stackblitz.com/edit/solidjs-templates-qxvqnt haha, yeah TIL about the plugin, thanks, pretty useful
<Alterion.Dev>
<Alterion.Dev>OP•2y ago
yep! that's the one 😄 I'm an absolute donkey, lol 😄
mdynnl
mdynnl•2y ago
the docs could definitely be improved, it isn't that clear what you have to do exactly
Want results from more Discord servers?
Add your server