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

Handling process.env.SOME_UNDEFINED_KEY as undefined with DefinePlugin #12444

Closed
whatasoda opened this issue Jan 19, 2021 · 2 comments
Closed

Comments

@whatasoda
Copy link

Bug report

What is the current behavior?
If I configure DefinePlugin with 'process.env': {...} format, some env variables which is not specified to the plugin is replaced with { /* full content of process.env I specified */ }.SOME_UNDEFINED_KEY, though the evaluated value is obviously undefined.
It'll affect bundle file size if I forget specifying some env variables, or if dependencies of my project use their custom env variables like SC_ATTR of styled-components.
I know 'process.env.SOME_ENV_VAR_NAME': ... format is recommended. But I sometimes use destructuring assignment with process.env and I don't want to give up doing that because of its convenience.

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

Here is the repo to reproduce the issue.
https://github.com/whatasoda/webpack-define-plugin-report

webpack.config.js

const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const path = require('path');

module.exports = {
  mode: 'production',
  entry: path.resolve(__dirname, 'index.js'),
  output: {
    path: path.resolve(__dirname, 'dist'),
  },
  optimization: {
    minimize: true,
    minimizer: [new TerserPlugin({})],
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        BROWSER: JSON.stringify(true),
        UNRELATED_ENV_VAR: JSON.stringify('hello'),
      },
    }),
  ],
};

index.js

if (process.env.BROWSER) {
  console.log('Hello, browser!');
}

if (process.env.SERVER) {
  console.log('Hello, server!');
}

Then the output file is like:

console.log("Hello, browser!"),{BROWSER:!0,UNRELATED_ENV_VAR:"hello"}.SERVER&&console.log("Hello, server!");

What is the expected behavior?

I'm happy if the output file becomes the same as when I specify undefined to SERVER, like:

console.log("Hello, browser!");

I saw a similar is reported at #10425, but it seems not to be fixed yet.
Thank you in advance.

Other relevant information:
webpack version: 5.15.0
Node.js version: 10.15.3
Operating System: macOS Catalina Version 10.15.7
Additional tools:
terser-webpack-plugin: 5.1.1

@alexander-akait
Copy link
Member

To be honestly I think it is not bug, change logic will break other cases... We have note about this:
https://webpack.js.org/plugins/define-plugin/

When defining values for process prefer 'process.env.NODE_ENV': JSON.stringify('production') over process: { env: { NODE_ENV: JSON.stringify('production') } }. Using the latter will overwrite the process object which can break compatibility with some modules that expect other values on the process object to be defined.

@whatasoda
Copy link
Author

I know the note. But as I mentioned, I wanted to use destructuring assignment with process.env 😭 I hope there will be some update on #5392. Perhaps it'll help me.

change logic will break other cases

Anyway, this comment is enough for me to close this issue. Thank you for your support!

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

3 participants