Skip to content

Tree shaking not working as expected? #10163

@kfancy

Description

@kfancy

Expected 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions