webpack 之常用的 plugin

718 阅读3分钟

小知识,大挑战!本文正在参与“  程序员必备小知识  ”创作活动

本文同时参与 「掘力星计划」  ,赢取创作大礼包,挑战创作激励金

  • 作⽤于webpack打包整个过程
  • webpack的打包过程是有(⽣命周期概念)钩⼦ plugin 可以在webpack运⾏到某个阶段的时候,帮你做⼀些事情,类似于⽣命周期的概念 扩展插件,在 Webpack 构建流程中的特定时机注⼊扩展逻辑来改变构建结果或做你想要的事情。 作⽤于整个构建过程。

HtmlWebpackPlugin

htmlwebpackplugin会在打包结束后,⾃动⽣成⼀个html⽂件,并把打包⽣成的js模块引⼊到该html 中

配置:

title: ⽤来⽣成⻚⾯的 title 元素
filename: 输出的 HTML ⽂件名,默认是 index.html, 也可以直接配置带有⼦⽬录。
template: 模板⽂件路径,⽀持加载器,⽐如 html!./index.html
inject: true | 'head' | 'body' | false ,注⼊所有的资源到特定的 template 或者
templateContent 中,如果设置为 true 或者 body,所有的 javascript 资源将被放置到 body
元素的底部,'head' 将放置到 head 元素中。
favicon: 添加特定的 favicon 路径到输出的 HTML ⽂件中。
minify: {} | false , 传递 html-minifier 选项给 minify 输出
hash: true | false, 如果为 true, 将添加⼀个唯⼀的 webpack 编译 hash 到所有包含的脚本和
CSS ⽂件,对于解除 cache 很有⽤。
cache: true | false,如果为 true, 这是默认值,仅仅在⽂件修改之后才会发布⽂件。
showErrors: true | false, 如果为 true, 这是默认值,错误信息会写⼊到 HTML ⻚⾯中
chunks: 允许只添加某些块 (⽐如,仅仅 unit test 块)
chunksSortMode: 允许控制块在添加到⻚⾯之前的排序⽅式,⽀持的值:'none' | 'default' |
{function}-default:'auto'
excludeChunks: 允许跳过某些块,(⽐如,跳过单元测试的块)

案例:

const path = require("path");
const htmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
 ...
 plugins: [
 new htmlWebpackPlugin({
 title: "My App",
 filename: "app.html",
 template: "./src/index.html"
 })
 ]
};
//index.html
<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <meta http-equiv="X-UA-Compatible" content="ie=edge" />
 <title><%= htmlWebpackPlugin.options.title %></title>
 </head>
 <body>
 <div id="root"></div>
</body>
</html>

clean-webpack-plugin (⾃动清理构建⽬录)

避免构建前每次都需要⼿动删除 dist

使⽤ clean-webpack-plugin

  • 默认会删除 output 指定的输出⽬录
module.exports = {
entry: {
app: './src/app.js',
search: './src/search.js'
},
output: {
filename: '[name][chunkhash:8].js',
path: __dirname + '/dist'
},
plugins: [
+ new CleanWebpackPlugin()
}

mini-css-extract-plugin

将 css 文件打成单独的文件

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
{
 test: /\.css$/,
 use: [MiniCssExtractPlugin.loader, "css-loader"]
}
 new MiniCssExtractPlugin({
 filename: "[name][chunkhash:8].css"
 })

html-inline-css-webpack-plugin

将 css 内嵌在 html,减少资源请求,同时减少页面抖动

  • 依赖 html-webpack-plugin、 mini-css-extract-plugin 插件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HTMLInlineCSSWebpackPlugin = require("html-inline-css-webpack-plugin").default;

module.exports = {
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    }),
    new HtmlWebpackPlugin(),
    new HTMLInlineCSSWebpackPlugin(),
  ],
  module: {
    rules: [
      {
        test: /.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader"
        ]
      }
    ]
  }
}

原理:

  • 监听 html-webpack-plugin 里的 beforeAssetTagGeneration hook(生成静态标签之前) 将相关的要内联的样式内容(assets[fileName].source())存储起来,并且防止生成对应的 link 标签(data.assets.css.splice.splice)
  • 监听 html-webpack-plugin 里的 beforeEmit hook 将存储的要内联 css 样式以 style 形式注入

style-loader 与 html-inline-css-webpack-plugin

  • style-loader: 插入样式是动态的过程,是在运行 js 文件时,将样式插入 style 标签
  • css-loader:将 css 转换成 commonjs 对象,将样式代码放在 js 里面
  • html-inline-css-webpack-plugin:将 css 提取打包成一个独立的 css 文件,然后读取提取出的 css 内容注入到页面的 style 里面去。这个过程在构建阶段完成