Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Initialize process.env on client side #569

Closed
thani-sh opened this issue Oct 25, 2016 · 2 comments
Closed

Initialize process.env on client side #569

thani-sh opened this issue Oct 25, 2016 · 2 comments

Comments

@thani-sh
Copy link
Contributor

The problem with #565 is that a copy of the complete env object will be placed everywhere process.env gets used. JSON is also a valid way to represent javascript objects so it should work fine. But in most cases this will add lots of redundant code.

Example:

// env = { X: 100, Y: 200, Z: 300 }
console.log(process.env)
console.log(process.env.X)
console.log(process.env.Y)

will be converted to

// env = { X: 100, Y: 200, Z: 300 }
console.log({ "X": 100, "Y": 200, "Z": 300 })
console.log({ "X": 100, "Y": 200, "Z": 300 }.X)
console.log({ "X": 100, "Y": 200, "Z": 300 }.Y)

Solution 1:

As a solution, we can initialize the process.env object on the client side:

  • Use the define plugin to set STORYBOOK_ENV_VARS
STORYBOOK_ENV_VARS: JSON.stringify(JSON.stringify(env))
  • Create the process.env object on the client side
window.process = window.process || {}
window.process.env = JSON.parse(STORYBOOK_ENV_VARS)

Solution 2:

If we can control the order the define plugin works (add it multiple times perhaps?), we can replace env values (ex: process.env.ABC) first and then replace the env object (process.env). If we do this, we don't have to add additional init code to the client.

@arunoda
Copy link
Member

arunoda commented Oct 25, 2016

I think Solution2 is kind a not possible, so I think we should go with solution 1.
BTW: We should add an issue on CRA on this as well.

@arunoda
Copy link
Member

arunoda commented Oct 25, 2016

This is how they do it. It's pretty similar.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants