VUE单页运用首屏加载速率优化计划

单页运用会跟着项目越大,致使首屏加载速率很慢!!!以下给出鄙人晓得的几种优化计划

  1. 运用CDN资本,减小服务器带宽压力
  2. 路由懒加载
  3. 将一些静态js css放到其他地方(如OSS),减小服务器压力
  4. 按需加载三方资本,如iview,发起按需引入iview中的组件
  5. 运用nginx开启gzip减小收集传输的流量大小
  6. 若首屏为登录页,能够做成多进口,登录页零丁星散为一个进口
  7. 运用uglifyjs-webpack-plugin插件替代webpack自带UglifyJsPlugin插件

运用CDN资本,减小服务器带宽压力

  • 在index.html中引入cdn资本
...
  <body>
    <div id="app">
    </div>
    <!-- built files will be auto injected -->
    <script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script>
    <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script>
    <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script>
    <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script>
  </body>
  ...
  • 修正 build/webpack.base.conf.js
module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  externals:{
    'vue': 'Vue',
    'vue-router': 'VueRouter',
    'vuex':'Vuex',
    'vue-resource': 'VueResource'
  },
  ...
}
  • 修正src/main.js src/router/index.js 解释掉import引入的vue,vue-resource
// import Vue from 'vue'
// import VueResource from 'vue-resource'
// Vue.use(VueResource)

路由懒加载

require.ensure体式格局

const workCircle = r => require.ensure([], () => r(require('@/module/work-circle/Index')), 'workCircle')
const workCircleList = r => require.ensure([], () => r(require('@/module/work-circle/page/List')), 'workCircleList')

import体式格局

const workCircle = () => import('@/module/work-circle/Index')

将一些静态js css放到其他地方(如OSS),减小服务器压力

注重这里的js文件,须要将效果抛出,然后在须要用到该js的组件中import引入

按需加载三方资本,如iview,发起按需引入iview中的组件

按需援用请检察iview官方文档iview

运用nginx开启gzip减小收集传输的流量大小

《VUE单页运用首屏加载速率优化计划》

设置nginx,能够参考Nginx开启Gzip紧缩大幅进步页面加载速率

webpack开启gzip紧缩。 只须要服务器开启gzip紧缩,服务器开启gzip紧缩后,服务器拿到我们布置上去的文件,会紧缩文件然后返回给浏览器。所以前端运用gzip紧缩是没有起作用的。所以compression-webpack-plugin插件有什么用,列位讨论下QAQ

这里须要合营Nginx服务器,Nginx开启gzip

《VUE单页运用首屏加载速率优化计划》

webpack4.x以下运用compression-webpack-plugin插件,插件版本应运用1.x

webpack4.x版本以上能够运用compression-webpack-plugin 2.x

  • config/index.js中
module.exports = {
  build: {
    ...
    // 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, // 就是这里开启gzip,vue-cli搭建项目,这里默以为false
    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
  }
}
  • build/webpack.prod.conf.js中

运用vue-cli构建项目时,默许会有这段代码

if (config.build.productionGzip) {
  const CompressionWebpackPlugin = require('compression-webpack-plugin')
  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(' +
        config.build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}

若首屏为登录页,能够做成多进口,登录页零丁星散为一个进口

运用uglifyjs-webpack-plugin插件替代webpack自带UglifyJsPlugin插件

两个插件都不支撑es6紧缩,所以运用此插件前须要用东西(如babel-loader)转换es6代码

题目形貌:项目中运用iview时,致使运用UglifyJsPlugin紧缩报错

由于iview某插件中包括es6语法。然则两个插件都不支撑es6紧缩

处理要领以下:

  • 修正webpack设置文件,运用babel-loader转换一下iview插件中的es6语法
module.exports = {
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
...
  module: {
    loaders: [
      { test: /iview.src.*?js$/, loader: 'babel' },
      { test: /\.js$/, loader: 'babel', exclude: /node_modules/ }
    ],
    rules: [
    ...
      {
        test: /\.js$/,
        loader: 'babel-loader',
         // resolve('/node_modules/iview/src'),resolve('/node_modules/iview/packages')处理iview打包时UglifyJs报错
        include: [resolve('src'), resolve('test'), resolve('/node_modules/iview/src'),resolve('/node_modules/iview/packages')]
      }
      ...
    ]
  }
}
  • webpack临盆环境中
...
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
...
    new UglifyJsPlugin({
      // 运用外部引入的新版本的js紧缩东西
      parallel: true,
      uglifyOptions: {
        ie8: false, // 启用IE8支撑
        ecma: 6, // 支撑的ECMAScript的版本(5,6,7或8)。影响parse,compress&& output选项
        warnings: false, // 显现正告
        mangle: true, // debug false
        output: {
          comments: false,
          beautify: false, // debug true
        },
        compress: {
          // 在UglifyJs删除没有用到的代码时不输出正告
          warnings: false,
          // 删除一切的 `console` 语句
          // 还能够兼容ie浏览器
          drop_console: true,
          // 内嵌定义了然则只用到一次的变量
          collapse_vars: true,
          // 提取出涌现屡次然则没有定义成变量去援用的静态值
          reduce_vars: true,
        }
      }
    }),
    // new webpack.optimize.UglifyJsPlugin({
    //   compress: {
    //     warnings: false
    //   },
    //   sourceMap: true
    // }),

此要领有待实践,留待下次分享 ==

    原文作者:停停儿
    原文地址: https://segmentfault.com/a/1190000016155447
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