IgnorePlugin

IgnorePlugin 阻止生成与正则表达式或过滤函数匹配的 importrequire 调用的模块。

使用正则表达式

  • resourceRegExp: 用于测试资源的正则表达式。
  • contextRegExp: (可选) 用于测试上下文(目录)的正则表达式。
new webpack.IgnorePlugin({ resourceRegExp, contextRegExp });

使用过滤函数

  • checkResource (resource, context) 一个过滤函数,接收 resourcecontext 作为参数,必须返回布尔值。
new webpack.IgnorePlugin({
  checkResource(resource) {
    // do something with resource
    return true | false;
  },
});

忽略 Moment 语言环境的例子

moment 2.18 版本起,所有语言环境都与核心库捆绑在一起(参见 此 GitHub issue)。

传递给 IgnorePluginresourceRegExp 参数不是根据被导入或请求的解析后的文件名或绝对模块名进行测试的,而是根据 导入发生源文件中 传递给 requireimport字符串 进行测试的。例如,如果你尝试排除 node_modules/moment/locale/*.js,这将不起作用

-new webpack.IgnorePlugin({ resourceRegExp: /moment\/locale\// });

相反,因为 moment 使用此代码导入:

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

...你的第一个正则表达式必须匹配 './locale/' 字符串。第二个 contextRegExp 参数用于从导入发生的特定目录中进行选择。以下配置将导致这些语言环境文件被忽略:

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

...这意味着“任何匹配 './locale' 且来自以 'moment' 结尾的目录的 require 语句都将被忽略”。

8 贡献者

simon04byzykDullReferenceExceptionEugeneHlushkoFadySamirSadekiamakulovchenxsansnitin315