Skip to content

Commit

Permalink
Experimental support for React 19 [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Jan 13, 2025
1 parent 370e275 commit ce12fed
Show file tree
Hide file tree
Showing 15 changed files with 786 additions and 2,104 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.1.0

- Experimental support for React 19. `_debugSource` [was removed](https://github.com/facebook/react/pull/28265) in React 19. The PR says that tools should lazily generate component stack strace, but I couldn't find a way to use the React devtools globals to generate one for a given Fiber node. I've aksed some React team members about insights on how to make this works, but for now I decided to patch the jsx dev runtime (when serving it) to reinject into Fiber node the source prop added by JSX transform.

## 3.0.1

- Add vite@6 to peer dependency ranges
Expand Down
568 changes: 568 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

Binary file removed bun.lockb
Binary file not shown.
2 changes: 0 additions & 2 deletions bunfig.toml

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-plugin-react-click-to-component",
"type": "module",
"version": "3.0.1",
"version": "3.1.0",
"license": "MIT",
"scripts": {
"postinstall": "cd playground && bun i",
Expand All @@ -18,7 +18,7 @@
"trailingComma": "all"
},
"peerDependencies": {
"react": "^18.3.1",
"react": "^18.3.1 || ^19.0.0",
"vite": "^4 || ^5 || ^6"
},
"devDependencies": {
Expand Down
177 changes: 177 additions & 0 deletions playground/bun.lock

Large diffs are not rendered by default.

Binary file removed playground/bun.lockb
Binary file not shown.
2 changes: 0 additions & 2 deletions playground/bunfig.toml

This file was deleted.

7 changes: 4 additions & 3 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"lint-ci": "eslint --max-warnings 0"
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react": "^19.0.0",
"react-dom": "^19.0.0",
"vite-plugin-restart": "^0.4.2"
},
"devDependencies": {
"@types/react-dom": "^18.3.1",
"@types/react-dom": "^19.0.2",
"@vitejs/plugin-react-swc": "^3.7.2"
}
}
7 changes: 6 additions & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import { reactClickToComponent } from "../dist/index.mjs";
import restart from "vite-plugin-restart";

export default defineConfig({
plugins: [react(), reactClickToComponent()],
plugins: [
react(),
reactClickToComponent(),
restart({ restart: ["../dist/client.js", "../dist/index.mjs"] }),
],
server: { open: true },
});
463 changes: 0 additions & 463 deletions playground/yarn.lock

This file was deleted.

17 changes: 10 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,23 @@ const getLayersForElement = (element: Element) => {
};

const getPath = (fiber: Fiber) => {
if (!fiber._debugSource) {
const source = fiber._debugSource ?? fiber._debugInfo;
if (!source) {
console.debug("Couldn't find a React instance for the element", fiber);
return;
}
const { columnNumber = 1, fileName, lineNumber = 1 } = fiber._debugSource;
const { columnNumber = 1, fileName, lineNumber = 1 } = source;
return `${fileName}:${lineNumber}:${columnNumber}`;
};

type Source = {
columnNumber?: number;
fileName: string;
lineNumber?: number;
};
type Fiber = {
_debugSource?: {
columnNumber?: number;
fileName: string;
lineNumber?: number;
};
_debugSource?: Source;
_debugInfo?: Source; // Injected by React jsxDev patch for React 19
_debugOwner?: Fiber;
type: string | { displayName?: string; name: string };
};
Expand Down
13 changes: 12 additions & 1 deletion src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let root = "";
export const reactClickToComponent = (): PluginOption => ({
name: "react-click-to-component",
apply: "serve",
configResolved: (config) => {
configResolved(config) {
root = config.root;
},
transformIndexHtml: () => [
Expand All @@ -21,4 +21,15 @@ export const reactClickToComponent = (): PluginOption => ({
).replace("__ROOT__", root),
},
],
transform(code, id) {
if (!id.includes("jsx-dev-runtime.js")) return;
if (code.includes("_source")) return;
const defineIndex = code.indexOf('"_debugInfo"');
if (defineIndex === -1) return;
const valueIndex = code.indexOf("value: null", defineIndex);
if (valueIndex === -1) return;
return (
code.slice(0, valueIndex) + "value: source" + code.slice(valueIndex + 11)
);
},
});
Loading

0 comments on commit ce12fed

Please sign in to comment.