Skip to content

Commit

Permalink
refactor: tree-shaking (#839)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Added support of tree-shaking of Chart.js. Now you should register Chart.js
components by yourself.
  • Loading branch information
dangreen committed Nov 3, 2021
1 parent 131daa0 commit fcd2849
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .size-limit
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
{
"path": "dist/index.js",
"limit": "1.5 KB",
"import": "Chart"
"import": "{ Chart }"
},
{
"path": "dist/index.modern.js",
"limit": "2.3 KB",
"limit": "2.35 KB",
"webpack": false,
"running": false
},
{
"path": "dist/index.modern.js",
"limit": "1.5 KB",
"import": "Chart"
"import": "{ Chart }"
}
]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "react-chartjs-2",
"version": "3.3.0",
"description": "React components for Chart.js",
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.modern.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/chart.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useRef, useState, forwardRef } from 'react';
import type { ForwardedRef, MouseEvent } from 'react';
import ChartJS from 'chart.js/auto';
import { Chart as ChartJS } from 'chart.js';
import type { ChartData, ChartType, DefaultDataPoint } from 'chart.js';

import type { ChartProps, TypedChartComponent } from './types';
Expand Down
56 changes: 44 additions & 12 deletions src/typedCharts.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
import React, { forwardRef } from 'react';
import { ChartType } from 'chart.js';

import { ChartProps, ChartJSOrUndefined, TypedChartComponent } from './types';
import {
Chart as ChartJS,
LineController,
BarController,
RadarController,
DoughnutController,
PolarAreaController,
BubbleController,
PieController,
ScatterController,
} from 'chart.js';
import type { ChartType, ChartComponentLike } from 'chart.js';

import type {
ChartProps,
ChartJSOrUndefined,
TypedChartComponent,
} from './types';
import { Chart } from './chart';

function createTypedChart<T extends ChartType>(type: T) {
function createTypedChart<T extends ChartType>(
type: T,
registerables: ChartComponentLike
) {
ChartJS.register(registerables);

return forwardRef<ChartJSOrUndefined<T>, Omit<ChartProps<T>, 'type'>>(
(props, ref) => <Chart {...props} ref={ref} type={type} />
) as TypedChartComponent<T, true>;
}

export const Line = createTypedChart('line');
export const Line = /* #__PURE__ */ createTypedChart('line', LineController);

export const Bar = createTypedChart('bar');
export const Bar = /* #__PURE__ */ createTypedChart('bar', BarController);

export const Radar = createTypedChart('radar');
export const Radar = /* #__PURE__ */ createTypedChart('radar', RadarController);

export const Doughnut = createTypedChart('doughnut');
export const Doughnut = /* #__PURE__ */ createTypedChart(
'doughnut',
DoughnutController
);

export const PolarArea = createTypedChart('polarArea');
export const PolarArea = /* #__PURE__ */ createTypedChart(
'polarArea',
PolarAreaController
);

export const Bubble = createTypedChart('bubble');
export const Bubble = /* #__PURE__ */ createTypedChart(
'bubble',
BubbleController
);

export const Pie = createTypedChart('pie');
export const Pie = /* #__PURE__ */ createTypedChart('pie', PieController);

export const Scatter = createTypedChart('scatter');
export const Scatter = /* #__PURE__ */ createTypedChart(
'scatter',
ScatterController
);
1 change: 1 addition & 0 deletions stories/Bar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import 'chart.js/auto';
import { Bar } from '../src';
import * as verticalBar from '../sandboxes/bar/vertical/App';
import * as horizontalBar from '../sandboxes/bar/horizontal/App';
Expand Down
1 change: 1 addition & 0 deletions stories/Bubble.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import 'chart.js/auto';
import { Bubble } from '../src';
import { data, options } from '../sandboxes/bubble/default/App';

Expand Down
1 change: 1 addition & 0 deletions stories/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState, useEffect, useReducer } from 'react';
import 'chart.js/auto';
import { InteractionItem } from 'chart.js';
import 'chartjs-adapter-date-fns';
import { Chart } from '../src';
Expand Down
1 change: 1 addition & 0 deletions stories/Doughnut.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState, useEffect } from 'react';
import 'chart.js/auto';
import { Doughnut } from '../src';
import { data } from '../sandboxes/doughnut/default/App';

Expand Down
1 change: 1 addition & 0 deletions stories/Line.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import 'chart.js/auto';
import { Line } from '../src';
import * as defaultLine from '../sandboxes/line/default/App';
import * as multiaxisLine from '../sandboxes/line/multiaxis/App';
Expand Down
1 change: 1 addition & 0 deletions stories/Pie.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import faker from 'faker';
import 'chart.js/auto';
import { Pie } from '../src';
import { data } from '../sandboxes/pie/default/App';

Expand Down
1 change: 1 addition & 0 deletions stories/PolarArea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import 'chart.js/auto';
import { PolarArea } from '../src';
import { data } from '../sandboxes/polarArea/default/App';

Expand Down
1 change: 1 addition & 0 deletions stories/Radar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import 'chart.js/auto';
import { Radar } from '../src';
import { data } from '../sandboxes/radar/default/App';

Expand Down
1 change: 1 addition & 0 deletions stories/Scatter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import 'chart.js/auto';
import { Scatter } from '../src';
import { data, options } from '../sandboxes/scatter/default/App';

Expand Down
3 changes: 2 additions & 1 deletion test/chart.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { render, cleanup, fireEvent } from '@testing-library/react';
import ChartJS from 'chart.js/auto';
import 'chart.js/auto';
import { Chart as ChartJS } from 'chart.js';
import { Chart } from '../src';

describe('<Chart />', () => {
Expand Down

0 comments on commit fcd2849

Please sign in to comment.