Skip to content

Commit 094fa63

Browse files
authored
fix: support React 16 and 17 under strict ES module resolution (#1378)
The package imported `react/jsx-runtime`, which Node.js and webpack 5 cannot resolve in React 16 and 17: they have no `exports` field, and React before 16.14 has no JSX runtime at all. The components create their elements with `createElement` now, and their sources are .ts files, so JSX cannot come back by accident.
1 parent c65365d commit 094fa63

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

src/chart.tsx renamed to src/chart.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
DefaultDataPoint
44
} from 'chart.js'
55
import {
6+
createElement,
67
useEffect,
78
useRef,
89
forwardRef
@@ -117,16 +118,18 @@ function ChartComponent<
117118
return () => destroyChart()
118119
}, [])
119120

120-
return (
121-
<canvas
122-
ref={canvasRef}
123-
role='img'
124-
height={height}
125-
width={width}
126-
{...canvasProps}
127-
>
128-
{fallbackContent}
129-
</canvas>
121+
// createElement instead of JSX: strict ES module resolution, as in Node.js and webpack 5,
122+
// cannot find `react/jsx-runtime` in React 16 and 17, which have no `exports` field (#1319)
123+
return createElement(
124+
'canvas',
125+
{
126+
ref: canvasRef,
127+
role: 'img',
128+
height,
129+
width,
130+
...canvasProps
131+
},
132+
fallbackContent
130133
)
131134
}
132135

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type {
22
ChartType,
33
ChartComponentLike
44
} from 'chart.js'
5-
import { forwardRef } from 'react'
5+
import {
6+
createElement,
7+
forwardRef
8+
} from 'react'
69
import {
710
Chart as ChartJS,
811
LineController,
@@ -28,13 +31,11 @@ function createTypedChart<T extends ChartType>(
2831
ChartJS.register(registerables)
2932

3033
return /* @__PURE__ */ forwardRef<ChartJSOrUndefined<T>, Omit<ChartProps<T>, 'type'>>(
31-
(props, ref) => (
32-
<Chart
33-
{...props}
34-
ref={ref}
35-
type={type}
36-
/>
37-
)
34+
(props, ref) => createElement(Chart<T>, {
35+
...props,
36+
ref,
37+
type
38+
})
3839
) as TypedChartComponent<T>
3940
}
4041

‎website/src/components/PropsTable.astro‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function getDocs() {
2323
// Standard <canvas> props are mentioned once on the page instead of being listed
2424
propFilter: prop => !prop.parent || !prop.parent.fileName.includes('node_modules')
2525
}).parse([
26-
resolve(root, 'src/chart.tsx'),
27-
resolve(root, 'src/typedCharts.tsx')
26+
resolve(root, 'src/chart.ts'),
27+
resolve(root, 'src/typedCharts.ts')
2828
])
2929
3030
return docs

0 commit comments

Comments
 (0)