You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guides/production-build.md
+36-42Lines changed: 36 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,79 +4,73 @@ sort: 13
4
4
contributors:
5
5
- henriquea
6
6
- rajagopal4890
7
+
- markerikson
8
+
- simon04
7
9
---
8
10
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.
10
12
11
-
- Source Maps
12
-
- Node environment
13
-
- Minification
13
+
## The automatic way
14
14
15
-
## Source Maps
15
+
Running `webpack -p` (or equivalently `webpack --optimize-minimize --define process.env.NODE_ENV="production"`). This performs the following steps:
16
16
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
26
20
27
-
The `DefinePlugin` creates **compile** time constants. Useful for injecting your Node.js environment as seen below.
21
+
### Minification
28
22
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:
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.
50
48
51
-
##Minification
49
+
### Node environment variable
52
50
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):
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
Copy file name to clipboardExpand all lines: content/plugins/define-plugin.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,8 @@ console.log("Running App version " + VERSION);
25
25
if(!BROWSER_SUPPORTS_HTML5) require("html5shiv");
26
26
```
27
27
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
+
28
30
Each key passed into `DefinePlugin` is an identifier or multiple identifiers joined with `.`.
29
31
30
32
* If the value is a string it will be used as a code fragment.
0 commit comments