Skip to content

Commit fcd2849

Browse files
committed
refactor: tree-shaking (#839)
BREAKING CHANGE: Added support of tree-shaking of Chart.js. Now you should register Chart.js components by yourself.
1 parent 131daa0 commit fcd2849

14 files changed

+60
-17
lines changed

.size-limit

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
{
99
"path": "dist/index.js",
1010
"limit": "1.5 KB",
11-
"import": "Chart"
11+
"import": "{ Chart }"
1212
},
1313
{
1414
"path": "dist/index.modern.js",
15-
"limit": "2.3 KB",
15+
"limit": "2.35 KB",
1616
"webpack": false,
1717
"running": false
1818
},
1919
{
2020
"path": "dist/index.modern.js",
2121
"limit": "1.5 KB",
22-
"import": "Chart"
22+
"import": "{ Chart }"
2323
}
2424
]

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "react-chartjs-2",
33
"version": "3.3.0",
44
"description": "React components for Chart.js",
5+
"sideEffects": false,
56
"main": "dist/index.js",
67
"module": "dist/index.modern.js",
78
"types": "dist/index.d.ts",

src/chart.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useRef, useState, forwardRef } from 'react';
22
import type { ForwardedRef, MouseEvent } from 'react';
3-
import ChartJS from 'chart.js/auto';
3+
import { Chart as ChartJS } from 'chart.js';
44
import type { ChartData, ChartType, DefaultDataPoint } from 'chart.js';
55

66
import type { ChartProps, TypedChartComponent } from './types';

src/typedCharts.tsx

+44-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
11
import React, { forwardRef } from 'react';
2-
import { ChartType } from 'chart.js';
3-
4-
import { ChartProps, ChartJSOrUndefined, TypedChartComponent } from './types';
2+
import {
3+
Chart as ChartJS,
4+
LineController,
5+
BarController,
6+
RadarController,
7+
DoughnutController,
8+
PolarAreaController,
9+
BubbleController,
10+
PieController,
11+
ScatterController,
12+
} from 'chart.js';
13+
import type { ChartType, ChartComponentLike } from 'chart.js';
14+
15+
import type {
16+
ChartProps,
17+
ChartJSOrUndefined,
18+
TypedChartComponent,
19+
} from './types';
520
import { Chart } from './chart';
621

7-
function createTypedChart<T extends ChartType>(type: T) {
22+
function createTypedChart<T extends ChartType>(
23+
type: T,
24+
registerables: ChartComponentLike
25+
) {
26+
ChartJS.register(registerables);
27+
828
return forwardRef<ChartJSOrUndefined<T>, Omit<ChartProps<T>, 'type'>>(
929
(props, ref) => <Chart {...props} ref={ref} type={type} />
1030
) as TypedChartComponent<T, true>;
1131
}
1232

13-
export const Line = createTypedChart('line');
33+
export const Line = /* #__PURE__ */ createTypedChart('line', LineController);
1434

15-
export const Bar = createTypedChart('bar');
35+
export const Bar = /* #__PURE__ */ createTypedChart('bar', BarController);
1636

17-
export const Radar = createTypedChart('radar');
37+
export const Radar = /* #__PURE__ */ createTypedChart('radar', RadarController);
1838

19-
export const Doughnut = createTypedChart('doughnut');
39+
export const Doughnut = /* #__PURE__ */ createTypedChart(
40+
'doughnut',
41+
DoughnutController
42+
);
2043

21-
export const PolarArea = createTypedChart('polarArea');
44+
export const PolarArea = /* #__PURE__ */ createTypedChart(
45+
'polarArea',
46+
PolarAreaController
47+
);
2248

23-
export const Bubble = createTypedChart('bubble');
49+
export const Bubble = /* #__PURE__ */ createTypedChart(
50+
'bubble',
51+
BubbleController
52+
);
2453

25-
export const Pie = createTypedChart('pie');
54+
export const Pie = /* #__PURE__ */ createTypedChart('pie', PieController);
2655

27-
export const Scatter = createTypedChart('scatter');
56+
export const Scatter = /* #__PURE__ */ createTypedChart(
57+
'scatter',
58+
ScatterController
59+
);

stories/Bar.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'chart.js/auto';
23
import { Bar } from '../src';
34
import * as verticalBar from '../sandboxes/bar/vertical/App';
45
import * as horizontalBar from '../sandboxes/bar/horizontal/App';

stories/Bubble.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'chart.js/auto';
23
import { Bubble } from '../src';
34
import { data, options } from '../sandboxes/bubble/default/App';
45

stories/Chart.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState, useEffect, useReducer } from 'react';
2+
import 'chart.js/auto';
23
import { InteractionItem } from 'chart.js';
34
import 'chartjs-adapter-date-fns';
45
import { Chart } from '../src';

stories/Doughnut.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState, useEffect } from 'react';
2+
import 'chart.js/auto';
23
import { Doughnut } from '../src';
34
import { data } from '../sandboxes/doughnut/default/App';
45

stories/Line.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'chart.js/auto';
23
import { Line } from '../src';
34
import * as defaultLine from '../sandboxes/line/default/App';
45
import * as multiaxisLine from '../sandboxes/line/multiaxis/App';

stories/Pie.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState } from 'react';
22
import faker from 'faker';
3+
import 'chart.js/auto';
34
import { Pie } from '../src';
45
import { data } from '../sandboxes/pie/default/App';
56

stories/PolarArea.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'chart.js/auto';
23
import { PolarArea } from '../src';
34
import { data } from '../sandboxes/polarArea/default/App';
45

stories/Radar.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'chart.js/auto';
23
import { Radar } from '../src';
34
import { data } from '../sandboxes/radar/default/App';
45

stories/Scatter.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'chart.js/auto';
23
import { Scatter } from '../src';
34
import { data, options } from '../sandboxes/scatter/default/App';
45

test/chart.test.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { render, cleanup, fireEvent } from '@testing-library/react';
3-
import ChartJS from 'chart.js/auto';
3+
import 'chart.js/auto';
4+
import { Chart as ChartJS } from 'chart.js';
45
import { Chart } from '../src';
56

67
describe('<Chart />', () => {

0 commit comments

Comments
 (0)