IgnorePlugin

IgnorePlugin 防止在 importrequire 调用时,生成以下正则表达式匹配的模块:

Using regular expressions

  • resourceRegExp:匹配(test)资源请求路径的正则表达式。
  • contextRegExp:(可选)匹配(test)资源上下文(目录)的正则表达式。
  1. new webpack.IgnorePlugin({resourceRegExp, contextRegExp});
  2. // old way, deprecated in webpack v5
  3. new webpack.IgnorePlugin(resourceRegExp, [contextRegExp]);

Using filter functions

  • checkContext(context) A Filter function that receives context as the argument, must return boolean.
  • checkResource(resource) A Filter function that receives resource as the argument, must return boolean.
  1. new webpack.IgnorePlugin({
  2. checkContext (context) {
  3. // do something with context
  4. return true|false;
  5. },
  6. checkResource (resource) {
  7. // do something with resource
  8. return true|false;
  9. }
  10. });

忽略 moment 本地化内容的示例

moment 2.18 会将所有本地化内容和核心功能一起打包(见该 GitHub issue)。

The resourceRegExp parameter passed to IgnorePlugin is not tested against the resolved file names or absolute module names being imported or required, but rather against the string passed to require or import within the source code where the import is taking place. For example, if you’re trying to exclude node_modules/moment/locale/*.js, this won’t work:

  1. -new webpack.IgnorePlugin(/moment\/locale\//);

Rather, because moment imports with this code:

  1. require('./locale/' + name);

…your first regexp must match that './locale/' string. The second contextRegExp parameter is then used to select specific directories from where the import took place. The following will cause those locale files to be ignored:

  1. new webpack.IgnorePlugin({
  2. resourceRegExp: /^\.\/locale$/,
  3. contextRegExp: /moment$/
  4. });

…which means “any require statement matching './locale' from any directories ending with 'moment' will be ignored.


贡献人员

DullReferenceException DullReferenceException EugeneHlushko EugeneHlushko byzyk byzyk simon04 simon04