Skip to content

Commit c4eae2e

Browse files
committed
Clarify what --define does exactly
1 parent 885bc04 commit c4eae2e

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

content/guides/production-build.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,25 @@ One of the good options to go is using `cheap-module-source-map` which simplifie
4848

4949
### Node environment variable
5050

51-
Running `webpack -p` (or `--define process.env.NODE_ENV="production"`) defines the `NODE_ENV` environment variable as `"production"`.
51+
Running `webpack -p` (or `--define process.env.NODE_ENV="production"`) invokes the [`DefinePlugin`](/plugins/define-plugin) in the following way:
5252

53-
Technically, `NODE_ENV` is a system environment variable that Node exposes into running scripts. It is used by convention to determine development-vs-production behavior, by both server tools, build scripts, and client-side libraries.
53+
```js
54+
// webpack.config.js
55+
const webpack = require('webpack');
56+
57+
module.exports = {
58+
/*...*/
59+
plugins:[
60+
new webpack.DefinePlugin({
61+
'process.env.NODE_ENV': JSON.stringify('production')
62+
})
63+
]
64+
};
65+
```
66+
67+
The `DefinePlugin` performs search-and-replace operations on the original source code. Any occurrence of `process.env.NODE_ENV` in the imported code is replaced by by `"production"`. Thus, checks like `if (process.env.NODE_ENV !== 'production') console.log('...')` are evaluated to `if (false) console.log('...')` and finally minified away using `UglifyJS`.
5468

55-
This invokes the [`DefinePlugin`](/plugins/define-plugin), which perform search-and-replace operations on the original source code. Any occurrence of `process.env.NODE_ENV` in the imported code is replaced by by `"production"`. Thus, checks like `if (process.env.NODE_ENV !== 'production') console.log('...')` are evaluated to `if (false) console.log('...')` and finally minified away using `UglifyJS`.
69+
T> Technically, `NODE_ENV` is a system environment variable that Node.js exposes into running scripts. It is used by convention to determine development-vs-production behavior, by both server tools, build scripts, and client-side libraries. Contrary to expectations, `process.env.NODE_ENV` is not set to `"production"` __within__ the build script `webpack.config.js`, see [#2537](https://github.com/webpack/webpack/issues/2537). Thus, conditionals like `process.env.NODE_ENV === 'production' ? '[name].[hash].bundle.js' : '[name].bundle.js'` do not work as expected.
5670

5771
## The manual way: Configuring webpack for multiple environments
5872

0 commit comments

Comments
 (0)