1.js代码压缩

js的代码压缩和webpack的环境有关。
当时生产环境时,会调用UglifyJsPlugin插件压缩js代码。

  1. module.exports = {
  2. mode: 'production',
  3. // mode: 'development',
  4. // ...
  5. }

2.html代码压缩

配置HtmlWebpackPlugin插件的minify选项即可:
webpack.config.js

// ...
module.exports = {
    // ...
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
            minify: {
                // 移除空格
                collapseWhitespace: true,
                // 移除注释
                removeComments: true,
                // keepClosingSlash: true,
                // removeRedundantAttributes: true,
                // removeScriptTypeAttributes: true,
                // removeStyleLinkTypeAttributes: true,
                // useShortDoctype: true
            }
        }),
    ],
}