Skip to content

Commit a1cd579

Browse files
Merge pull request #346 from FormidableLabs/task/convert-source-ts
Migrate project source to TypeScript
2 parents 20d789a + 76a10a8 commit a1cd579

40 files changed

+653
-423
lines changed

.babelrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"loose": true
77
}
88
],
9-
"@babel/preset-react"
9+
"@babel/preset-typescript",
10+
["@babel/preset-react", { "runtime": "automatic" }]
1011
],
1112
"plugins": [
1213
"add-module-exports",
@@ -21,4 +22,4 @@
2122
}
2223
]
2324
]
24-
}
25+
}

.changeset/ninety-boats-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-live": major
3+
---
4+
5+
Migrated codebase to TypeScript.

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"root": true,
33
"parser": "@babel/eslint-parser",
4-
"plugins": ["prettier"],
4+
"plugins": ["prettier", "@typescript-eslint"],
55
"settings": {
66
"react": {
77
"version": "detect"
88
}
99
},
1010
"extends": [
1111
"eslint:recommended",
12+
"plugin:@typescript-eslint/recommended",
1213
"plugin:react/recommended",
1314
"plugin:import/recommended"
1415
],

.github/workflows/code-check.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
- name: Test
2929
run: yarn run test
3030

31+
- name: Type Check
32+
run: yarn run typecheck
33+
3134
- name: Library Build
3235
run: yarn run build
3336

.storybook/.babelrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"presets": [
33
"@babel/preset-env",
4-
"@babel/preset-react"
4+
"@babel/preset-typescript",
5+
["@babel/preset-react", { "runtime": "automatic" }]
56
],
67
"plugins": [
78
"add-module-exports",
89
"@babel/plugin-proposal-object-rest-spread",
910
"@babel/plugin-proposal-class-properties"
1011
]
11-
}
12+
}

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
"stories": [
3-
"../stories/**/*js"
3+
"../src/**/*.stories.@(js|jsx|ts|tsx)"
44
],
55
"addons": [
66
"@storybook/addon-controls",

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "3.2.0",
44
"description": "A production-focused playground for live editing React code",
55
"main": "dist/react-live.cjs.js",
6-
"typings": "./typings/react-live.d.ts",
6+
"types": "dist/index.d.ts",
77
"jsnext:main": "dist/react-live.es.js",
88
"module": "dist/react-live.es.js",
99
"license": "MIT",
@@ -16,6 +16,7 @@
1616
"prepublishOnly": "npm run build",
1717
"test": "jest",
1818
"test:typings": "typings-tester --dir typings",
19+
"typecheck": "tsc --noEmit",
1920
"lint": "eslint --config .eslintrc \"./src/**/*.js\"",
2021
"install:docs": "yarn --cwd website install",
2122
"start:docs": "yarn --cwd website start",
@@ -37,17 +38,23 @@
3738
"@babel/plugin-transform-runtime": "^7.15.0",
3839
"@babel/preset-env": "^7.15.0",
3940
"@babel/preset-react": "^7.14.5",
41+
"@babel/preset-typescript": "^7.21.0",
4042
"@changesets/cli": "^2.26.1",
4143
"@rollup/plugin-babel": "^5.3.0",
4244
"@rollup/plugin-commonjs": "^20.0.0",
4345
"@rollup/plugin-node-resolve": "^13.0.4",
4446
"@rollup/plugin-replace": "^3.0.0",
47+
"@rollup/plugin-typescript": "^11.0.0",
4548
"@storybook/addon-controls": "^6.4.13",
4649
"@storybook/builder-webpack5": "^6.5.16",
4750
"@storybook/manager-webpack5": "^6.5.16",
4851
"@storybook/react": "^6.4.13",
4952
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
50-
"@types/react": "^17.0.38",
53+
"@types/prismjs": "^1.26.0",
54+
"@types/react": "^18.0.31",
55+
"@types/styled-components": "^5.1.26",
56+
"@typescript-eslint/eslint-plugin": "^5.57.0",
57+
"@typescript-eslint/parser": "^5.57.0",
5158
"babel-jest": "^27.0.6",
5259
"babel-loader": "^8.2.2",
5360
"babel-plugin-add-module-exports": "^1.0.4",
@@ -61,15 +68,16 @@
6168
"prettier": "^2.5.1",
6269
"prismjs": "^1.26.0",
6370
"prop-types": "^15.7.2",
64-
"react": "^17.0.2",
65-
"react-dom": "^17.0.2",
71+
"react": "^18.2.0",
72+
"react-docgen-typescript": "^2.2.2",
73+
"react-dom": "^18.2.0",
6674
"react-test-renderer": "^17.0.2",
6775
"rollup": "^2.55.1",
6876
"rollup-plugin-filesize": "^9.1.1",
6977
"rollup-plugin-terser": "^7.0.2",
7078
"rollup-plugin-visualizer": "^5.5.4",
7179
"styled-components": "^4.0.0-beta.8",
72-
"typescript": "^2.9.2",
80+
"typescript": "^4.9",
7381
"typings-tester": "^0.3.1",
7482
"webpack": "^5.76.3"
7583
},

