Home > Web Front-end > JS Tutorial > body text

What are the ways vue-cli can shorten the first screen loading time?

php中世界最好的语言
Release: 2018-05-02 15:27:13
Original
1656 people have browsed it

This time I will bring you how vue-cli can shorten the loading time of the first screen. What methods vue-cli can shorten the loading time of the first screen. What are the precautions?. Here are practical cases. One Get up and take a look.

The project requirements of my recent internship did not have many requirements, so I learned about project optimization. The main reason was that the first screen loaded too slowly.

Large file positioning

We can use the webpack visual plug-inWebpack Bundle Analyzer View the project js file size, and then have The purpose is to solve the js file that is too large.

Installation

npm install --save-dev webpack-bundle-analyzer
Copy after login

Set as follows in webpack, and then npm run dev By default, it will be displayed on port 8888.

const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
 plugins: [
  new BundleAnalyzerPlugin()
 ]
}
Copy after login

JS files are loaded on demand

Without this setting, all JS files of the entire website will be loaded when the first screen of the project is loaded, so It is a good optimization method to disassemble the JS file and load the JS of the page when you click on the page.

What is used here is the lazy loading of vue components. In router.js, do not use the import method to introduce components, use require.ensure.

import index from '@/components/index'
const index = r => require.ensure( [], () => r (require('@/components/index'),'index'))
//如果写了第二个参数,就打包到该`/JS/index` 的文件中。
//不写第二个参数,就直接打包在`/JS` 目录下。
const index = r => require.ensure( [], () => r (require('@/components/index')))
Copy after login

When using cdn

, replace vue, vuex, vue-router, axios, etc. with domestic bootcdn and directly introduce them to In index.html in the root directory.

Add externals in webpack settings and ignore libraries that do not need to be packaged.

externals: { 
 'vue': 'Vue', 
 'vue-router': 'VueRouter', 
 'vuex': 'Vuex', 
 'axios': 'axios' 
}
Copy after login

Use cdn to import in index.html.

 

 
Copy after login

Place the JS file at the end of the body

By default, in the built index.html, js is introduced in header.

Use the html-webpack-plugin plug-in and change the value of inject to body. You can put the js introduction at the end of the body.

var HtmlWebpackPlugin = require('html-webpack-plugin');
new HtmlWebpackPlugin({
   inject: 'body',
})
Copy after login

Compress the code and remove the console

Use the UglifyJsPlugin plug-in to compress the code and remove the console.

new webpack.optimize.UglifyJsPlugin({
 compress: {
  warnings: false,
  drop_console: true,
  pure_funcs: ['console.log']
 },
 sourceMap: false
})
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to split the page code and load it on demand

Implement js origin policy and cross-domain Detailed explanation of access steps

The above is the detailed content of What are the ways vue-cli can shorten the first screen loading time?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!