bundle-splitting
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBundle Splitting
代码包拆分
When building a modern web application, bundlers such as Webpack or Rollup take an application's source code, and bundle this together into one or more bundles. When a user visits a website, the bundle is requested and loaded in order to display the data to the user's screen.
JavaScript engines such as V8 are able to parse and compile data that's been requested by the user as it's being loaded. Although modern browsers have evolved to parse and compile the code as quickly and performantly as possible, the developer is still in charge of optimizing two steps in the process: the loading time and execution time of the requested data. We want to make sure we're keeping the execution time as short as possible to prevent blocking the main thread.
在构建现代Web应用时,Webpack或Rollup这类打包工具会将应用的源代码打包成一个或多个代码包。当用户访问网站时,浏览器会请求并加载这些代码包,以便将内容显示在用户屏幕上。
V8这类JavaScript引擎能够在加载用户请求的数据时,同步解析和编译这些数据。尽管现代浏览器已经进化到可以尽可能快速高效地解析和编译代码,但开发者仍需优化流程中的两个步骤:请求数据的加载时间和执行时间。我们需要确保执行时间尽可能短,以避免阻塞主线程。
When to Use
适用场景
- Use this when your application has a large JavaScript bundle that affects load times
- This is helpful when you want to reduce First Contentful Paint (FCP) and Largest Contentful Paint (LCP)
- Use this when parts of your code are only needed for specific user interactions or routes
- 当你的应用拥有大型JavaScript代码包,且该代码包影响加载时间时使用此方法
- 如果你希望缩短首次内容绘制(FCP)和最大内容绘制(LCP)时间,此方法会有所帮助
- 当代码的某些部分仅在特定用户交互或路由下才需要时使用此方法
Instructions
操作指南
- Use bundlers like Webpack or Rollup to split code into multiple smaller bundles
- Separate code that isn't needed for the initial render into its own bundle
- Consider the impact on Time To Interactive (TTI) and prioritize critical rendering code
- 使用Webpack或Rollup等打包工具将代码拆分为多个更小的包
- 将初始渲染不需要的代码分离到单独的包中
- 考虑对交互时间(TTI)的影响,优先处理关键渲染代码
Details
详细说明
Even though modern browsers are able to stream the bundle as it arrives, it can still take a significant time before the first pixel is painted on the user's device. The bigger the bundle, the longer it can take before the engine reaches the line on which the first rendering call has been made. Until that time, the user has to stare at a blank screen.
We want to display data to the user as quickly as possible. A larger bundle leads to an increased amount of loading time, processing time, and execution time. It would be great if we could reduce the size of this bundle, in order to speed things up.
Instead of requesting one giant bundle that contains unnecessary code, we can split the bundle into multiple smaller bundles!
By bundle-splitting the application, we can reduce the time it takes to load, process and execute a bundle! By reducing the loading and execution time, we can reduce the time it takes before the first content has been painted on the user's screen, the First Contentful Paint (FCP), and the time it takes before the largest component has been rendered to the screen, the Largest Contentful Paint (LCP).
Although being able to see data on our screen is great, we don't just want to see the content. In order to have a fully functioning application, we want users to be able to interact with it as well! The UI only becomes interactive after the bundle has been loaded and executed. The time it takes before all content has been painted to the screen and has been made interactive, is called the Time To Interactive (TTI).
A bigger bundle doesn't necessarily mean a longer execution time. It could happen that we loaded a ton of code that the user won't even use! Maybe some parts of the bundle will only get executed on a certain user interaction, which the user may or may not do!
The engine still has to load, parse and compile code that's not even used on the initial render before the user is able to see anything on their screen. Although the parsing and compilation costs can be practically ignored due to the browser's performant way of handling these two steps, fetching a larger bundle than necessary can hurt the performance of your application. Users on low-end devices or slower networks will see a significant increase in loading time before the bundle has been fetched.
Instead of initially requesting parts of the code that don't have a high priority in the current navigation, we can separate this code from the code that's needed in order to render the initial page.
By bundle-splitting the large bundle into two smaller bundles, and , we reduce the initial loading time by fetching a smaller amount of data.
main.bundle.jsemoji-picker.bundle.js尽管现代浏览器能够在代码包到达时流式加载,但在用户设备上绘制第一个像素之前,仍可能需要相当长的时间。代码包越大,引擎执行到首次渲染调用代码行的时间就越长。在这之前,用户只能盯着空白屏幕。
我们希望尽快向用户显示内容。更大的代码包会导致加载时间、处理时间和执行时间增加。如果我们能缩小代码包的大小,就能加快这些流程。
与其请求一个包含不必要代码的大型代码包,我们可以将其拆分为多个更小的代码包!
通过对应用进行代码包拆分,我们可以缩短代码包的加载、处理和执行时间!通过减少加载和执行时间,我们能够缩短首次内容绘制(FCP)时间——即用户屏幕上首次显示内容的时间,以及最大内容绘制(LCP)时间——即最大组件渲染到屏幕上的时间。
虽然能在屏幕上看到内容很棒,但我们不希望用户只能_查看_内容。为了让应用完全可用,我们希望用户能够与应用_交互_!只有在代码包加载并执行完成后,UI才会变为可交互状态。所有内容绘制完成并可交互所需的时间称为交互时间(TTI)。
更大的代码包并不一定意味着更长的执行时间。有可能我们加载了大量用户根本不会用到的代码!比如代码包中的某些部分只有在特定用户交互时才会执行,而用户可能进行也可能不进行这些交互!
在用户能够看到屏幕上的任何内容之前,引擎仍需加载、解析和编译初始渲染中根本不会用到的代码。尽管浏览器处理这两个步骤的方式非常高效,解析和编译的成本几乎可以忽略,但获取不必要的大型代码包仍会损害应用性能。使用低端设备或网络较慢的用户会发现代码包获取前的加载时间显著增加。
我们可以将当前导航中优先级不高的代码部分与初始页面渲染所需的代码分离,而不是在初始请求时就加载这些低优先级代码。
通过将大型代码包拆分为两个更小的包——和——我们通过获取更少的数据来缩短初始加载时间。
main.bundle.jsemoji-picker.bundle.js