zoukankan      html  css  js  c++  java
  • 解决vue-cli npm run build之后vendor.js文件过大的方案

    问题描述
    npm run build命令默认把dependencies中的依赖统一打包,导致vendor.js文件太大,出现首屏加载过于缓慢的问题。

    解决方案
    像vue、axios、element-ui、ivew这些基本上不会改变的依赖我们可以把它们用cdn导入,没有必要打包到vendor.js中。

    <div id="app"></div>
    <!-- 先引入 Vue -->
    <!--开发环境-->
    <script src="https://cdn.bootcss.com/vue/2.5.3/vue.js"></script>
    <!--生产环境-->
    <!--<script src="https://cdn.bootcss.com/vue/2.5.3/vue.min.js"></script>-->
    <!-- 引入组件库 -->
    <script src="https://cdn.bootcss.com/axios/0.17.1/axios.min.js"></script>
    <script src="https://cdn.bootcss.com/element-ui/1.4.10/index.js"></script>


     

     2.项目根目录下build/webpack.base.config.js中添加externals

    externals: {
        vue: 'Vue',
        'element': 'element-ui',
        'axios':'axios',
      },
    

    3.mian.js中import ... from ...与Vue.use(...)就可以去掉了,若没去掉webpack还是会把对应的依赖进行打包。

    2019.01.12补充
    vue-cli 2.x
    在项目config/index.js中可以开启gzip压缩,对打包优化也有很大的帮助

    1.首先安装插件 compression-webpack-plugin
    cnpm install --save-dev compression-webpack-plugin
     

     2.设置productionGzip: true

        // Gzip off by default as many popular static hosts such as
        // Surge or Netlify already gzip all static assets for you.
        // Before setting to `true`, make sure to:
        // npm install --save-dev compression-webpack-plugin
        productionGzip: true,
        productionGzipExtensions: ['js', 'css'],
    
        // Run the build command with an extra argument to
        // View the bundle analyzer report after build finishes:
        // `npm run build --report`
        // Set to `true` or `false` to always turn it on or off
        bundleAnalyzerReport: process.env.npm_config_report
    

    3.npm run build执行后会发现每个js和css文件会压缩一个gz后缀的文件夹,浏览器如果支持g-zip 会自动查找有没有gz文件 找到了就加载gz然后本地解压 执行。

    vue-cli 3.x

    1.安装插件compression-webpack-plugin

    npm install --save-dev compression-webpack-plugin
    

    2.修改vue.config.js文件

    ...
    // gzip压缩插件
    const CompressionWebpackPlugin = require('compression-webpack-plugin')
    
    ...
     // webpack配置
      configureWebpack: config => {
        let plugins = [
          new CompressionWebpackPlugin({
            filename: '[path].gz[query]',
            algorithm: 'gzip',
            test: new RegExp(
              '\.(' +
              ['js', 'css'].join('|') +
              ')$',
            ),
            threshold: 10240,
            minRatio: 0.8,
          }),
        ]
        if (process.env.NODE_ENV !== 'development') {
          config.plugins = [...config.plugins, ...plugins]
        }
      },
    ...
    
    

    vue.config.js:

    module.exports = {
        pages,
        devServer: {
            host: "localhost",
            port: `5001`,
            // 代理链接配置
            proxy: {
                "/api": {
                    target: proxyHost,
                    changeOrigin: true,
                    pathRewrite: {
                        "^/api": ""
                    }
                }
            }
        },
        configureWebpack: config=>{
            return {
                plugins:[new webpack.BannerPlugin({
                    banner:"hash:[hash],chunkhash:[chunkhash],filename:[name],author:'lllomh',time:"+new Date()
                }),
                    new CompressionWebpackPlugin({
                        filename: '[path].gz[query]',
                        algorithm: 'gzip',
                        test: new RegExp(
                            '\.(' +
                            ['js', 'css'].join('|') +
                            ')$',
                        ),
                        threshold: 10240,
                        minRatio: 0.8,
                    })
                ]
            }
        }
    }
    

    3.npm run build执行后会发现每个js和css文件会压缩一个gz后缀的文件夹,浏览器如果支持g-zip 会自动查找有没有gz文件 找到了就加载gz然后本地解压 执行。

    本文通过互联网收集整理

  • 相关阅读:
    Task示例,多线程
    request
    do put in ruby
    Ruby零星笔记
    Git的常用操作
    如何在Rails中执行Get/Post/Put请求
    Lua中的基本函数库
    Step By Step(Lua目录)
    position:fixed失效原因
    前端性能监控-window.performance.timing篇
  • 原文地址:https://www.cnblogs.com/lllomh/p/14991907.html
Copyright © 2011-2022 走看看