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

vue-cli project optimization method-shorten the first screen loading time

亚连
Release: 2018-05-28 09:52:58
Original
976 people have browsed it

This article mainly introduces the vue-cli project optimization to shorten the first screen loading time. Friends who need it can refer to it

The recent internship project requirements do not have many requirements, so I learned about project optimization, mainly The first screen loads 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, thennpm run dev will be displayed on port 8888 by default.

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

JS files are loaded on demand

If there is no this setting, the first screen of the project When loading, all the JS files of the entire website will be loaded, so it is a good optimization method to disassemble the JS files 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

, put vue, vuex, vue-router , axios, etc., use domestic bootcdn and directly introduce it into 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.

<script src="//cdn.bootcss.com/vue/2.2.5/vue.min.js"></script> 
<script src="//cdn.bootcss.com/vue-router/2.3.0/vue-router.min.js"></script>
<script src="//cdn.bootcss.com/vuex/2.2.1/vuex.min.js"></script> 
<script src="//cdn.bootcss.com/axios/0.15.3/axios.min.js"></script>
Copy after login

Place the JS file at the end of the body

By default, build In the subsequent index.html, js is introduced in the 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(&#39;html-webpack-plugin&#39;);
new HtmlWebpackPlugin({
   inject: &#39;body&#39;,
})
Copy after login

Compress the code and remove the console

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

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

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Tutorial on using WeChat applet block

Detailed explanation of PHP late static binding analysis and application

Detailed explanation of the usage of WeChat applet wx:for and wx:for-item

The above is the detailed content of vue-cli project optimization method-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!