alibaba / BizCharts

Powerful data visualization library based on G2 and React.
http://bizcharts.net/products/bizCharts
6.17k stars 671 forks source link

Reduce Bundle Size #1085

Open davidgolub opened 4 years ago

davidgolub commented 4 years ago

Hi, the Bizcharts bundle size is huge ( image)

How do you reduce it?

yoyo837 commented 4 years ago

need this.

dreamy-dev commented 4 years ago

same, I want to use it but it's way too large...

DudeHu commented 4 years ago

i also confused by this problem, too large

stefanfuchs commented 4 years ago

I tried the same technique as I used to reduce my lodash bundle size, and it worked. Tree shaking will only work when you import individual components from their respective files. Bizcharts only occupies 36Kb in my bundle now. I'm using bizcharts V4. This is what I have done:

Change:

import { Chart, Tooltip, Legend, Axis, Area, Line } from "bizcharts";

to:

import Chart from 'bizcharts/lib/components/Chart';
import Tooltip from 'bizcharts/lib/components/Tooltip';
import Legend from 'bizcharts/lib/components/Legend';
import Axis from 'bizcharts/lib/components/Axis';
import Area from 'bizcharts/lib/geometry/Area';
import Line from 'bizcharts/lib/geometry/Line';

If you need other components than I have shown in the example above, simply add them to the imports. Check the index.tsx file in the bizcharts source code to help you find the correct path for the imports.

Also, if you use the Geom or Geometry components, stop using them, because they will also import all the other components from bizcharts automatically. Use more specific ones instead, like Line or Point. Example:

Change:

<Geometry type="line" position={"item*value"} />
<Geometry type="point" position={"item*value"} />

to:

<Line position={"item*value"} />
<Point position={"item*value"} />
sunkehappy commented 4 years ago

@stefanfuchs Your method is much complicated. We can't we just use import { Chart } from "bizcharts"; like Ant Design

stefanfuchs commented 4 years ago

@sunkehappy Well, just look here: https://github.com/alibaba/BizCharts/blob/master/src/index.tsx When you import bizcharts like this: import { Chart } from "bizcharts". All those files inside index.tsx are imported as well. That's why the bundle size gets so large.

I agree that importing like this would be much easier, but it would require some optimization in the bizcharts code itself to reduce the bundle size. Unfortunatelly I don't have any experience in doing this sort of optimization.

Any sugestions would be appreciated. If we knew a solution, maybe we could do a PR to fix this.

sunkehappy commented 4 years ago

@stefanfuchs Sounds interesting. I will take a deep look at this and try to fix it if possible.

sunkehappy commented 3 years ago

@stefanfuchs We can simply enable the tree shaking by importing files from the es directory. We can do this by importing files from bizcharts/es instead of simply bizcharts. Or by change the resolve strategy of webpack by adding the resolve: { mainFields: ["module", "main"] } in webpack.config.js file.

stefanfuchs commented 3 years ago

@sunkehappy interesting. So importing like this: import { Chart } from "bizcharts/es" should work? Have you tested to see if the bundle size gets reduced this way?

sunkehappy commented 3 years ago

@stefanfuchs Now I see that although size gets reduced. But feature is not working. I'm curious why. I need more research.

stefanfuchs commented 3 years ago

Maybe we could see how antd did this, because in v4 they did some optimizations regarding icons. All the icons were being imported in the bundle, even when you just wanted to use one icon.