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
This article example shares the specific code of ...
In development, it is often necessary to cache th...
Table of contents Preface: accomplish: Summarize:...
MySql batch insert optimization Sql execution eff...
1. Introduction This article will show you how to...
I made a Dockerfile for openresty on centos7 and ...
This article shares with you a draggable photo wa...
Preface When developing a gateway project, the si...
Table of contents 1. Simple page example 2.uni-ap...
Table of contents Basic selectors: Level selector...
Table of contents Preface 1. Environment Configur...
1. Advantages and Disadvantages of Indexes Advant...
By default, Nginx supports only one SSL certifica...
Table of contents Scope Global Scope Function Sco...
1. Introduction to TypeScript The previous articl...