Webpack IgnorePlugin配置后为什么没排除模块?
我在用Webpack优化项目时,想通过IgnorePlugin排除某个不常用的依赖模块。按照文档配置了以下代码,但打包后的bundle里还是能看到这个模块,到底是哪里出问题了?
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.IgnorePlugin({
resource: /lodash-es/,
context: /my-component/
})
]
};
具体场景是第三方组件my-component引入了完整的lodash-es,但实际只用到2个方法。我尝试过把正则改为^lodash-es、调整context路径,甚至直接写lodash-es/isFunction,但都不管用。控制台也没有报错提示,求大神指条明路…
resource和context都得精确匹配模块路径。改成这样:另外,lodash-es这种ESM模块最好用babel-plugin-import按需加载,别折腾IgnorePlugin了。搞定。