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

Read keys from ENV will increase bundle size #14453

Closed
behnammodi opened this issue Oct 10, 2021 · 12 comments
Closed

Read keys from ENV will increase bundle size #14453

behnammodi opened this issue Oct 10, 2021 · 12 comments

Comments

@behnammodi
Copy link

behnammodi commented Oct 10, 2021

Bug report

If we destructure more than one key from process.env, all keys will append to bundle file.

# .env file
REACT_APP_NAME=BEHNAM
REACT_APP_SURNAME=MOHAMMADI
// app
const { REACT_APP_NAME, REACT_APP_SURNAME } = process.env;

What is the current behavior?

// bundle file
var e=Object({NODE_ENV:"production",PUBLIC_URL:"",WDS_SOCKET_HOST:void 0,WDS_SOCKET_PATH:void 0,WDS_SOCKET_PORT:void 0,FAST_REFRESH:!0,REACT_APP_NAME:"BEHNAM",REACT_APP_SURNAME:"MOHAMMADI"}),t=e.REACT_APP_NAME,n=e.REACT_APP_SURNAME;

If the current behavior is a bug, please provide the steps to reproduce.

Just destructure more than 1 key from process.env

const { REACT_APP_NAME, REACT_APP_SURNAME } = process.env;

What is the expected behavior?

// bundle file
var t='BEHNAM',n='MOHAMMADI';

Other relevant information:
webpack version: 4.41.2
Node.js version: 14.16.1
Operating System: macOS
Additional tools: Create React App

@alexander-akait
Copy link
Member

Avoid using process.env and destructuring, it is limitation, please use process.env.REACT_APP_NAME and process.env.REACT_APP_SURNAME directly, even more process.env is not safe to use, you can have sensitive data there, not all things can be tree shaking and you can leak your local env.

@behnammodi
Copy link
Author

@alexander-akait Thanks, why does this code work?

const { REACT_APP_NAME } = process.env;
const { REACT_APP_SURNAME } = process.env;

@alexander-akait
Copy link
Member

@vankop I think we have this issue, I can't find...

@vankop
Copy link
Member

vankop commented Oct 11, 2021

Yes, we had one. I have added this issue to webpack 5 project board https://github.com/webpack/webpack/projects/5#card-43856731

@behnammodi
Copy link
Author

@alexander-akait @vankop Thanks a lot, so have you any plan to handle destructure?

@alexander-akait
Copy link
Member

It is not easy, but maybe yes

@behnammodi
Copy link
Author

It is not easy, but maybe yes

And also I think this issue is for babel not webpack, am I right?

@vankop
Copy link
Member

vankop commented Oct 11, 2021

No, this is webpack issue.

@vankop
Copy link
Member

vankop commented Nov 23, 2021

closing in favor of #14800

@vankop vankop closed this as completed Nov 23, 2021
@BradPrit09
Copy link

Avoid using process.env and destructuring, it is limitation, please use process.env.REACT_APP_NAME and process.env.REACT_APP_SURNAME directly, even more process.env is not safe to use, you can have sensitive data there, not all things can be tree shaking and you can leak your local env.

Hi Alex, Thank you for your effort. But I have doubt regarding the documentation of the web-pack 5. Specially EnvironmentPlugin and DefinePlugin. There it's mentioned we can use process.env clearly.
Additionally, I can access process.env in my configs, whereas in the migration it's written that webpack5 removed process pollyfills. Can you please elaborate on this ?

**VERSION USING**
"webpack": "^5.73.0",

@alexander-akait
Copy link
Member

@BradPrit09 in documentation we use directly process.env.ENV_NAME (maybe we should improve docs to avoid misleading here, so feel free to do it).

You can use process.env inside webpack.config.js safely

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

No branches or pull requests

5 participants