Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow object destructure #142

Closed
piranna opened this issue Sep 7, 2018 · 2 comments
Closed

Allow object destructure #142

piranna opened this issue Sep 7, 2018 · 2 comments

Comments

@piranna
Copy link

piranna commented Sep 7, 2018

const formatData = Object.keys(vars).reduce((obj, key) => {
obj[`process.env.${key}`] = JSON.stringify(vars[key])
return obj
}, {})

dotenv-webpack is replacing the references to process.env at source code level, just doing a text replace. I use to do object destructure of process.env like const {foo} = process.env, so this is not changed and I end with process.env strings in the browser code.

As a quick solution it would be just enough to add a new replace for process.env to the content of JSON.stringify(vars). I know this probably will increase a lot the browser source code and also expose the secrets, so maybe this would need to be under an unsafe flag, but better than that would be instead to do a replacement of the exported constants at AST level, although this is more dificult than a simple text replacement.

@mrsteele
Copy link
Owner

This had been reviewed extensively here: #70 (comment)

The risk of security is far too important to move forward with this approach.

If you do not personally mind for your personal project, feel free to just load in all your env variables.

// webpack.config.js

// have not tested, but probably would look something like this:
const { parsed } = require('dotenv').config()

module.exports = {
  plugins: [
    new webpack.DefinePlugin({
      'process.env': parsed
    })
  ]
}

Since we have already covered this extensively in the other thread, I'm going to close this, but feel free to reopen if this is not the case.

@piranna
Copy link
Author

piranna commented Sep 10, 2018

Yes, I know this first approach has some serious security issues, as I pointed out the best one would be to work at AST level so it would be possible to do the replacements of the variables. Anyway, thanks for pointing out the example code, for some use cases it would be useful :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants