Skip to content

Commit b95a6dc

Browse files
committed
refactor: convert reconciler package to TS
1 parent 63a504d commit b95a6dc

10 files changed

+71
-5
lines changed

.changeset/gold-fishes-pretend.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-pdf/reconciler": patch
3+
---
4+
5+
refactor: convert reconciler package to TS

packages/reconciler/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"homepage": "https://github.com/diegomura/react-pdf#readme",
88
"type": "module",
99
"main": "./lib/index.js",
10+
"types": "./lib/index.d.ts",
1011
"repository": {
1112
"type": "git",
1213
"url": "https://github.com/diegomura/react-pdf.git",

packages/reconciler/rollup.config.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
import resolve from '@rollup/plugin-node-resolve';
22
import commonjs from '@rollup/plugin-commonjs';
33
import terser from '@rollup/plugin-terser';
4+
import typescript from '@rollup/plugin-typescript';
5+
import { dts } from 'rollup-plugin-dts';
6+
import del from 'rollup-plugin-delete';
47

58
import trimReconciler from './build/trim-reconciler.js';
69

710
export default [
811
{
9-
input: 'src/index.js',
12+
input: 'src/index.ts',
1013
output: { format: 'es', file: 'lib/index.js' },
1114
external: ['./reconciler-23.js', './reconciler-31.js'],
15+
plugins: [typescript()],
1216
},
1317
{
14-
input: 'src/reconciler-23.js',
18+
input: 'src/reconciler-23.ts',
1519
output: { format: 'es', file: 'lib/reconciler-23.js' },
1620
plugins: [
21+
typescript(),
1722
resolve({ resolveOnly: ['react-reconciler-23'] }),
1823
commonjs({ esmExternals: (id) => id === 'scheduler' }),
1924
trimReconciler(),
2025
terser({ compress: { dead_code: true } }),
2126
],
2227
},
2328
{
24-
input: 'src/reconciler-31.js',
29+
input: 'src/reconciler-31.ts',
2530
output: { format: 'es', file: 'lib/reconciler-31.js' },
2631
plugins: [
32+
typescript(),
2733
resolve({ resolveOnly: ['react-reconciler-31'] }),
2834
commonjs({ esmExternals: (id) => id === 'scheduler' }),
2935
trimReconciler(),
3036
terser({ compress: { dead_code: true } }),
3137
],
3238
},
39+
{
40+
input: './lib/types/index.d.ts',
41+
output: [{ file: 'lib/index.d.ts', format: 'es' }],
42+
plugins: [dts(), del({ targets: 'lib/types', hook: 'buildEnd' })],
43+
},
3344
];
File renamed without changes.
File renamed without changes.

packages/reconciler/src/reconciler-23.js packages/reconciler/src/reconciler-23.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Reconciler from 'react-reconciler-23/cjs/react-reconciler.production.min.js';
22

33
import propsEqual from './propsEqual';
4+
import { ReconcilerFactory } from './types';
45

56
const emptyObject = {};
67

7-
const createRenderer = ({
8+
const createRenderer: ReconcilerFactory = ({
89
appendChild,
910
appendChildToContainer,
1011
commitTextUpdate,

packages/reconciler/src/reconciler-31.js packages/reconciler/src/reconciler-31.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import {
55
} from 'react-reconciler-31/constants';
66

77
import propsEqual from './propsEqual';
8+
import { ReconcilerFactory } from './types';
89

910
const emptyObject = {};
1011

1112
const logRecoverableError = console.error;
1213

13-
const createRenderer = ({
14+
const createRenderer: ReconcilerFactory = ({
1415
appendChild,
1516
appendChildToContainer,
1617
commitTextUpdate,

packages/reconciler/src/types.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export interface Reconciler<T> {
2+
createContainer: (container: any) => T;
3+
updateContainer: (
4+
element: T,
5+
container: T,
6+
parentComponent: T,
7+
callback?: () => void,
8+
) => void;
9+
}
10+
11+
export type ReconcilerFactory = <I, T>(config: {
12+
appendChild: (parent: I, child: I | T) => void;
13+
appendChildToContainer: (parent: I, child: I | T) => void;
14+
commitTextUpdate: (textInstance: T, oldText: string, newText: string) => void;
15+
commitUpdate: (
16+
instance: I,
17+
updatePayload: any,
18+
type: string,
19+
oldProps: any,
20+
newProps: any,
21+
) => void;
22+
createInstance: (type: string, props: any) => I;
23+
createTextInstance: (text: string) => T;
24+
insertBefore: (parent: I, child: I | T, beforeChild: I | T) => void;
25+
removeChild: (parent: I, child: I | T) => void;
26+
removeChildFromContainer: (parent: I, child: I | T) => void;
27+
resetAfterCommit: () => void;
28+
}) => Reconciler<I>;

packages/reconciler/tsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"compilerOptions": {
4+
"outDir": "lib",
5+
"declaration": true,
6+
"declarationDir": "lib/types",
7+
"target": "ES2022",
8+
"module": "ESNext",
9+
"lib": ["ES2022", "DOM"],
10+
"moduleResolution": "Node",
11+
"esModuleInterop": true,
12+
"allowSyntheticDefaultImports": true,
13+
"strict": false,
14+
"skipLibCheck": true,
15+
"forceConsistentCasingInFileNames": true,
16+
"types": ["vitest/globals"],
17+
},
18+
"include": ["src", "globals.d.ts"],
19+
}

0 commit comments

Comments
 (0)