html 和 js 压缩

1,js 压缩:

将 mode 改为“production" 即可,production 模式下,webpack 会自动压缩 js 文件

2,html 压缩:

src / index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>webpack</title>
</head>
<body>
    <!-- 这是注释 -->
    <h1>hello webpack</h1>
</body>
</html>

webpack配置:

const {resolve} = require(‘path‘)
const HtmlWebpackPlugin = require(‘html-webpack-plugin‘)

module.exports={
    entry:‘./src/index.js‘,
    output:{
        filename:‘bundle.js‘,
        path:resolve(__dirname,‘build‘)
    },
    plugins:[
        new HtmlWebpackPlugin({
            template:‘./src/index.html‘,
            minify:{
                collapseWhitespace:true, //移除空格
                removeComments:true //移除注释
            }
        })
    ],
    mode:‘development‘
}

打包后的 index.html,清除了空格和注释,

html 和 js 压缩