rollup.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { babel } from "@rollup/plugin-babel";
55
import { terser } from "rollup-plugin-terser";
66
import filesize from "rollup-plugin-filesize";
77
import { visualizer } from "rollup-plugin-visualizer";
8+
import typescript from "@rollup/plugin-typescript";
89

910
const plugins = [
1011
nodeResolve({
@@ -33,13 +34,15 @@ const plugins = [
3334
];
3435

3536
const devPlugins = plugins.concat([
37+
typescript(),
3638
replace({
3739
"process.env.NODE_ENV": JSON.stringify("development"),
3840
preventAssignment: true,
3941
}),
4042
]);
4143

4244
const prodPlugins = plugins.concat([
45+
typescript(),
4346
replace({
4447
"process.env.NODE_ENV": JSON.stringify("production"),
4548
preventAssignment: true,
@@ -50,19 +53,21 @@ const prodPlugins = plugins.concat([
5053
]);
5154

5255
const base = {
53-
input: "src/index.js",
56+
input: "src/index.ts",
5457
external: [
5558
"react",
5659
"react-dom",
5760
"prop-types",
5861
"prism-react-renderer",
5962
"sucrase",
63+
"react/jsx-runtime",
6064
],
6165
};
6266

6367
const output = {
6468
exports: "named",
6569
globals: {
70+
"react/jsx-runtime": "jsx-runtime",
6671
"prism-react-renderer": "Prism",
6772
react: "React",
6873
sucrase: "Sucrase",

src/components/Editor/index.js renamed to src/components/Editor/index.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
import Highlight, { Prism } from "prism-react-renderer";
2-
import PropTypes from "prop-types";
3-
import React, { useCallback, useEffect, useRef, useState } from "react";
1+
import Highlight, { Language, Prism, PrismTheme } from "prism-react-renderer";
2+
import { CSSProperties, useCallback, useEffect, useRef, useState } from "react";
43
import { useEditable } from "use-editable";
54
import { theme as liveTheme } from "../../constants/theme";
65

7-
const CodeEditor = (props) => {
6+
export type Props = {
7+
className?: string;
8+
code: string;
9+
disabled?: boolean;
10+
language: Language;
11+
prism?: typeof Prism;
12+
style?: CSSProperties;
13+
tabMode?: "focus" | "indentation";
14+
theme?: PrismTheme;
15+
onChange?(value: string): void;
16+
};
17+
18+
const CodeEditor = (props: Props) => {
819
const editorRef = useRef(null);
920
const [code, setCode] = useState(props.code || "");
1021

1122
useEffect(() => {
1223
setCode(props.code);
1324
}, [props.code]);
1425

15-
const onEditableChange = useCallback((_code) => {
26+
const onEditableChange = useCallback((_code: string) => {
1627
setCode(_code.slice(0, -1));
1728
}, []);
1829

@@ -41,6 +52,7 @@ const CodeEditor = (props) => {
4152
getLineProps,
4253
getTokenProps,
4354
style: _style,
55+
/* @ts-ignore — this property exists but the lib's types are incorrect */
4456
theme: _theme,
4557
}) => (
4658
<pre
@@ -79,20 +91,8 @@ const CodeEditor = (props) => {
7991
);
8092
};
8193

82-
CodeEditor.propTypes = {
83-
className: PropTypes.string,
84-
code: PropTypes.string,
85-
disabled: PropTypes.bool,
86-
language: PropTypes.string,
87-
onChange: PropTypes.func,
88-
prism: PropTypes.object,
89-
style: PropTypes.object,
90-
tabMode: PropTypes.oneOf(["focus", "indentation"]),
91-
theme: PropTypes.object,
92-
};
93-
9494
CodeEditor.defaultProps = {
9595
tabMode: "indentation",
96-
};
96+
} as Pick<Props, "tabMode">;
9797

9898
export default CodeEditor;

src/components/Live/LiveContext.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)