background One of the most important points in achieving high-performance applications is to allow users to load only necessary resources each time as much as possible. Resources that are not too high in priority should be obtained progressively using technologies such as delayed loading, so as to ensure the first screen speed of the page. Code sharding is a technology unique to the webpck packaging tool. This feature allows the code to be split into specific forms so that users do not have to load all of it at once, but can load it on demand. CommonsChunkPluginAlthough this plug-in is no longer recommended in webpack4, we still need to understand it. This plug-in can extract the common parts from multiple Chunks. Common module extraction can bring several benefits to several projects:
The default rule of this plugin is that as long as a module is used by two entry chunks, it will be extracted. For example, as long as a and b use react, react will be extracted. But it still has some shortcomings:
splitChunks This is a new feature of webpack, which improves the code segmentation feature of CommonChunkPlugin and redesigns and implements it. It is not only more powerful than CommonChunkPlugin, but also simpler and easier to use. The code is as follows module.exports = { entry: './foo.js', output: { filename: 'foo.js', publicPath: '/dist/' }, mode: 'development', optimization: splitChunks: { chunks: 'all', } } } // foo.js import React from 'react'; import('./bar.js'); document.write('foo.js', React.version); //bar.js import react from 'react'; console.log('bar.js', React.version); The default extraction conditions of splitChunk are:
ConfigurationsplitChunk: { chunks: 'async', minSize: { javascript: 30000, style: 50000, }, maxSize: 0, minChunks: 1, maxAsyncRequests: 5, maxInitialRequests: 3, automaticNameDelimiter: '~', name: true, cacheGroups: vendor: test: /[\\/]node_modules[\\/]/, priority: -10, }, default: { minChunks: 2, priority: -20, reuseExistingChunk: true } } } Matching Pattern Matching conditions name cacheGroup Loading resources asynchronously The main problem that asynchronous resource loading solves is that when there are too many modules and the resource size is too large, some modules that are not used temporarily can be delayed in loading. This way, the resources downloaded by the user when the page is rendered for the first time are as small as possible, and subsequent modules are triggered to load when needed, so this is generally called on-demand loading. Summarize There are several ways to split code - CommonChunkPlugin or SplitChunks, as well as asynchronous resource loading. With the help of these methods, the resource size can be effectively reduced, while the cache can be better utilized to provide users with a more user-friendly experience. This is the end of this article about the implementation of webpack code slicing. For more relevant webpack code slicing content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Examples of simple add, delete, modify, and query operations using mysql statements
<br />I'm basically going crazy with thi...
1. Command Introduction The ln command is used to...
Recently I have been saying that design needs to h...
After installing the database, if you accidentall...
CSS3Please Take a look at this website yourself, ...
1. Merge the margins of sibling elements The effe...
First, let’s think about a question: To insert su...
Hello everyone, I am Qiufeng. Recently, WeChat ha...
This article shares the specific code of JavaScri...
1. Abnormal performance of Docker startup: 1. The...
Preface Last week, a colleague asked me: "Br...
Recently, I have been learning to use nginx to pl...
The div element is used to provide structure and b...
Table of contents 1.mysqldump Execution process: ...
Quickly modify the table structure of a MySQL tab...