IgnorePlugin 插件
webpackjsorg-main插件参考
IgnorePlugin 插件
IgnorePlugin 会阻止生成与正则表达式或过滤函数匹配的 import 或 require 调用所对应的模块。
提示: 该插件同时适用于 JavaScript 和 CSS,也允许你忽略 CSS 中的特定资源。
使用正则表达式
resourceRegExp:用于测试资源的 RegExp。contextRegExp:(可选)用于测试上下文(目录)的 RegExp。
js
new webpack.IgnorePlugin({ resourceRegExp, contextRegExp });
使用过滤函数
checkResource (resource, context):一个过滤函数,接收resource和context作为参数,必须返回布尔值。
js
new webpack.IgnorePlugin({
checkResource(resource) {
// do something with resource
return true | false;
},
});
忽略 Moment 语言环境的示例
从 moment 2.18 开始,所有语言环境都与核心库捆绑在一起(参见 GitHub issue)。
传递给 IgnorePlugin 的 resourceRegExp 参数不针对被导入或 require 的已解析文件名或绝对模块名进行测试,而是针对源代码中执行导入的位置传入 require 或 import 的 字符串 进行测试。例如,如果你想排除 node_modules/moment/locale/*.js,这样做是行不通的:
diff
-new webpack.IgnorePlugin({ resourceRegExp: /moment\/locale\// });
实际上,由于 moment 使用以下代码进行导入:
js
import(`./locale/${name}`);
// or
require(`./locale/${name}`);
...你的第一个正则表达式必须匹配那个 './locale/' 字符串。然后,第二个 contextRegExp 参数用于选择导入发生的特定目录。以下配置将导致这些语言环境文件被忽略:
js
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
});
...这意味着,来自任何以 'moment' 结尾的目录、且匹配 './locale' 的 require 语句都将被忽略。
帮助我们改进文档
发现翻译问题或内容错误?请告诉我们。
