webpack中使用html-webpack-plugin生成HTML文件並主動插入css和js引入標簽


  • html-webpack-plugin
  • clean-webpack-plugin

 一、html-webpack-plugin

由於打包時生成的css樣式文件和js腳本文件會采用hash值作為文件命名的一部分,每一次調試打包結果都需要手動修改名稱,這種做法就違背了webpack的自動化打包的初衷,而且還有需求就是要對html文件進行優化壓縮,也不能直接在源文件上進行操作,還有清除注釋等一系列操作。

npm install html-webpack-plugin --save-dev

更詳細的教程文檔可以參考npm插件文檔:https://www.npmjs.com/package/html-webpack-plugin

 1 var HtmlWebpackPlugin = require('html-webpack-plugin');
 2 module.exports = {
 3     plugins: [
 4         //生成html文件
 5         new HtmlWebpackPlugin({
 6             filename:'index.html',//生成的文件名
 7             template:'./index.html',//指定打包壓縮的文件
 8             minify:{
 9                 removeComments:true,//清除注釋
10                 collapseWhitespace:true//清理空格
11             }
12         })
13 } 

當然也可以同時處理多個html文件(通過chunks屬性):

1 plugins: [
2   new HtmlWebpackPlugin({
3     chunks: ['app']
4   })
5 ]

 二、clean-webpack-plugin

clean-webpack-plugin插件是用來清理構建文件夾,將上一次構建的文件全部清除,這個插件很簡單,只需要plugins中引入就可以,沒有什么多余的配置,但是需要注意的是在創建變量的時候需要使用大括號將變量名包裹起來,不然有時會出現報錯情況,原因尚不明確:

1 const {CleanWebpackPlugin} = require('clean-webpack-plugin');
2 module.exports = {
3     plugins: [
4         new CleanWebpackPlugin()//清理構建文件夾
5     ]
6 }     

 

這邊博客是基於上一篇博客的基礎上測試的,所有測試代碼與上一篇博客一致,只有配置文件增加了一些新的功能,下面貼上全部配置文件代碼:

 1 var path = require("path");
 2 const glob = require('glob');
 3 const PurifyCSSPlugin = require('purifycss-webpack');
 4 var MiniCssExtractPlugin = require("mini-css-extract-plugin");
 5 var HtmlWebpackPlugin = require('html-webpack-plugin');
 6 const {CleanWebpackPlugin} = require('clean-webpack-plugin');
 7 module.exports = {
 8     entry: {
 9         index: './src/index.js',
10     },
11     output: {
12         path: path.resolve(__dirname, "dist"),
13         filename: "[name]-[hash:5].js",
14         // publicPath:'/dist'
15     },
16     module: {
17         rules: [
18             {
19                 test: /\.less$/,
20                 use: [
21                     // {loader:'style-loader'},
22                     { loader: MiniCssExtractPlugin.loader },
23                     { loader: 'css-loader' },
24                     {
25                         loader: 'postcss-loader',
26                         options: {
27                             ident: 'postcss',
28                             plugins: [
29                                 // require('autoprefixer')(),//添加前綴
30                                 require('postcss-cssnext')(),//添加前綴 轉換css未來語法
31                                 require('cssnano')({
32                                     preset: 'default'
33                                 }), 
34                             ]
35                         }
36                     },
37                     { loader: 'less-loader' }],
38             }
39         ]
40     },
41     plugins: [
42         new MiniCssExtractPlugin({
43             // Options similar to the same options in webpackOptions.output
44             // both options are optional
45             filename: "[name]-[hash:5].css",
46             // chunkFilename: "[id].css"
47         }),
48         new HtmlWebpackPlugin({
49             filename:'index.html',//生成的文件名
50             template:'./index.html',//打包壓縮的文件
51             minify:{
52                 removeComments:true,//清除注釋
53                 collapseWhitespace:true//清理空格
54             }
55         }),
56         new CleanWebpackPlugin()
57         // new PurifyCSSPlugin({
58         //     paths:glob.sync(path.join(__dirname,'../index.html'))
59         // })
60     ]
61 }     
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM