How to use webpack4+SplitChunksPlugin
这次给大家带来如何使用webpack4+SplitChunksPlugin,使用webpack4+SplitChunksPlugin的注意事项有哪些,下面就是实战案例,一起来看一下。
写在前面
前面写了一篇有关webpack4的不完全升级指南以及在webpack3.x迁移的时候遇到的问题,有兴许可以看一下。
0. 参数介绍
先对参数有一个大概的认识,虽然撸了很多遍官方的更新文档,但是还是去参看了一下新的wbepack源码,下面是各种参数及含义:
chunks: 表示显示块的范围,有三个可选值:
initial
(初始块)、async
(按需加载块)、all
(全部块),默认为all
;minSize: 表示在压缩前的最小模块大小,默认为0;
minChunks: 表示被引用次数,默认为1;
maxAsyncRequests: 最大的按需(异步)加载次数,默认为1;
maxInitialRequests: 最大的初始化加载次数,默认为1;
name: 拆分出来块的名字(Chunk Names),默认由块名和hash值自动生成;
cacheGroups: 缓存组。
对于缓存组是一个对象,处了可以有上面的chunks
、minSize
、minChunks
、maxAsyncRequests
、maxInitialRequests
、name
外,还有其他的一些参数:
如果不在缓存组中重新赋值,缓存组默认继承上面的选项,但是还有一些参数是必须在缓存组进行配置的。
priority: 表示缓存的优先级;
test: 缓存组的规则,表示符合条件的的放入当前缓存组,值可以是
function
、boolean
、string
、RegExp
,默认为空;reuseExistingChunk: 表示可以使用已经存在的块,即如果满足条件的块已经存在就使用已有的,不再创建一个新的块。
1. 基本使用
首先,在新版本的webpack会默认对代码进行拆分,拆分的规则是:
模块被重复引用或者来自
node_modules
中的模块在压缩前最小为30kb
在按需加载时,请求数量小于等于5
在初始化加载时,请求数量小于等于3
小于30kb的模块不值得再单独发送一次请求,在很小的模块的前提下,相比与多次打包,减少请求次数成本要低。
当然也可以不使用默认的配置,比如这样:
new webpack.optimize.SplitChunksPlugin({ chunks: "all", minSize: 20000, minChunks: 1, maxAsyncRequests: 5, maxInitialRequests: 3, name: true )}
上面的代码就表示,在所有代码中,引用模块大小最小为20kb,引用次数最少为1次,按需加载最大请求次数为5,初始化加载最大请求次数为3的所有模块就行拆分到一个单独的代码块中,name
表示代码的名字,设置为true
则表示根据模块和缓存组秘钥自动生成。
2. 使用缓存组(Cache Groups)
如果想继续细分代码,可以使用缓存组(Cache Groups)。同样的,缓存组也有默认的配置;缓存组默认将node_modules
中的模块拆分带一个叫做vendors
的代码块中,将最少重复引用两次的模块放入default
中。
这是一段官方里面的代码:
splitChunks: { chunks: "async", minSize: 30000, minChunks: 1, maxAsyncRequests: 5, maxInitialRequests: 3, name: true, cacheGroups: { default: { minChunks: 2, priority: -20 reuseExistingChunk: true, }, vendors: { test: /[\\/]node_modules[\\/]/, priority: -10 } } }
上面是缓存组的默认配置,可以通过default:false
禁用默认的缓存组,然后就可以自定义缓存组,将初始化加载时被重复引用的模块进行拆分,就像这样:
cacheGroups: { commons: { name: "commons", chunks: "initial", minChunks: 2 } }
之后就随心所欲,可以根据具体的需求,创建多个缓存组:
cacheGroups: { a: { // ... }, b: { // ... } // ... }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
Detailed analysis of webpack style loading
The above is the detailed content of How to use webpack4+SplitChunksPlugin. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.
