Tree shaking not working as expected?
See original GitHub issueExpected behavior
Using named imports to target what components are imported, the bundle size created with webpack by the host should be smaller than including everything in ChartJS, as described in the integration docs.
https://www.chartjs.org/docs/latest/getting-started/integration.html
Current behavior
Bundle size is the same for both the ākitchen sinkā import and the named imports version.
Not really a reproducible issue that is code-based, itās for bundling/minifying for production useā¦ please note this is why the required reproducible sample is just the react-chartjs-2
template.
Reproducible sample
https://codesandbox.io/s/react-chartjs-2-chart-js-issue-template-cg7b5?file=/src/App.tsx
Optional extra steps/info to reproduce
Using this in a react app, where the targeted component is lazy-loaded to try and isolate the ChartJS code into its own bundle. Both of these import strategies result in the same 97.5kb minified dynamic import bundle.
Strategy 1: named imports
import {
CategoryScale,
Chart,
Legend,
LinearScale,
LineController,
BarController,
LineElement,
PointElement,
Tooltip,
BarElement
} from 'chart.js';
Chart.register(
LineController,
PointElement,
Legend,
Tooltip,
CategoryScale,
LinearScale,
LineElement,
BarController,
BarElement
);
Strategy 2: ākitchen sinkā registrables import:
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);
Lazy-loading technique (for reference):
const ChartJsTreeShakeTester = React.lazy(() => import('./ChartJsTreeShakeTester'));
//// elsewhere in the code, used with Suspense as directed:
<Suspense fallback={<div>Loading chart...</div>}>
<ChartJsTreeShakeTester />
</Suspense>
Possible solution
No response
Context
We are trying to create small production bundles using chartJS, and only use a subset of the components. This affects us by forcing the bundle to contain code weāre not using, and affects our customers with longer download times.
chart.js version
3.7.1
Browser name and version
n/a
Link to your project
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:10 (2 by maintainers)
Top GitHub Comments
Hopefully someone can fix this š„¹. The bundles size include 67kbs gzip š„². Thanks
When I test this I do see a minimal change between importing the minimal things to show a bar chart or importing everything.
What I did was use
create-react-app
to create a new react app. Only show a canvas with the bar chart. Then I build the app and check the size of the build folder. When using the auto or ā¦registerables way of importing/registering everything I noticed they both had (almost) the same size, When I only imported the basics for a bar chart I noticed it was a bit smaller (around 6 mb and 40kb on disk)So it seems to me the treeshaking is working, only thing I can think off why it might seem that it doesnt work is that under the hood a lot of things depend on the same base classes. So the dataset controllers for example all use the core dataset controller which is a pretty big part. So the small parts dont matter anymore then.
This can bee seen when I add the legend and tooltip to the imports.