解决 vendor.js 过大的问题

开启gzip压缩

  • 安装插件 compression-webpack-plugin
npm install --save-dev [email protected]
  • 修改build文件夹中webpack.prod.conf.js文件,将asset改为filename;
if (config.build.productionGzip) {
    const CompressionWebpackPlugin = require('compression-webpack-plugin')

    webpackConfig.plugins.push(
        new CompressionWebpackPlugin({
            // asset: '[path].gz[query]',
            filename: '[path].gz[query]',
            algorithm: 'gzip',
            test: new RegExp(
                '\\.(' +
                config.build.productionGzipExtensions.join('|') +
                ')$'
            ),
            threshold: 10240,
            minRatio: 0.8
        })
    )
}
  • 设置config/index.js中productionGzip: true;
build: {
        index: path.resolve(__dirname, '../dist/index.html'),
        assetsRoot: path.resolve(__dirname, '../dist'),
        assetsSubDirectory: 'static',
        assetsPublicPath: './',
        productionSourceMap: false,
        devtool: '#source-map',
        productionGzip: true,// 这里开启Gzip
        productionGzipExtensions: ['js', 'css'],
        bundleAnalyzerReport: process.env.npm_config_report
    }

成功从7MB压缩到1.4MB

你可能感兴趣的:(webpack)