vue项目build打包优化

打包优化插件

  • CompressionWebpackPlugin
    Gzip打包压缩,压缩js、html、css

  • terser-webpack-plugin
    清除console和debugger

  • image-webpack-loader
    压缩图片

  • webpack-bundle-analyzer
    打包生成文件大小分析工具

打包优化配置

const CompressionWebpackPlugin = require('compression-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const isProduction = process.env.NODE_ENV === 'production'; //生产环境

//cdn加速地址
const cdn = {
    css: [],
    js: [
        'https://cdn.bootcss.com/vue/2.6.10/vue.runtime.min.js',
        'https://cdn.bootcss.com/vue-router/3.1.3/vue-router.min.js',
        'https://cdn.bootcss.com/vuex/3.1.2/vuex.min.js',
        'https://cdn.bootcss.com/axios/0.19.0/axios.min.js'
    ]
};

module.exports = {
    productionSourceMap: false, //不生成map文件,
    publicPath: isProduction ? './' : '/', //打包时区分开发环境与生产环境静态资源路径
    chainWebpack: config => {
    
    	// 生产环境
        if (isProduction) {
        
            // 注入cdn加速
            config.plugin('html').tap(args => {
                args[0].cdn = cdn;
                return args;
            });

            // 开启图片压缩
            config.module
                .rule('images')
                .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
                .use('image-webpack-loader')
                .loader('image-webpack-loader')
                .options({ bypassOnDebug: true });
        }
    },
    configureWebpack: config => {
    	// 生产环境
        if (isProduction) {
        
            // 用cdn方式引入
            config.externals = {
                vue: 'Vue',
                vuex: 'Vuex',
                'vue-router': 'VueRouter',
                axios: 'axios'
            };

            // gzip压缩
            config.plugins.push(
                new CompressionWebpackPlugin({
                    algorithm: 'gzip',
                    test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
                    threshold: 10240,
                    minRatio: 0.8
                })
            );

            // 压缩js、html、css
            config.plugins.push(
                new CompressionWebpackPlugin({
                    test: /\.js$|\.html$|.\css/, // 匹配文件名
                    threshold: 10240, // 对超过10k的数据压缩
                    deleteOriginalAssets: false // 不删除源文件
                })
            );

            // 删除console和debugger
            config.plugins.push(
                new TerserPlugin({
                    terserOptions: {
                        ecma: undefined,
                        warnings: false,
                        parse: {},
                        compress: {
                            drop_console: true,
                            drop_debugger: false, //是否删除debugger
                            pure_funcs: ['console.log'] // 移除console
                        }
                    }
                })
            );
        }
    }
};


<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width,initial-scale=1.0" />
        <link rel="icon" href="<%= BASE_URL %>favicon.ico" />
        <title>vue项目build打包优化title>
    head>
    <body>
        <noscript>
            <strong>We're sorry but longdian-management doesn't work properly without JavaScript enabled. Please enable it to continue.strong>
        noscript>
        <div id="app">div>
        
        <% for (var i in htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.js) { %>
        <script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>">script>
        <% } %>
    body>
html>

打包生成文件分析工具

npm run build -- --report

vue项目build打包优化_第1张图片

参考文章

vue-cli 3.0 build包太大导致首屏过长的解决方案
https://www.jianshu.com/p/d1fb954f5713?utm_source=oschina-app

vue打包优化
https://www.jianshu.com/p/171e8e529f35
https://www.jianshu.com/p/130a856467a1

vue cli3 优化配置生产去除console.log - from UglifyJs warnings is not a supported option
https://blog.csdn.net/sinat_35538827/article/details/99672544

Vue打包优化 详解 https://blog.csdn.net/zhenghao35791/article/details/93649587

你可能感兴趣的:(vue,webpack,build)