Skip to content

Commit a40e872

Browse files
fix(signature-canvas): add universal import handling for trim-canvas to support CJS and ESM builds
1 parent 1998063 commit a40e872

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"@rollup/plugin-commonjs": "^21.1.0",
9898
"@rollup/plugin-node-resolve": "^13.2.1",
9999
"@testing-library/react": "^16.1.0",
100+
"@types/babel__core": "^7.20.5",
100101
"@types/prop-types": "^15.7.3",
101102
"@types/react": "^19.0.2",
102103
"canvas": "^3.1.0",
@@ -114,6 +115,7 @@
114115
"rollup-plugin-typescript2": "^0.32.1",
115116
"ts-node": "^10.7.0",
116117
"ts-standard": "^11.0.0",
118+
"tslib": "^2.8.1",
117119
"typescript": "^4.6.3",
118120
"window-resizeto": "^0.0.2"
119121
},

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import PropTypes from 'prop-types'
22
import React, { Component } from 'react'
33
import SignaturePad from 'signature_pad'
4-
import * as TrimCanvasModule from 'trim-canvas'
5-
6-
const trimCanvas: (canvas: HTMLCanvasElement) => HTMLCanvasElement =
7-
(TrimCanvasModule as any).default || (TrimCanvasModule as any)
4+
import * as trimCanvasImport from "trim-canvas";
85

96
export interface SignatureCanvasProps extends SignaturePad.SignaturePadOptions {
107
canvasProps?: React.CanvasHTMLAttributes<HTMLCanvasElement>
@@ -79,17 +76,20 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
7976
}
8077

8178
// return a trimmed copy of the canvas
82-
getTrimmedCanvas = (): HTMLCanvasElement => {
79+
getTrimmedCanvas = (): HTMLCanvasElement => {
8380
// copy the canvas
84-
const canvas = this.getCanvas()
85-
const copy = document.createElement('canvas')
86-
copy.width = canvas.width
87-
copy.height = canvas.height
81+
const canvas = this.getCanvas();
82+
const copy = document.createElement("canvas");
83+
copy.width = canvas.width;
84+
copy.height = canvas.height;
8885
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
89-
copy.getContext('2d')!.drawImage(canvas, 0, 0)
86+
copy.getContext("2d")!.drawImage(canvas, 0, 0);
9087
// then trim it
91-
return trimCanvas(copy)
92-
}
88+
// Handle both CJS and ESM default export forms
89+
const trimFn = (trimCanvasImport as any).default || trimCanvasImport;
90+
91+
return typeof trimFn === "function" ? trimFn(copy) : copy;
92+
};
9393

9494
// return the internal SignaturePad reference
9595
getSignaturePad = (): SignaturePad => {

tsconfig.build.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
// allowlist of files to build
55
"files": ["src/index.tsx", "types/trim-canvas.d.ts"],
66
"compilerOptions": {
7+
"importHelpers": true,
8+
"typeRoots": ["./typings", "./node_modules/@types"],
9+
"target": "ES2018",
10+
"lib": ["DOM", "ES2018"],
11+
"module": "ESNext",
12+
"declaration": true,
13+
"outDir": "dist",
14+
"strict": true,
15+
"esModuleInterop": true,
16+
"skipLibCheck": true,
717
// override the base
818
"noEmit": false,
919
// don't output JS files, only declarations (Rollup outputs the JS)

typings/babel-core.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module '@babel/core';

0 commit comments

Comments
 (0)