简体   繁体   English

减少生产应用程序的包大小

[英]Reducing bundle size of a production application

I got a production application where its bundle size is 8.06MB . 我有一个生产应用程序,其捆绑包大小为8.06MB

# Console log from npm build
File sizes after gzip:

  1.67 MB    build/static/js/3.73cf59a2.chunk.js
  794.29 KB  build/typescript.worker.js
  131.13 KB  build/css.worker.js
  104.68 KB  build/html.worker.js
  104.02 KB  build/static/css/3.01bcafd3.chunk.css
  67.03 KB   build/static/js/main.6acf560d.chunk.js
  49.64 KB   build/json.worker.js
  25.12 KB   build/editor.worker.js
  7.99 KB    build/static/js/54.afc981d1.chunk.js
  ...

On building the application and running source-map-explorer 构建应用程序并运行source-map-explorer

npm run build
source-map-explorer 'build/static/js/*.js'

I get a warning from build : 我从build收到警告:

The bundle size is significantly larger than recommended. 捆绑大小明显大于建议。

我的源地图

You can inspect the source map . 您可以检查源地图

I would like to reduce the bundle size , from the research I've done, it concluded to: 我想减少捆绑尺寸 ,从我所做的研究中得出结论:

  • Finding alternatives for big libraries. 寻找大型图书馆的替代方案。
    • Making a "slim" version of the library for my needs. 根据我的需求制作一个“苗条”的图书馆版本。
  • Import on-demand individual components: 导入按需个别组件:
import Button from 'antd/es/button'; 
import { Button } from 'antd'; // Imports all library

I have some question regarding: 我有一些问题:

  1. Why is it important to have a small bundle size? 为什么小捆绑包很重要?
  2. What is the recommended bundle size, why? 什么是推荐的捆绑尺寸,为什么?
  3. I understand that code-splitting splits your code into various bundles which can then be loaded on-demand or in parallel. 我知道代码拆分将您的代码拆分为各种捆绑包,然后可以按需或并行加载。 How does it help in reducing the bundle size? 它如何帮助减少捆绑大小?
  4. How do I decide if a library needs to be a part of devDependencies 如何确定库是否需要成为devDependencies的一部分
  5. Are there any other approaches? 还有其他方法吗?

Why is it important to have a small bundle size? 为什么小捆绑包很重要?

Because this will reduce the amount of time it takes for users to load your application / site, by reducing the amount of data they have to transfer. 因为这会减少用户加载应用程序/站点所需的时间,减少他们必须传输的数据量。 This is particularly important for users on low bandwidth connections, including patchy cellular connections. 这对于低带宽连接的用户尤其重要,包括不完整的蜂窝连接。

What is the recommended bundle size, why? 什么是推荐的捆绑尺寸,为什么?

As small as possible. 尽可能小。 I don't think that it's really possible to give a precise answer here, as every application is different, but generally you want the resources on the "critical" path to be as small as possible, so that you decrease your initial load time, and then load more resources as needed on the fly. 我不认为在这里给出一个确切的答案,因为每个应用程序都不同,但通常你希望“关键”路径上的资源尽可能小,这样你就可以减少初始加载时间,然后根据需要动态加载更多资源。

I understand that code-splitting splits your code into various bundles which can then be loaded on-demand or in parallel. 我知道代码拆分将您的代码拆分为各种捆绑包,然后可以按需或并行加载。 How does it help in reducing the bundle size? 它如何帮助减少捆绑大小?

Whilst it may not reduce your overall bundle size, it can reduce the amount of data required by an individual user. 虽然它可能不会减少您的整体捆绑包大小,但它可以减少单个用户所需的数据量。 For example, if you only use monaco-editor in a particular part of your application, it may make sense to lazily load this only when the user activates that feature. 例如,如果您只在应用程序的特定部分使用monaco-editor ,则只有在用户激活该功能时才可以延迟加载。

How do I decide if a library needs to be a part of devDependencies 如何确定库是否需要成为devDependencies的一部分

devDependencies should include any dependencies that are only required whilst developing the project. devDependencies应该包括开发项目时只需要的任何依赖项。 This would include tooling (eg: webpack, eslint, gulp) and test frameworks (mocha, chai, sinon). 这将包括工具(例如:webpack,eslint,gulp)和测试框架(mocha,chai,sinon)。

This is more relevant to server side projects however, as true devDependencies should not end up in your bundled code even if you place them in the dependencies section by mistake. 这与服务器端项目更相关,因为真正的devDependencies不应该在捆绑代码中结束,即使您错误地将它们放在依赖项部分中也是如此。

Are there any other approaches? 还有其他方法吗?

Primarily you should focus on tree shaking and code splitting/lazy loading. 主要是你应该专注于树摇动和代码分裂/延迟加载。

For example, moment-timezone is taking up almost 1mb by itself - do you actually need this? 例如, moment-timezone自身占据近1mb - 你真的需要这个吗? For many applications it is enough to just work in the browsers time zone and utc, which shouldn't need moment-timezone 对于许多应用程序来说,仅在浏览器时区和utc中工作就足够了,这应该不需要moment-timezone

For the antd library, making sure all your imports are tree-shakable (eg: import individual components as in your example) may significantly reduce the size of the imported code (other libraries like lodash may be a similar story, though if you have dependencies that haven't used tree-shakable imports the whole thing will be included anyway) 对于antd库,确保所有导入都是树可动的(例如:导入单个组件,如示例所示)可能会显着减小导入代码的大小(其他库如lodash可能是类似的故事,但如果你有依赖没有使用树可以进口的东西,无论如何都会包括整个东西)

For vis and monaco-editor consider if they can be loaded on demand, rather than always loaded at startup. 对于vismonaco-editor考虑是否可以按需加载,而不是始终在启动时加载。 Consider if you need codemirror as well as monaco-editor - from a glance they appear to fill similar purposes. 考虑一下你是否需要codemirror以及monaco-editor - 从一目了然它们似乎可以填充类似的用途。

ref: https://webpack.js.org/guides/tree-shaking/ ref: https//webpack.js.org/guides/tree-shaking/
https://webpack.js.org/guides/lazy-loading/ https://webpack.js.org/guides/lazy-loading/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM