zoukankan      html  css  js  c++  java
  • vue打包解决 首屏加载速度慢

    1. productionSourceMap

      vue.config.js 中将 productionSourceMap 参数值设置为false,   去掉打包的时候生成的map文件

    2.  CompressionWebpackPlugin

      打包生成 gzip 压缩文件

      安装: npm install  compression-webpack-plugin --save-dev

      修改 vue.config.js 的配置文件

    const CompressionWebpackPlugin = require("compression-webpack-plugin");
    
    /**
       *  需要在改下nginx的配置  启用gzip压缩
       * */
    configureWebpack: {
        plugins: [
          new CompressionWebpackPlugin({
            filename: "[path][base].gz",
            algorithm: "gzip",
            // test: new RegExp('\.(' + productionGzipExtensions.join('|') + ')$'),  //匹配文件名
            test: /.js$|.html$|.css/,
            threshold: 10240, //对超过10k的数据压缩
            minRatio: 0.8,
            deleteOriginalAssets: false //不删除源文件
          })
        ]
      }

    3. 使用cdn

        修改 vue.config.js

    const cdn = {
      css: ["//unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css"],
      js: [
        "//unpkg.com/vue@2.6.11/dist/vue.min.js",
        "//unpkg.com/vue-router@3.0.1/dist/vue-router.min.js",
        "//unpkg.com/vuex@3.1.3/dist/vuex.min.js",
        "//unpkg.com/element-ui@2.13.0/lib/index.js"
      ]
    };
    
    module.exports = {
      pages: {
        index: {
          entry: "src/main.js",
          template: "public/index.html",
          filename: "index.html",
          title: "中国手球协会",
          chunks: ["chunk-vendors", "chunk-common", "index"],
          cdn: cdn
        }
      },
      configureWebpack: {
        externals: {
          vue: "Vue",
          "element-ui": "ELEMENT",
          "vue-router": "VueRouter",
          vuex: "Vuex"
        },
      }
    }

      然后修改 public/index.html

    <!DOCTYPE html>
    <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,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <link rel="icon" href="<%= BASE_URL %>favicon.ico">
        <title><%= htmlWebpackPlugin.options.title %></title>
        <% for (let i in htmlWebpackPlugin.options.cdn.css) { %>
          <link rel="stylesheet" href="<%= htmlWebpackPlugin.options.cdn.css[i] %>" />
        <% } %>
      </head>
      <body>
        <noscript>
          <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
        </noscript>
        <div id="app"></div>
        <!-- built files will be auto injected -->
        <% for (let i in htmlWebpackPlugin.options.cdn.js) { %>
          <script
            type="text/javascript"
            src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"
          ></script>
        <% } %>
      </body>
    </html>
  • 相关阅读:
    终于找到了一本PYTHON的中文书籍
    深圳测试协会第二次活动顺利举行
    Peer review
    如何在LINUX/UNIX上运行PYTHON程序
    CMMI各级关注的过程域(原创)
    深圳测试协会第三次活动顺利结束
    深圳测试沙龙第二期活动园满结束
    51testing软件测试沙龙
    sql中使用游标
    关于Asp.net 页面动态加载用户控件,出现“未能加载视图状态”的原因[续]
  • 原文地址:https://www.cnblogs.com/shenjilin/p/14927937.html
Copyright © 2011-2022 走看看