Closed msbasanth closed 3 years ago
Having this same problem on Angular 12. Copy-pasted the documentation, and looking at the webpack analysis shows every echarts module is included.
@chris-wahl
Yes it looks like when we add theme (downloaded from theme builder) we have echarts included inside that. Here is the comment from echarts https://github.com/apache/echarts/issues/15138
I will keep you updated with the package size.
I tried the suggestion from echarts contributor and looks like tree shaking is working in echarts. 👍 https://github.com/apache/echarts/issues/15138
But the same approach of registering the theme ourselves by passing json (basically avoiding require('echarts')) didn't work here with ngx-echarts.
This is what I tried, instead of importing style directly I registered theme manually.
import * as echarts from 'echarts/core';
echarts.registerTheme('dark', { <<Entire theme as json>>});
Even with this network transfer shows same size 1.2MB for the application.
@msbasanth
Sorry for the late reply and thanks for your issue report.
I double checked the codes in https://github.com/xieziyu/ngx-echarts-starter and found the duplicated echarts/theme/macarons.js
importing in main.ts
. If we remove all the codes importing the theme files directly, the tree shaking should be working as expected.
@xieziyu Thanks for looking into this.
@xieziyu from this issue I didn't really get how to tree shake echarts in Angular. Could you provide some examples and/or explanations other than in linked issues?
This issue still seems to be prevailing, any update on this?
@Alphapredator01
Here's the solution I used so that I got both tree-shaking and lazy loading of modules:
charts.ts
:import * as echarts from 'echarts/core';
import {DataZoomComponent, GridComponent, LegendComponent, MarkLineComponent, TitleComponent, ToolboxComponent, TooltipComponent} from 'echarts/components';
import {BarChart, LineChart, SankeyChart} from 'echarts/charts';
import {CanvasRenderer} from 'echarts/renderers';
echarts.use([
BarChart, LineChart, SankeyChart,
DataZoomComponent, GridComponent, LegendComponent, MarkLineComponent, TitleComponent, ToolboxComponent, TooltipComponent,
CanvasRenderer,
]);
export default echarts;
app.module.ts
:@NgModule({
declarations: [AppComponent],
imports: [
NgxEchartsModule.forRoot({
echarts: () => import(
/* webpackChunkName: "charts" */
/* webpackMode: "lazy" */
'./path/to/charts').then(m => m.default)
}),
.
.
.
bootstrap: [AppComponent]
})
export class AppModule {
}
With my current imports, this gets my gzip'd webpack analysis down to ~204 KB. That used to be closer to 1 MB:
For testing, I had to make a separate test module file so that it could be imported in unit tests. Created charts-testing.module.spec.ts
right next to my charts.ts
in step 1:
import echarts from './charts';
import {NgxEchartsModule} from 'ngx-echarts';
/** Simple export to simplify charts import when testing, but allow AppModule to continue to use lazy-loading process */
export const ChartsTestingModuleSpec = NgxEchartsModule.forRoot({echarts});
and then import it like I would any other module in a unittest:
beforeEach( async () => {
await TestBed.configureTestingModule({
imports: [
ChartsTestingModuleSpec,
NoopAnimationsModule,
.
.
.
],
});
.
.
.
});
How would this look for a standalone component?
Hi,
We compared using ngx-echarts with full echarts against echarts tree shakable API's. For experimenting we used the demo sample given @https://github.com/xieziyu/ngx-echarts-starter
Option 1: Full ECharts imported
Our observation is this:
Total Main.js Size: 1.1 MB; Total Network transfer: 1.2MB
Option 2: Using tree shakable API's from ECharts
Our observation is, Total Main.js Size: 1.2 MB; Total Network transfer: 1.2MB
So as you could see total size of main.js increased with tree shakable API usage. What could be the reason for the increase in package size after using tree shakable API?
Please let us know in case the approach we followed is not correct.
Thanks Basanth