HOW do you read NODE_ENV while bundling?

I am trying to bundle my frontend code with spago

NODE_ENV=development spago bundle-app

and I would like to get variable from NODE_ENV to find out if it is in production or in develpoment, how would I read this variable ?

How do you do it ?

Thank you in advance.

PureScript tooling will not bundle env variables in anyway. If you are writing a node app, and you have access to a “real” env, then you can read it with node-process. Otherwise, I’d probably recommend having separate spago.dhall files for production and dev, and including different sources. There’s also probably a way to include ad-hoc file paths as a command line argument for the build.

While you can read different spago.dhall file which you can pass by --config. you end up with 2 different dependencies list, which I don’t like. I tried to take it out from spago.dhall file but that didn’t work.

However I end-up with this solution.
in my sources I have:

, sources =
  [ "pages/assets/js/pure/**/*.purs"
  , "${if (((env:PRODUCTION : Bool) ? False )) then ".env.purs" else ".env.dev.purs"}"
  , "test/**/*.purs"
  ]

This way if I don’t pass environment variable, I call Config module from .env.dev.purs.
and if I pass PRODUCTION=True (note the uppercase), then I get .env.purs Config module. :))
My .env.purs and .env.dev.purs have the same name of the module and I import it as Config.

and Futher I simplified it by

  , "${(env:ENVPATH as Text) ? ".env.dev.purs"}"
2 Likes

Anything you want to share you could put in a different file and import, but I think the env conditional you are using is better. I didn’t know you could do that with Dhall!

1 Like

I tried that, but spago didn’t like that dependencies were file and not String. :confused: Yea, I learned it today as well.

1 Like