|
| 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