Skip to content

Commit e86b98f

Browse files
Replace react with preact (#3221)
1 parent 7de6e61 commit e86b98f

42 files changed

Lines changed: 394 additions & 323 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Config } from "jest";
2+
import { preactModuleNameMapper } from "@cursorless/common";
3+
4+
const config: Config = {
5+
preset: "ts-jest",
6+
testEnvironment: "jsdom",
7+
moduleNameMapper: {
8+
...preactModuleNameMapper,
9+
"^@cursorless/cheatsheet$": "<rootDir>/../cheatsheet/src/index.ts",
10+
"\\.(css|scss)$": "<rootDir>/src/test/styleMock.ts",
11+
},
12+
};
13+
14+
export default config;

packages/cheatsheet-local/package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
}
2020
},
2121
"scripts": {
22+
"test": "jest",
2223
"compile": "tsc --build",
2324
"watch": "tsc --build --watch",
2425
"build": "pnpm build:prod",
@@ -29,19 +30,14 @@
2930
"dependencies": {
3031
"@cursorless/cheatsheet": "workspace:*",
3132
"@cursorless/common": "workspace:*",
32-
"react": "^19.2.4",
33-
"react-dom": "^19.2.4",
33+
"preact": "^10.29.0",
3434
"tslib": "^2.8.1"
3535
},
3636
"devDependencies": {
37+
"@preact/preset-vite": "^2.10.3",
3738
"@tailwindcss/postcss": "^4.2.1",
38-
"@testing-library/dom": "^10.4.1",
39-
"@testing-library/react": "^16.3.2",
4039
"@types/jest": "^30.0.0",
4140
"@types/node": "^24.12.0",
42-
"@types/react": "^19.2.14",
43-
"@types/react-dom": "^19.2.3",
44-
"@vitejs/plugin-react": "^6.0.1",
4541
"jest": "^30.3.0",
4642
"postcss": "^8.5.8",
4743
"tailwindcss": "^4.2.1",
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
import { render } from "@testing-library/react";
1+
import { fakeCheatsheetInfo } from "@cursorless/cheatsheet";
2+
import { render } from "preact";
3+
import { act } from "preact/test-utils";
24
import { App } from "./app";
35

46
describe("App", () => {
5-
it("should render successfully", () => {
6-
const { baseElement } = render(<App />);
7+
beforeEach(() => {
8+
document.cheatsheetInfo = fakeCheatsheetInfo;
9+
});
10+
11+
afterEach(() => {
12+
document.body.innerHTML = "";
13+
});
714

8-
expect(baseElement).toBeTruthy();
15+
it("should render successfully", async () => {
16+
const container = document.createElement("div");
17+
document.body.append(container);
18+
19+
await act(() => {
20+
render(<App />, container);
21+
});
22+
23+
expect(container).toBeTruthy();
924
});
1025

11-
it("should have a greeting as the title", () => {
12-
const { getByText } = render(<App />);
26+
it("should have a greeting as the title", async () => {
27+
const container = document.createElement("div");
28+
document.body.append(container);
29+
30+
await act(() => {
31+
render(<App />, container);
32+
});
1333

14-
expect(getByText(/Welcome cheatsheet-local/gi)).toBeTruthy();
34+
expect(container.textContent).toMatch(/Cursorless Cheatsheet/gi);
1535
});
1636
});
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { StrictMode } from "react";
2-
import * as ReactDOM from "react-dom/client";
1+
import { render } from "preact";
2+
import { StrictMode } from "preact/compat";
33
import { App } from "./app/app";
44

5-
const root = ReactDOM.createRoot(
6-
document.getElementById("root") as HTMLElement,
7-
);
8-
root.render(
5+
render(
96
<StrictMode>
107
<App />
118
</StrictMode>,
9+
getRoot(),
1210
);
11+
12+
function getRoot() {
13+
const root = document.getElementById("root");
14+
if (root == null) {
15+
throw new Error("Missing root container");
16+
}
17+
return root;
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {};

packages/cheatsheet-local/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"target": "es2022",
55
"lib": ["dom", "es2022"],
66
"jsx": "react-jsx",
7+
"jsxImportSource": "preact",
78
"allowSyntheticDefaultImports": true,
89
"skipLibCheck": true,
910
"esModuleInterop": true,

packages/cheatsheet-local/vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fakeCheatsheetInfo } from "@cursorless/cheatsheet";
22
import { viteHtmlParams } from "@cursorless/common";
3-
import react from "@vitejs/plugin-react";
3+
import preact from "@preact/preset-vite";
44
import { defineConfig } from "vite";
55
import { viteSingleFile } from "vite-plugin-singlefile";
66

@@ -11,7 +11,7 @@ export default defineConfig(() => {
1111
},
1212

1313
plugins: [
14-
react(),
14+
preact(),
1515
viteSingleFile(),
1616
viteHtmlParams({
1717
FAKE_CHEATSHEET_INFO: JSON.stringify(fakeCheatsheetInfo),

packages/cheatsheet/jest.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Config } from "jest";
2+
import { preactModuleNameMapper } from "@cursorless/common";
23

34
const config: Config = {
45
preset: "ts-jest",
56
testEnvironment: "jsdom",
7+
moduleNameMapper: preactModuleNameMapper,
68
};
79

810
export default config;

packages/cheatsheet/package.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@cursorless/cheatsheet",
33
"version": "0.1.0",
4-
"description": "Core cheatsheet react component",
4+
"description": "Core cheatsheet Preact component",
55
"license": "MIT",
66
"type": "module",
77
"main": "./out/index.js",
@@ -26,21 +26,15 @@
2626
"watch": "pnpm run --filter @cursorless/cheatsheet --parallel '/^watch:.*/'"
2727
},
2828
"dependencies": {
29+
"@cursorless/common": "workspace:*",
2930
"@fortawesome/fontawesome-svg-core": "^7.2.0",
3031
"@fortawesome/free-solid-svg-icons": "^7.2.0",
3132
"@fortawesome/react-fontawesome": "^3.2.0",
32-
"react": "^19.2.4",
33-
"react-dom": "^19.2.4",
34-
"react-string-replace": "^2.0.1",
33+
"preact": "^10.29.0",
3534
"react-use": "^17.6.0"
3635
},
3736
"devDependencies": {
38-
"@testing-library/dom": "^10.4.1",
39-
"@testing-library/react": "^16.3.2",
4037
"@types/jest": "^30.0.0",
41-
"@types/react": "^19.2.14",
42-
"@types/react-dom": "^19.2.3",
43-
"@types/react-helmet": "^6.1.11",
4438
"jest": "^30.3.0",
4539
"jest-environment-jsdom": "^30.3.0",
4640
"ts-jest": "^29.4.6",

packages/cheatsheet/src/lib/CheatsheetPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from "react";
1+
import type { ComponentChildren } from "preact";
22
import CheatsheetListComponent from "./components/CheatsheetListComponent";
33
import CheatsheetLegendComponent from "./components/CheatsheetLegendComponent";
44
import cheatsheetLegend from "./cheatsheetLegend";
@@ -56,7 +56,7 @@ function Cheatsheet({ cheatsheetInfo }: Props) {
5656
}
5757

5858
type CheatsheetSectionProps = {
59-
children?: React.ReactNode;
59+
children?: ComponentChildren;
6060
};
6161

6262
function CheatsheetSection({ children }: CheatsheetSectionProps) {

0 commit comments

Comments
 (0)