Webpack 2 – eval can cause bug with process.env

Do you want to request a feature or report a bug?

What is the current behavior?
webpack transform process.env to a real object like

  "NODE_ENV": "development"

but when a module which use process.env in this way

process.env.DEBUG = false

it will cause a syntax error: unexpected token : , ie: visionmedia/debug

function save(namespaces) {
  if (null == namespaces) {
    // If you set a process.env field to null or undefined, it gets cast to the
    // string 'null' or 'undefined'. Just delete instead.
    delete process.env.DEBUG;
  } else {
    process.env.DEBUG = namespaces;

because webpack use eval to load this module, and the code above will be transformed to

eval("{\"NODE_ENV\": \"development\"}.DEBUG = namespace")

but in this situation, {\"NODE_ENV\": \"development\"} will be a block, but not a Object, the correct way is

eval("({\"NODE_ENV\": \"development\"}).DEBUG = namespace")

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

What is the expected behavior?

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

  • webpack: 2.1.0-beta.28
  • Node.js: v7.2.1
  • OS: macOS Sierra 10.12.2 (16C67)
  • Chrome Canary: 57.0.2972.0

Author: Fantashit

1 thought on “Webpack 2 – eval can cause bug with process.env

Comments are closed.