A brief analysis of web front-end project optimization in Vue (with code)
In the previous article "Teach you step-by-step how to use JS to write universal modules (detailed code explanation)", I introduced how to use JS to write universal modules. The following article will give you an understanding of web front-end project optimization in Vue. Friends in need can refer to it. I hope it will be helpful to you.
I finally got free time today, and I want to optimize the kui documentation project. It's too slow to open. It takes a few seconds for http://k-ui.cn
to be fully displayed. I can't stand it. If you look at the picture, you will understand
There is nothing surprising when you see this, why it is so slow.
Because the webpack
configuration in the documentation is uselessvue-cli
scaffolding, I configured it manually, so I guess there will be more problems
1 ) The webpack configuration error caused the library to be repeatedly compiled into a file
I gradually checked the relatively large files after compilation and found that index.js is the entry file, and its content includes the vue library that has been repeatedly packaged. As shown below
Troubleshooting the webpack
configuration step by step, no problem is found, so where is the problem? I searchedvue
was introduced, and it was found that 3
files were imported vue
, but this did not affect the duplication of compilation. It shouldn’t be. Finally, the problem was discovered, because it was mac
Due to the case sensitivity of the environment, "import Vue from 'vue'
" was written as "import Vue from 'Vue'
" with a shake of the hand.
There seems to be no problemdebug
There will be no errors in debugging. But the problem arises here. The problem is solved by changing the "Vue
" after from
to lowercase "vue
". After recompilation the file is over 130 kb smaller. From 945kb to over 800kb, keep optimizing.
2) Caused by too large third-party files
Because some parts of the documentation require code highlighting, the highlight.js
code highlighting library is used in this article. I wrote a component by myself, the code is as follows:
<!-- code.vue --> <template> <div v-high class="k-code"> <pre: style="styles" ref="rel"> <code: class="lang"> <slot> </slot> </code>
//code.js import Hljs from "highlight.js"; import "highlight.js/styles/atom-one-light.css"; const vueHljs = {}; vueHljs.install = (Vue) => { Vue.directive("high", function (el, binding) { let blocks = el.querySelectorAll("pre code"); Array.prototype.forEach.call(blocks, Hljs.highlightBlock); }); }; export default vueHljs;
<!-- 调用 --> <code lang="xml html"> {{ code }} </code>
In fact, there will be no problem if the code is written like this, but why is the file so big after compilation, 800
is more than kb, so... I commented out the key code highlighting code, which is where highlight.js
was introduced. Compile again:
The compiled file is only 130kb
, and we have found the source of the problem.
I used Google's code highlighting before, but I don't need it this time, and markdown
don't want to bother with it either.
Go to node_modules
and explore carefully. Because code highlighting contains too many languages and syntax, I compile it every time and it is a full package, python
,sql
, c
, etc.50
are all in several highlighted languages, but I only need js
and html
syntax Highlight, so I put forward what I want from the library:
var Hljs = require("./highlight"); //只要这2个高亮语言库,其他干掉 Hljs.registerLanguage("xml", require("./lang/xml")); Hljs.registerLanguage("javascript", require("./lang/javascript"));
Compile again, after compilation 180kb
is still within the acceptable range.
3) The js module does not load on demand
because vue
is a single page web
, driven by router
view
, as the project becomes larger and larger, it is necessary to load this on demand, otherwise all pages will be packaged in the same js file. Causing slow loading.
On-demand loading (that is, lazy loading) has 3
implementation methods
1) Vue’s own asynchronous method
in Just make modifications when router push
{ path: '/test', name: 'test', component: resolve => require(['../components/test'], resolve) }
2) Import() of es proposal
Official document
Pay attention to the content and name The same will be packaged into a file
const test = () => import( /* webpackChunkName: "test" */ '../components/test') { path: '/test', name: 'test', component: test },
3) require.ensure() provided by webpack
Note that ensure passes parameters, the last chunkname, and the output configuration is not passed chunkFilename: will be [id].build.js
{ path: '/test', name: 'test', component: resolve => require.ensure([], () => resolve(require('../components/test')), 'test') },
Note: require.ensure() is unique to webpack and has been replaced by import().
The above 3
methods can achieve on-demand loading, and finally configure chunkFilename
<pre class='brush:php;toolbar:false;'>output: {
path: path.resolve(__dirname, &#39;../dist&#39;),
filename: &#39;js/[name].js&#39;, //.[hash].js&#39;,
publicPath: &#39;/&#39;,
chunkFilename: &#39;js/[name].[chunkhash:3].js&#39;,
},</pre># in
webpack config
import Vue from "vue"; import Router from "vue-router"; Vue.use(Router); let routes = []; let r = [ "", "install", "start", "log", "input", "button", "select", "switch", "form", "colorpicker", "loading", "icon", "timeline", "theme", "react-kui", "angular-kui", "alert", "message", "notice", "upload", "poptip", "menu", "tabs", "badge", "checkbox", "radio", "datepicker", "table", "layout", "page", "modal", "kyui-loader", "sponsor", "about", ]; r.forEach((x) => { routes.push({ path: `/${x}`, component: (resolve) => require([x == "" ? "./ui/index" : `./ui/${x}`], resolve), // component: r => require.ensure([], () => r(require(x==''?'/ui/index': `./ui/${x}` )), x) }); }); let routers = new Router({ routes: routes, mode: "history", }); export default routers;There seems to be no problem with on-demand loading, but the finally packaged
chunkFilename has
300kb, and all pages are typed into a
js file.
探究了一番,因为是异步加载,所以不能动态传值的,map
遍历的时候路径组合x
值是动态传入,导致打包后无法识别。最后修改为静态的,问题解决了。重新编译后多个页面路由分割成单个js
文件,每个约10kb
左右,路由改变时,动态加载对应的js
文件
import xx from '/dev/test‘ //这里的abc 是静态的值 如 ‘/ui/abc.vue’ { path: 'xx', component: xx }
至此,问题解决了,页面加载正常情况下延时1-2秒,时间缩短了将近10陪。
【完】
推荐学习:vue.js高级教程
The above is the detailed content of A brief analysis of web front-end project optimization in Vue (with code). For more information, please follow other related articles on the PHP Chinese website!

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

Reasons for Vue.js' popularity include simplicity and easy learning, flexibility and high performance. 1) Its progressive framework design is suitable for beginners to learn step by step. 2) Component-based development improves code maintainability and team collaboration efficiency. 3) Responsive systems and virtual DOM improve rendering performance.

Vue.js is easier to use and has a smooth learning curve, which is suitable for beginners; React has a steeper learning curve, but has strong flexibility, which is suitable for experienced developers. 1.Vue.js is easy to get started with through simple data binding and progressive design. 2.React requires understanding of virtual DOM and JSX, but provides higher flexibility and performance advantages.

Vue.js is suitable for fast development and small projects, while React is more suitable for large and complex projects. 1.Vue.js is simple and easy to learn, suitable for rapid development and small projects. 2.React is powerful and suitable for large and complex projects. 3. The progressive features of Vue.js are suitable for gradually introducing functions. 4. React's componentized and virtual DOM performs well when dealing with complex UI and data-intensive applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
