You can apply tree-shaking for Chart.js when you finish development and ready to push your project to production. Tree-shaking is a term for removing unused code from the JavaScript bundle.
When we build the project without tree-shaking, which implies that we imported all available components from "chart.js/auto"
, we receive the following bundle result:
To achieve tree-shaking is simple. We only need to replace import Chart from "chart.js/auto";
with the following method of importing only the necessary components to create our chart.
And the result shows our bundle size is reduced.
Tip
If you don’t know which components you need for creating your chart, you can open the browser console and learn from the warning message, such as:
Unhandled Promise Rejection: Error: "bar" is not a registered controller.
References