hughsk / envify

:wrench: Selectively replace Node-style environment variables with plain strings.
902 stars 57 forks source link

Doesn't support object destructuring or accessing process.env object #58

Open timwis opened 7 years ago

timwis commented 7 years ago

It took me about 2 hours of debugging to release this, but when you think about how envify works, it makes sense. I was using object destructuring to cut down on some boilerplate, which works fine in node:

const {
  VAR_A,
  VAR_B,
  VAR_C
} = process.env

And none of them were being set. So I added a console.log(process.env) beneath it, which output {}, and that made me think envify wasn't being executed at all. Of course it's because envify is parsing the file looking for process.env.SOMETHINGHERE literally.

It would be nice if object destructuring were supported, though I can understand why it would be challenging, and accessing process.env would almost always be a mistake (except for debugging). Perhaps the solution here is just to make this more clear in the readme, that this module doesn't actually provide a process.env object; it basically just does string replacing?

yoshuawuyts commented 7 years ago

it basically just does string replacing

Yep, it's literally a find-and-replace. Such is the nature of most transforms haha. Agree that if it isn't clear we should update the docs. PR welcome!

3den commented 5 years ago

@yoshuawuyts currently the find and replace works if you pass a single var const {VAR_A} = process.env but not if you pass many, I understand that this is because you use a find and replace for /\bprocess\.env\b/ but there is a way that we can make so it support both.

Babel transpiler converts this:

const {A1, B2} = process.env;

To:

var _process$env = process.env,
   A1 = _process$env.A1,
   B2 = _process$env.B2

So if we make the regex a bit more generic we can make it match both cases, e.g. /\b_?process[\.$]env\b/.

I can send a PR for that if you are ok with this.