IgnorePlugin用於忽略某些特定的模塊,讓 webpack 不把這些指定的模塊打包進去
測試例子:
src/index.js
import moment from 'moment' moment.locale('zh-cn') let date = moment().startOf('hour').fromNow(); // 7 分鍾前 console.log(date)
打包

包的體積差不多為 1000kib
這個時候我們測試使用 IgnorePlugin 來忽略 moment 的語言包
const path = require('path')
let webpack = require('webpack')
let htmlWebpckPlugin = require('html-webpack-plugin')
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new htmlWebpckPlugin({
template: './public/index.html'
}),
new webpack.IgnorePlugin(/^\.\/locale/, /moment$/)
],
module: {
noParse: /jquery|lodash/, // 正則表達式
rules: [
{
test: /\.js?$/,
include: path.join(__dirname, 'src'),
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
"presets": [
"@babel/preset-env",
"@babel/preset-react",
]
}
}
}
]
},
}
再次打包測試

體積明顯減少 效果明顯
測試代碼 : webpack-dev-3 文件夾下
https://gitee.com/guangzhou110/ten-days_to_master_webpack4/tree/master/webpack-dev-3
