DefinePlugin fails to inject process.env.NODE_ENV into React without UglifyJSPlugin #868
Comments
when you take a closer look to the react file you'll notice that it looks like this: ...
if ("production" !== process.env.NODE_ENV) {
...
("production" !== process.env.NODE_ENV ? warning(
standardName == null,
'Unknown DOM property ' + name + '. Did you mean ' + standardName + '?'
) : null);
...
}
... webpack already replaces the top-most ...
if (false) {
...
("production" !== process.env.NODE_ENV ? warning(
standardName == null,
'Unknown DOM property ' + name + '. Did you mean ' + standardName + '?'
) : null);
...
}
... |
You're right, I should have inspected the output more carefully. Thanks for the quick response and sorry for the bother! |
Am I understanding correctly that Webpack is so clever that it detects the dead code and doesn't perform a no-op injection? |
The DefinePlugin just replaces occurrences of the given identifiers with the given expressions. After that, UglifyJS detects dead code blocks and removes them. |
@skozin Webpack does all of its AST walking and transforming together on an AST, and it does so while performing static analysis, so if it recognizes that it can't get into one side of an if, it won't run any transforms in that part. |
@loganfsmyth, thanks! That's a great feature :) |
package.json:
main.js:
webpack.config.js:
Place all these files into the same directory and then run:
npm install rm -rf _dist && ./node_modules/.bin/webpack
Then inspect
_dist/out.js
. You will see thatprocess.env.NODE_ENV
has been successfully injeted tomain.js
module (line47
):But all entries of
process.env.NODE_ENV
inside React modules remain intact, e.g. line314
:You can verify this by searching for
("production")
andprocess.env.NODE_ENV
. Changing definition to a flat one doesn't help:However, when I add
UglifyJSPlugin
, injection starts working as intended: UglifyJS prints a lot ofCondition always false
messages, and there are no occurrences ofNODE_ENV
inside the minifiedout.js
.The text was updated successfully, but these errors were encountered: