简体   繁体   English

减小React / MERN堆栈的大小 - 主要是删除重复的D3

[英]Reducing size of React / MERN Stack Bundle - mainly removing duplicative D3

When I run npm run-script build to bundle my React app, once the bundle is complete the following webpack bundle analyzer launches which shows what my app's bundle is comprised of: 当我运行npm run-script build来捆绑我的React应用程序时,一旦捆绑完成,下面的webpack捆绑分析器将启动,显示我的应用程序包的组成:

在此输入图像描述

Although I am not positive of it, this seems like a fairly large build, which may be slowing down my app. 虽然我不是肯定的,但这似乎是一个相当大的构建,这可能会减慢我的应用程序。

It appears that d3 is one of the largest parts of my bundle, and it also appears that d3 is being bundled twice - once on its own, and once bundled with d3-tip. 似乎d3是我的捆绑中最大的部分之一,并且似乎d3被捆绑两次 - 一次独立,一次捆绑d3-tip。 (My React app is a graphing / d3 intensive application, and I use several d3 modules throughout the app. I use d3-tip as my tooltip library for my graphs). (我的React应用程序是一个图形/ d3密集型应用程序,我在整个应用程序中使用了几个d3模块。我使用d3-tip作为我的图形工具提示库)。 How can i fix this so that d3 is only bundled once? 我怎么能解决这个问题,以便d3只捆绑一次? And also, if it would help the speed of my app significantly, how can i bundle just the d3 modules that i use, rather than all of d3? 而且,如果它有助于我的应用程序的速度显着,我怎么能捆绑我使用的d3模块 ,而不是所有的d3?

My index.js seems pretty large as well on the right, and I have no idea if that is ordinary or not. 我的index.js在右边看起来也很大,我不知道这是不是普通的。 In fact, I have very little idea as to what is considered a large/bulky build, vs. a small/compact build. 事实上,对于什么被认为是大型/大型构建而不是小型/紧凑型构建,我几乎一无所知。 I do know that the size of my build folder is ~53MBs. 我知道我的构建文件夹的大小是〜53MBs。

.

----EDIT ----- Updating the post before bounty . ----编辑-----在赏金之前更新帖子 I removed d3-tip, and switched over to using ordinary divs with hover as tooltips. 我删除了d3-tip,并切换到使用普通div并将鼠标悬停为工具提示。 Here's the latest build: 这是最新版本:

在此输入图像描述

... my main question for the bounty is then: ...那时我对赏金的主要问题是:

1: How can i bundle only the modules in d3, react-spinners, etc. that are actually used in my app? 1:我如何只捆绑我的应用程序中实际使用的d3,react-spinners等模块? I've heard about tree-shaking, but could not find a good source on how to tree-shake d3 in the app. 我听说过树震动,但是找不到如何在应用程序中摇晃d3的好消息来源。 My components that use d3 generally have the following in the top few lines: 我使用d3的组件通常在前几行中有以下内容:

import React, { Component } from 'react';
import * as d3 from 'd3';
import * as d3Hexbin from 'd3-hexbin';

class SomeClassHere extends Component { ...

...and I assume I'd have to no longer import d3 in this manner, but I'm not sure if simply changing all of the imports will change the bundle as well, or if I have to do something else to remove the modules that aren't used. ...我假设我不得不以这种方式导入d3,但我不确定是否只是更改所有导入也会改变捆绑,或者如果我必须做其他事情来删除未使用的模块。

2: Is there a smaller pdf-generation library to use, rather than kendo-react-pdf? 2:是否有较小的pdf-generation库可供使用,而不是kendo-react-pdf? And likewise is there something smaller than react-datepicker? 同样,还有比react-datepicker更小的东西吗? I only have a single datepicker in my app, and the entire react-datepicker library seems like overkill? 我的应用程序中只有一个日期选择器,整个react-datepicker库看起来react-datepicker矫枉过正?

3: Lastly, what is a size for a bundle that i should strive for? 3:最后,我应该努力争取的捆绑规模是多少? The screenshot of the bundle show the stat size, parsed size, and gzipped size for most of the app (excluding just the blue chunks which is the code ive written in /src i think?). 捆绑包的屏幕截图显示了大多数应用程序的统计大小,解析大小和gzip压缩大小(不包括蓝色块,这是我认为/ src中编写的代码?)。 Is this big? 这个大吗?

Thanks in advance for your help with this!! 在此先感谢您的帮助!!

Bounty isn't gaining traction, but I'd like to update to share with my efforts and learned knowledge over the last couple of days: 赏金没有获得牵引力,但我想更新以分享我过去几天的努力和学到的知识

Per this blocks link , using rollup is a way to make custom bundles of a library like d3. 根据此块链接 ,使用汇总是一种方法来制作像d3这样的库的自定义包。 I haven't used it yet, but I will review the rollup docs and try to make a smaller d3 bundle (which may be challenging as my app uses dozens of d3 modules) using this. 我还没有使用它,但是我会检查汇总文档并尝试使用这个来制作更小的d3软件包(这可能很有挑战性,因为我的应用程序使用了几十个d3模块)。 I'm optimistic that it will work. 我很乐观它会奏效。

For react-spinners, I will simply remove this library and make my own spinner in react (simply grab an icon and animate it). 对于反应旋转器,我将简单地删除这个库并使我自己的微调器做出反应(只需抓取一个图标并为其设置动画)。 Removes a large library from bundle, and it's not that hard to create with react without a library. 从bundle中删除一个大型库,如果没有库,使用react进行创建并不困难。

I will also probably create my own datepicker in react, rather than use react-datepicker. 我也可能会在react中创建自己的datepicker,而不是使用react-datepicker。 This removes 2 large dependencies that aren't really needed. 这将删除2个不需要的大型依赖项。 In particular with react-datepicker, this is mostly 1 large module, so no custom module creator will make this library smaller. 特别是使用react-datepicker,这主要是一个大模块,因此没有自定义模块创建者会使这个库变小。 I will probably follow this tutorial as it looks legit. 我可能会遵循这个教程,因为它看起来合法。

kendo-react-pdf is pretty huge, but my application needs a "download-to-pdf" functionality for generating 1-page pdf reports. kendo-react-pdf非常庞大,但我的应用程序需要“下载到pdf”功能来生成1页的pdf报告。 In general - any advice on pdf-generation in react (links to guides, tutorial, libraries, etc.) would be helpful. 一般而言 - 任何反应中的pdf生成建议(指南,教程,图书馆等链接)都会有所帮助。 What is the latest library and/or approach to building download-to-pdf capabilities in react? 什么是构建下载到pdf功能的最新库和/或方法?

To recap - removing libraries, and recreating them in react, is straightforward seems simpler than trying to "tree-shake" every library my app uses. 回顾一下 - 删除库,并在反应中重新创建它们,直接看起来比试图“震动”我的应用程序使用的每个库更简单。 d3 in the exception, and I will try to build a custom bundle that uses only the d3 modules that my app requires. d3在异常中,我将尝试构建一个仅使用我的应用程序所需的d3模块的自定义捆绑包。

EDIT: I am on a trajectory where, in the bundle hierarchy graph in the post, the yellow part of my bundle will be smaller than the blue part of my bundle. 编辑:我在一个轨道上,在帖子的包层次结构图中,我的包的黄色部分将小于我的包的蓝色部分。 At that point, presumably the only way to make the bundle smaller is to reduce my own code? 那时,大概是使捆绑更小的唯一方法是减少我自己的代码? My app is probably 20K - 30K lines of code across all of the components (this is the blue in the bundle hierarchy graph I believe) and container components (is that a lot?), but if i ever were to take the time to refactor things, I could reduce this by probably 30% - 50% 我的应用程序可能是所有组件的20K - 30K代码行(这是我认为的捆绑层次结构图中的蓝色)和容器组件(很多都是?),但是如果我曾经花时间重构事情,我可以减少大约30% - 50%

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

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