You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I wanted to get env variables to SPA(like API_URL) and found a discussion on Discord
looking that quite some time has passed since this ticket was created, I wonder how this PROPOSAL would fit Gleam's language design:
populate env variables
create a new template in lustre_dev/priv/template for env variables
in build.gleam inside do_app create a new populated file from cli.template("env.mjs")
inside env.mjs, we would need to add an Object with all populated env values and some code(could be extended with get_or)
Inside this file we could automatically add LUSTRE_PUBLIC_* and additionally, populate flags from --env: gleam run -m lustre/dev build --minify --outdir=../server/priv/static --env=ADDITIONAL_ENV_VAR=true,ANOTHER=example
add lustre_env() to entry-with-main.mjs inside main(..): context({ env })
lustre/context, simplified
pub type LustreEnv {
LustreEnv(get: fn(String) -> Result(String, Nil))
}
pub type LustreContext { // in case you want to add something more than `env`
LustreContext(env: LustreEnv)
}
usage
import lustre/context.{LustreContext, LustreEnv}
pub fn main(context: Option(LustreContext)) {
let assert Some(context) = context
let env = context.env
let api_url_result = env.get("LUSTRE_PUBLIC_API_URL")
...
let flags =
dict.new()
|> dict.insert("api_url", result.unwrap(api_url_result, "http://api:3030")
let app = lustre.application(init, update, view)
let assert Ok(_) = lustre.start(app, "#app", flags)
with this design, we won't use the generation of gleam files
ANOTHER option I was thinking about, which is worse, is to use esbuild --define=[...] that will replace env variables inline in the generated code
in Gleam it could also be called with something like lustre_env("LUSTRE_PUBLIC_API") that will be replaced in JS with the value
Feature request for including environment variables in lustre bundles. Useful for changing behaviors or values between development and production etc.
The text was updated successfully, but these errors were encountered: