Skip to content

Commit ffdde0b

Browse files
committed
production-build: document webpack -p, clarify
1 parent 17abd03 commit ffdde0b

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

content/guides/production-build.md

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,73 @@ sort: 13
44
contributors:
55
- henriquea
66
- rajagopal4890
7+
- markerikson
8+
- simon04
79
---
810

9-
Generating production builds with webpack is straight-forward. There are three things to keep in mind:
11+
This page explains how to generate production builds with webpack.
1012

11-
- Source Maps
12-
- Node environment
13-
- Minification
13+
## The automatic way
1414

15-
## Source Maps
15+
Running `webpack -p` (or equivalently `webpack --optimize-minimize --define process.env.NODE_ENV="production"`). This performs the following steps:
1616

17-
We encourage you to have Source Maps enabled in production. They are useful for debugging and to run benchmark tests. Webpack can generate inline Source Maps included in the bundles or separated files.
18-
19-
In your configuration, use the `devtools` object to set the Source Map type. We currently support seven types of Source Maps. You can find more information about them in our [configuration](/configuration/devtool) documentation page.
20-
21-
One of the good options to go is using `cheap-module-source-map` which simplifies the Source Maps to a single mapping per line.
22-
23-
## Node environment variable
24-
25-
The second step is to tell webpack to generate a production build by setting the Node.js environment variable to `production`. webpack will not include any extra useful code, warnings and checks used in development.
17+
- Minification using `UglifyJsPlugin`
18+
- Runs the `LoaderOptionsPlugin`, see its [documentation](/plugins/loader-options-plugin)
19+
- Sets the Node environment variable
2620

27-
The `DefinePlugin` creates **compile** time constants. Useful for injecting your Node.js environment as seen below.
21+
### Minification
2822

29-
?> TODO: Add a link to the `ProvidePlugin` documentation
23+
webpack comes with `UglifyJsPlugin`, which runs [UglifyJS](http://lisperator.net/uglifyjs/) in order to minimize the output. The plugin supports all of [UglifyJS options](https://github.com/mishoo/UglifyJS2#usage). Specifying `--optimize-minimize` on the command line, the following plugin configuration is added:
3024

3125
```js
3226
// webpack.config.js
33-
3427
const webpack = require('webpack');
3528

3629
module.exports = {
3730
/*...*/
38-
plugins: [
39-
new webpack.DefinePlugin({
40-
'process.env': {
41-
'NODE_ENV': JSON.stringify('production')
42-
}
31+
plugins:[
32+
new webpack.optimize.UglifyJsPlugin({
33+
sourceMap: options.devtool && (options.devtool.indexOf("sourcemap") >= 0 || options.devtool.indexOf("source-map") >= 0)
4334
})
4435
]
45-
/*...*/
4636
};
4737
```
4838

49-
T> Spoiler: Setting the env var only won't make your bundle smaller. This take us to the last step:
39+
Thus, depending on the [devtool options](/configuration/devtool), Source Maps are generated.
40+
41+
### Source Maps
42+
43+
We encourage you to have Source Maps enabled in production. They are useful for debugging and to run benchmark tests. Webpack can generate inline Source Maps included in the bundles or separated files.
44+
45+
In your configuration, use the `devtools` object to set the Source Map type. We currently support seven types of Source Maps. You can find more information about them in our [configuration](/configuration/devtool) documentation page.
46+
47+
One of the good options to go is using `cheap-module-source-map` which simplifies the Source Maps to a single mapping per line.
5048

51-
## Minification
49+
### Node environment variable
5250

53-
webpack comes with UglifyJS plugin which minimize the output. You can pass an object containing [UglifyJS options](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin).
51+
Running `webpack -p` (or `--define process.env.NODE_ENV="production"`) defines the `NODE_ENV` environment variable as `"production"`.
52+
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.
54+
55+
On one side, this allows you to conditionally modify the build configuration, for instance change the [output filename](/configuration/output/#output-filename):
5456

5557
```js
5658
// webpack.config.js
57-
5859
const webpack = require('webpack');
5960

6061
module.exports = {
6162
/*...*/
62-
plugins:[
63-
new webpack.DefinePlugin({
64-
'process.env':{
65-
'NODE_ENV': JSON.stringify('production')
66-
}
67-
}),
68-
new webpack.optimize.UglifyJsPlugin({
69-
compress:{
70-
warnings: true
71-
}
72-
})
73-
]
74-
/*...*/
63+
output: {
64+
filename: process.env.NODE_ENV === 'production' ? '[name].[hash].bundle.js' : '[name].bundle.js'
65+
}
7566
};
7667
```
77-
## Configuring webpack for multiple environments
7868

79-
When we do have multiple configurations in mind for different environments, the easiest way is to write seperate js files for
69+
Secondly, 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`.
70+
71+
## The manual way: Configuring webpack for multiple environments
72+
73+
When we do have multiple configurations in mind for different environments, the easiest way is to write seperate js files for
8074
each environment. For example:
8175

8276
** dev.js **

content/plugins/define-plugin.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ console.log("Running App version " + VERSION);
2525
if(!BROWSER_SUPPORTS_HTML5) require("html5shiv");
2626
```
2727

28+
T> Note that because the plugin does a direct text replacement, the value given to it must include actual quotes inside of the string itself. Typically, this is done either with alternate quotes, such as `'"production"'`, or by using `JSON.stringify('production')`.
29+
2830
Each key passed into `DefinePlugin` is an identifier or multiple identifiers joined with `.`.
2931

3032
* If the value is a string it will be used as a code fragment.

0 commit comments

Comments
 (0)