赞助 webpack,同时从官方商店购买衣服 所有收益将转到我们的 open collective

IgnorePlugin

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

Using regular expressions

  • resourceRegExp:匹配(test)资源请求路径的正则表达式。
  • contextRegExp:(可选)匹配(test)资源上下文(目录)的正则表达式。
new webpack.IgnorePlugin({resourceRegExp, contextRegExp});
// old way, deprecated in webpack v5
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.
new webpack.IgnorePlugin({
  checkContext (context) {
    // do something with context
    return true|false;
  },
  checkResource (resource) {
    // do something with resource
    return true|false;
  }
});

忽略 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:

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

Rather, because moment imports with this code:

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:

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

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


贡献人员