IgnorePlugin
IgnorePlugin 阻止生成用于 import 的模块或 require 匹配正则表达式或过滤函数的调用:
Using regular expressions
-
resourceRegExp: 用于测试资源的 RegExp。 -
contextRegExp: (可选)用于测试上下文(目录)的 RegExp。
new webpack.IgnorePlugin({ resourceRegExp, contextRegExp });Using filter functions
checkResource (resource, context) 接收resource和context作为参数的 Filter 函数,必须返回布尔值。
new webpack.IgnorePlugin({
checkResource(resource) {
// do something with resource
return true | false;
},
});Example of ignoring Moment Locales
从 2.18 开始,所有语言环境都与核心库捆绑在一起(请参阅此 GitHub 问题)。
传递给 IgnorePlugin 的 resourceRegExp 参数没有针对已解析的文件名或正在导入或需要的绝对模块名称进行测试,而是针对在导入发生的源代码中传递给 require 或 import 的字符串进行测试。例如,如果您试图排除 node_modules/moment/locale/*.js,这将不起作用:
-new webpack.IgnorePlugin({ resourceRegExp: /moment\/locale\// });相反,因为 moment 使用此代码导入:
require('./locale/' + name);您的第一个正则表达式必须匹配那个 './locale/' 字符串。然后使用第二个 contextRegExp 参数来选择发生导入的特定目录。以下将导致忽略这些语言环境文件:
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
});这意味着“任何以 'moment' 结尾的目录中与 './locale' 匹配的 require 语句都将被忽略。