웹팩 공식 샵 을 방문하여 의류를 구매하시고 웹팩을 후원해주세요!   모든 후원의 진행사항은 웹팩의 오픈 챌린지 페이지 에서 확인하실 수 있습니다.

ag-grid is proud to partner with webpack

NormalModuleReplacementPlugin

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.

This can be useful for allowing different behaviour between builds.

new webpack.NormalModuleReplacementPlugin(
  resourceRegExp,
  newResource
)

Basic Example

Replace a specific module when building for a development environment.

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

Just add the following plugin when building for production:

new webpack.NormalModuleReplacementPlugin(
  /some\/path\/config\.development\.js/,
  './config.production.js'
);

Advanced Example

Conditional build depending on an specified environment.

Say you want a configuration with specific values for different build targets.

module.exports = function(env) {
  var appTarget = env.APP_TARGET || 'VERSION_A';
  return {
    plugins: [
      new webpack.NormalModuleReplacementPlugin(/(.*)-APP_TARGET(\.*)/, function(resource) {
        resource.request = resource.request.replace(/-APP_TARGET/, `-${appTarget}`);
      })
    ]
  }

}

Create the two config files:

app/config-VERSION_A.js

export default {
  title : 'I am version A'
}

app/config-VERSION_B.js

export default {
  title : 'I am version B'
}

Then import that config using the keyword you're looking for in the regexp:

import config from 'app/config-APP_TARGET';
console.log(config.title);

And now you just get the right config imported depending on which target you're building for:

webpack --env.APP_TARGET VERSION_A
=> 'I am version A'

webpack --env.APP_TARGET VERSION_B
=> 'I am version B'

Contributors