Skip to content

Commit 049895a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 5d3b13e + aa357c9 commit 049895a

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

content/configuration/target.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ Tells webpack which environment the application is targeting. The following valu
2020
| `target` | Description |
2121
| ------------- |------------------------|
2222
| `async-node`| Compile for usage in a Node.js-like environment (uses `fs` and `vm` to load chunks asynchronously) |
23-
| `electron` | Compile for [Electron](http://electron.atom.io/) renderer process, provide a target using `JsonpTemplatePlugin`, `FunctionModulePlugin` for browser environments and `NodeTargetPlugin` and `ExternalsPlugin` for CommonJS and Electron built-in modules. |
24-
| `electron-renderer` | Compile for [Electron](http://electron.atom.io/) for `renderer-process` |
23+
| `electron` | Alias for `electron-main` |
24+
| `electron-main` | Compile for [Electron](http://electron.atom.io/) for main process. |
25+
| `electron-renderer` | Compile for [Electron](http://electron.atom.io/) for renderer process, providing a target using `JsonpTemplatePlugin`, `FunctionModulePlugin` for browser environments and `NodeTargetPlugin` and `ExternalsPlugin` for CommonJS and Electron built-in modules. |
2526
| `node` | Compile for usage in a Node.js-like environment (uses Node.js `require` to load chunks) |
2627
|`node-webkit`| Compile for usage in WebKit and uses JSONP for chunk loading. Allows importing of built-in Node.js modules and [`nw.gui`](http://docs.nwjs.io/en/latest/) (experimental) |
2728
|`web`| Compile for usage in a browser-like environment **(default)** |

content/guides/production-build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = {
6767
};
6868
```
6969

70-
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`.
70+
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 `"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`.
7171

7272
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. See how to specify the [environment in webpack configuration](/configuration/environment).
7373

content/plugins/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Plugins
33
contributors:
44
- simon04
5+
- gonzoyumo
56
---
67

78
webpack has a rich plugin interface. Most of the features within webpack itself use this plugin interface. This makes webpack **flexible**.
@@ -14,6 +15,7 @@ webpack has a rich plugin interface. Most of the features within webpack itself
1415
|[`CompressionWebpackPlugin`](/plugins/compression-webpack-plugin)|Prepare compressed versions of assets to serve them with Content-Encoding|
1516
|[`I18nWebpackPlugin`](/plugins/i18n-webpack-plugin)|Adds i18n support to your bundles|
1617
|[`HtmlWebpackPlugin`](/plugins/html-webpack-plugin)| Simplifies creation of HTML files (`index.html`) to serve your bundles|
18+
|[`NormalModuleReplacementPlugin`](/plugins/normal-module-replacement-plugin)|Replaces resource that matches a regexp|
1719

1820

1921
![Awesome](../assets/awesome-badge.svg)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: NormalModuleReplacementPlugin
3+
contributors:
4+
- gonzoyumo
5+
---
6+
## Install
7+
8+
The `NormalModuleReplacementPlugin` is a built-in webpack plugin.
9+
10+
11+
## Usage
12+
13+
``` javascript
14+
new webpack.NormalModuleReplacementPlugin(resourceRegExp, newResource)
15+
```
16+
17+
The `NormalModuleReplacementPlugin` allows you to replace resources that match `resourceRegExp` with `newResource`. If `newResource` is relative, it is resolved relative to the previous resource. If `newResource` is a function, it is expected to overwrite the request attribute of the supplied resource.
18+
19+
This can be useful for allowing different behaviour between builds.
20+
21+
## Basic example
22+
23+
Replace a specific module when building for development environment ([read more](/configuration/environment)).
24+
25+
26+
Say you have a config file `some/path/config.development.module.js` and a special version for production in `some/path/config.production.module.js`
27+
28+
Just add the following plugin when building for production:
29+
30+
``` javascript
31+
new webpack.NormalModuleReplacementPlugin(
32+
/some\/path\/config\.development\.js/,
33+
'./config.production.js'
34+
);
35+
```
36+
37+
## Advanced example
38+
39+
Conditional build depending on an environment var ([read more](/configuration/environment)).
40+
41+
Say you want a configuration with specific values for different build targets.
42+
43+
``` javascript
44+
module.exports = function(env) {
45+
var appTarget = env.APP_TARGET || 'VERSION_A';
46+
return {
47+
plugins: [
48+
new webpack.NormalModuleReplacementPlugin(/(.*)-APP_TARGET(\.*)/, function(resource) {
49+
resource.request = resource.request.replace(/-APP_TARGET/, `-${appTarget}`);
50+
})
51+
]
52+
}
53+
54+
}
55+
```
56+
57+
Create the two config files:
58+
59+
**app/config-VERSION_A.js:**
60+
``` javascript
61+
export default {
62+
title : 'I am version A'
63+
}
64+
```
65+
**app/config-VERSION_B.js:**
66+
``` javascript
67+
export default {
68+
title : 'I am version B'
69+
}
70+
```
71+
Then import that config using the keyword you're looking for in the regexp:
72+
73+
``` javascript
74+
import config from 'app/config-APP_TARGET';
75+
console.log(config.title);
76+
```
77+
78+
And now you just get the right config imported depending on which target you're building for:
79+
80+
``` shell
81+
webpack --env.APP_TARGET VERSION_A
82+
=> 'I am version A'
83+
84+
webpack --env.APP_TARGET VERSION_B
85+
=> 'I am version B'
86+
87+
```

0 commit comments

Comments
 (0)