Plugin
是一个独立的模块。
模块对外暴露一个 js 函数。
函数的原型
(prototype)上定义了一个注入compiler对象的apply方法apply函数中需要有通过compiler对象挂载的webpack事件钩子,钩子的回调中能拿到当前编译的compilation对象,如果是异步编译插件的话可以拿到回调callback完成自定义子编译流程并处理
complition对象的内部数据。如果异步编译插件的话,数据处理完成后执行
callback回调。
1、HotModuleReplacementPlugin
Hot-Module-Replacement 的热更新是依赖于 webpack-dev-server,后者是在打包文件改变时更新打包文件或者 reload 刷新整个页面,HRM 是只更新修改的部分。
HotModuleReplacementPlugin是webpack模块自带的,所以引入webpack后,在plugins配置项中直接使用即可。
const webpack = require('webpack')plugins: [new webpack.HotModuleReplacementPlugin(), // 热更新插件]
2、html-webpack-plugin
entry配置的相关入口 chunk 和 extract-text-webpack-plugin抽取的 css 样式 插入到该插件提供的template或者templateContent配置项指定的内容基础上生成一个 html 文件,具体插入方式是将样式link插入到head元素中,script插入到head或者body中。
const HtmlWebpackPlugin = require('html-webpack-plugin')plugins: [new HtmlWebpackPlugin({filename: 'index.html',template: path.join(__dirname, '/index.html'),minify: {// 压缩HTML文件removeComments: true, // 移除HTML中的注释collapseWhitespace: true, // 删除空白符与换行符minifyCSS: true, // 压缩内联css},inject: true,}),]
true:默认值,
script标签位于html文件的body底部body:
script标签位于html文件的body底部(同 true)head:
script标签位于head标签内false:不插入生成的 js 文件,只是单纯的生成一个
html文件
const path = require('path')const HtmlWebpackPlugin = require('html-webpack-plugin')module.exports = {entry: {index: './src/index.js',login: './src/login.js',},output: {path: path.resolve(__dirname, 'dist'),filename: '[name].[hash:6].js',},//...plugins: [new HtmlWebpackPlugin({template: './public/index.html',filename: 'index.html', //打包后的文件名}),new HtmlWebpackPlugin({template: './public/login.html',filename: 'login.html', //打包后的文件名}),],}
HtmlWebpackPlugin,那么 filename 字段不可缺省,否则默认生成的都是 index.html。
index.html 和 login.html 会发现,都同时引入了 index.f7d21a.js 和 login.f7d21a.js,通常这不是我们想要的,我们希望 index.html 中只引入 index.f7d21a.js,login.html 只引入 login.f7d21a.js。
HtmlWebpackPlugin 提供了一个 chunks 的参数,可以接受一个数组,配置此参数仅会将数组中指定的 js 引入到 html 文件中
module.exports = {//...plugins: [new HtmlWebpackPlugin({template: './public/index.html',filename: 'index.html', //打包后的文件名chunks: ['index'],}),new HtmlWebpackPlugin({template: './public/login.html',filename: 'login.html', //打包后的文件名chunks: ['login'],}),],}
npm run build,可以看到 index.html 中仅引入了 index 的 js 文件,而 login.html 中也仅引入了 login 的 js 文件。
3、clean-webpack-plugin
clean-webpack-plugin 用于在打包前清理上一次项目生成的 bundle 文件,它会根据 output.path 自动清理文件夹;这个插件在生产环境用的频率非常高,因为生产环境经常会通过 hash 生成很多 bundle 文件,如果不进行清理的话每次都会生成新的,导致文件夹非常庞大。
const { CleanWebpackPlugin } = require('clean-webpack-plugin')plugins: [new HtmlWebpackPlugin({template: path.join(__dirname, '/index.html'),}),new CleanWebpackPlugin(), // 所要清理的文件夹名称]
4、extract-text-webpack-plugin
const ExtractTextPlugin = require('extract-text-webpack-plugin')plugins: [// 将css分离到/dist文件夹下的css文件夹中的index.cssnew ExtractTextPlugin('css/index.css'),]
5、mini-css-extract-plugin
sourceMap。只能用在 webpack4 中,对比另一个插件 extract-text-webpack-plugin 有以下特点:
异步加载
不重复编译,性能更好
更容易使用
只针对 CSS
loaders 链中不使用 style-loader, 而且这个插件暂时不支持 HMR。
const MiniCssExtractPlugin = require('mini-css-extract-plugin')module.exports = {module: {rules: [{test: /\.(le|c)ss$/,use: [{loader: MiniCssExtractPlugin.loader,options: {publicPath: '../',},},'css-loader','postcss-loader','less-loader',],},],},plugins: [new MiniCssExtractPlugin({filename: 'css/[name].[contenthash:8].css',chunkFilename: 'css/[id].[contenthash:8].css',}),],}
6、purifycss-webpack
const path = require('path')const PurifyCssWebpack = require('purifycss-webpack') // 引入PurifyCssWebpack插件const glob = require('glob') // 引入glob模块,用于扫描全部html文件中所引用的cssmodule.exports = merge(common, {plugins: [new PurifyCssWebpack({paths: glob.sync(path.join(__dirname, 'src/*.html')),}),],})
7、optimize-css-assets-webpack-plugin
optimize-css-assets-webpack-plugin。
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin") // 压缩css代码optimization: {minimizer: [// 压缩cssnew OptimizeCSSAssetsPlugin({})]}
8、UglifyJsPlugin
uglifyJsPlugin 是 vue-cli 默认使用的压缩代码方式,用来对 js 文件进行压缩,从而减小 js 文件的大小,加速 load 速度。
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')plugins: [new UglifyJsPlugin({uglifyOptions: {compress: {warnings: false}},sourceMap: true, //是否启用文件缓存parallel: true //使用多进程并行运行来提高构建速度})
9、ParallelUglifyPlugin
UglifyJS 去压缩代码,但是变成了并行执行。
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin')plugins: [new ParallelUglifyPlugin({用于配置缓存存放的目录路径。cacheDir: '.cache/',sourceMap: true,uglifyJS: {output: {comments: false,},compress: {warnings: false,},},}),]
10、terser-webpack-plugin
terser-webpack-plugin 这个压缩插件,在此之前是使用 uglifyjs-webpack-plugin,两者的区别是后者对 ES6 的压缩不是很好,同时我们可以开启 parallel 参数,使用多进程压缩,加快压缩。
const TerserPlugin = require('terser-webpack-plugin') // 压缩js代码optimization: {minimizer: [new TerserPlugin({parallel: 4, // 开启几个进程来处理压缩,默认是 os.cpus().length - 1cache: true, // 是否缓存sourceMap: false,}),]}
11、NoErrorsPlugin
NoEmitOnErrorsPlugin 来跳过输出阶段。这样可以确保输出资源不会包含错误。
plugins: [new webpack.NoEmitOnErrorsPlugin()]
12、compression-webpack-plugin
gzip 压缩,启用 gzip 压缩可大幅缩减传输资源大小,从而缩短资源下载时间,减少首次白屏时间,提升用户体验。
const CompressionPlugin = require('compression-webpack-plugin')plugins: [new CompressionPlugin({// gzip压缩配置test: /\.js$|\.html$|\.css/, // 匹配文件名threshold: 10240, // 对超过10kb的数据进行压缩deleteOriginalAssets: false, // 是否删除原文件}),]
13、DefinePlugin
DefinePlugin 可以定义一些全局的变量,我们可以在模块当中直接使用这些变量,无需作任何声明,DefinePlugin 是 webpack 自带的插件。
plugins: [new webpack.DefinePlugin({DESCRIPTION: 'This Is The Test Text.',}),]// 直接引用console.log(DESCRIPTION)
14、ProvidePlugin
identifier 被当作未赋值的变量时, module 就会自动被加载,并且 identifier 会被这个 module 输出的内容所赋值。这是 webpack 自带的插件。
module.exports = {resolve: {alias: {jquery: './lib/jquery',},},plugins: [//提供全局的变量,在模块中使用无需用require引入new webpack.ProvidePlugin({$: 'jquery',React: 'react',}),],}
15、DLLPlugin
bundle(dll-only-bundle)。这个插件会生成一个名为 manifest.json 的文件,这个文件是用来让 DLLReferencePlugin 映射到相关的依赖上去的。
webpack.dll.config.js
const path = require('path')const webpack = require('webpack')module.exports = {entry: {vendor: ['vue-router','vuex','vue/dist/vue.common.js','vue/dist/vue.js','vue-loader/lib/component-normalizer.js','vue','axios','echarts',],},output: {path: path.resolve('./dist'),filename: '[name].dll.js',library: '[name]_library',},plugins: [new webpack.DllPlugin({path: path.resolve('./dist', '[name]-manifest.json'),name: '[name]_library',}),// 建议加上代码压缩插件,否则dll包会比较大。new webpack.optimize.UglifyJsPlugin({compress: {warnings: false,},}),],}
webpack.prod.conf.js 的 plugin 后面加入配置
new webpack.DllReferencePlugin({manifest: require('../dist/vendor-manifest.json'),})
package.json文件中添加快捷命令(build:dll)
"scripts": {"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js","start": "npm run dev","lint": "eslint --ext .js,.vue src","build": "node build/build.js","build:dll": "webpack --config build/webpack.dll.conf.js"}
npm run build:dll命令会在打包目录下生成 vendor-manifest.json 文件与 vendor.dll.js 文件。然后npm run build生产其他文件。
index.html 加入引用
<script type="text/javascript" src="./vendor.dll.js"></script>
16、HappyPack
HappyPack 能让 webpack 把任务分解给多个子进程去并发的执行,子进程处理完后再把结果发送给主进程。要注意的是 HappyPack 对 file-loader、url-loader 支持的不友好,所以不建议对该 loader 使用。
npm i -D happypack
webpack.base.conf.js 文件对 module.rules 进行配置
module: {rules: [{test: /\.js$/,use: ['happypack/loader?id=babel'],include: [resolve('src'), resolve('test')],exclude: path.resolve(__dirname, 'node_modules'),},{test: /\.vue$/,use: ['happypack/loader?id=vue'],},]}
webpack.prod.conf.js 文件进行配置
const HappyPack = require('happypack')// 构造出共享进程池,在进程池中包含5个子进程const HappyPackThreadPool = HappyPack.ThreadPool({ size: 5 })plugins: [new HappyPack({// 用唯一的标识符id,来代表当前的HappyPack是用来处理一类特定的文件id: 'babel',// 如何处理.js文件,用法和Loader配置中一样loaders: ['babel-loader?cacheDirectory'],threadPool: HappyPackThreadPool,}),new HappyPack({id: 'vue', // 用唯一的标识符id,来代表当前的HappyPack是用来处理一类特定的文件loaders: [{loader: 'vue-loader',options: vueLoaderConfig,},],threadPool: HappyPackThreadPool,}),]
17、copy-webpack-plugin
public/index.html 中引入了静态资源,但是打包的时候 webpack 并不会帮我们拷贝到 dist 目录,因此 copy-webpack-plugin 就可以很好地帮我做拷贝的工作了。
const CopyWebpackPlugin = require('copy-webpack-plugin')module.exports = {plugins: [new CopyWebpackPlugin({patterns: [{from: 'public/js/*.js',to: path.resolve(__dirname, 'dist', 'js'),flatten: true,},],}),],}
18、IgnorePlugin
moment 这个第三方依赖库,该库主要是对时间进行格式化,并且支持多个国家语言。虽然我设置了语言为中文,但是在打包的时候,是会将所有语言都打包进去的。这样就导致包很大,打包速度又慢。
IgnorePlugin 使得指定目录被忽略,从而使得打包变快,文件变小。
const Webpack = require('webpack')plugins: [//moment这个库中,如果引用了./locale/目录的内容,就忽略掉,不会打包进去new Webpack.IgnorePlugin(/\.\/locale/, /moment/),]
’./locale/'该字段路径的文件目录,但是也使得我们使用的时候不能显示中文语言了,所以这个时候可以手动引入中文语言的目录。
import moment from 'moment'//手动引入所需要的语言包import 'moment/locale/zh-cn'moment.locale('zh-cn')let r = moment().endOf('day').fromNow()console.log(r)
欢迎加我微信(winty230),拉你进技术群,长期交流学习...
欢迎关注「前端Q」,认真学前端,做个专业的技术人...


