React Dev Inspector
repository·dev·Indexed 23 days ago
https://github.com/zthxxx/react-dev-inspectorA tool that enables instant navigation from a React component in the browser directly to its source code in a local IDE. It consists of a three-part pipeline: an optional Babel plugin for JSX source injection, an inspector component to read source info, and dev-server middleware to trigger the local editor. It provides official plugins and integration guides for Vite, Webpack 4/5, Next.js, Rspack, Umi3, and Umi4.
What's inside react-dev-inspector
- React Dev Inspector is a tool designed to enable seamless code navigation from the browser to your local IDE. It allows you to click on a React component in the browser and instantly jump to its corresponding source code in your editor, acting as an advanced version of Chrome's Inspector specifically for React development.
Introduction to React Dev Inspector
devReact Dev Inspector is a tool designed for seamless code navigation from the browser to your local IDE. It allows you to click on a React component in the browser and instantly jump to its source code in your editor.
Key features include:
- No browser restrictions or framework limitations.
- Works with local, remote, or containerized environments.
- Supports both development and production build modes.
- Designed to be integrated into custom tools like browser extensions or developer toolkits.
Use @react-dev-inspector/web-components for UI elements
devThe
@react-dev-inspector/web-componentspackage provides the Web UI components used byreact-dev-inspector. These components are built usingsolid-jsand are exported as standard Web Components.Key components include:
Overlay: The main inspector UI. It supports customboxSizingand bounding configurations.InspectContextPanel: A floating, draggable, and resizable context menu used to display the element hierarchy.
Configure the optional Compiler Plugin
devThe Compiler Plugin is an optional part of the
react-dev-inspectorconfiguration. Its purpose is to record source path information (file name, line number, and column) into React components during development. This information allows the<Inspector>component to map DOM elements back to their original source code.Most modern React frameworks provide this information out-of-the-box, so manual configuration is often unnecessary. The
<Inspector>component can retrieve this data from either:- Custom data attributes starting with
data-inspector-. - React's internal
fiber._debugSourceproperty.
If your framework does not automatically provide this, you must configure a compiler plugin (Babel, SWC, or esbuild) as described in the following sections.
- Custom data attributes starting with
Compatibility and Automatic Disabling of the Babel Plugin
devThe Umi4 plugin includes
@react-dev-inspector/babel-plugin. This Babel plugin is automatically disabled under the following conditions:- When your Umi4 configuration uses
srcTranspiler: 'swc'. - When you are running a production build.
- When your Umi4 configuration uses
Understand the React Dev Inspector working pipeline
devThe tool operates through a three-part pipeline:
- Inject JSX Source (Optional): A compiler plugin records source path information into React components during the development stage. Most frameworks support this out-of-the-box.
- Inspector Component: The
<Inspector/>component reads the injected source info and sends it to the dev-server when you inspect elements in the browser. - Dev Server Middleware: Middleware integrated into your dev-server receives the source path info via an API and calls your local IDE/Editor to open the specific source file.
How @react-dev-inspector/babel-plugin works
devThe
@react-dev-inspector/babel-plugininjects custom data attributes into your JSX elements during the compilation process. These attributes allow the inspector to map DOM elements back to their original source code location.Note: This step is generally OPTIONAL. Most modern React frameworks provide this functionality out-of-the-box. You typically only need to configure this manually if your build pipeline does not already support these features or if you are using a custom Babel setup.
// Input <div /> // Output <div data-inspector-relative-path="src/path/Component.tsx" data-inspector-line="10" data-inspector-column="6" />Extend the Inspector with custom InspectAgents
devAs of v2.1.0-beta.10, the
Inspectorimplementation has been refactored intoDOMInspectAgent. This allows you to set customInspectAgentimplementations to work alongside the defaultDOMInspectAgent.To build custom agents, you can use the following exported utilities:
fiberUtils: For accessing React Fiber nodes.inspectUtils: General utilities to assist with the inspection process.
Additionally, the
@react-dev-inspector/web-componentspackage provides:Overlay: The UI indicating the inspected element. It supports custom logic forboxSizingandbounding.InspectContextPanel: A draggable and resizable floating context-menu panel triggered by right-click inspection.
Integrate react-dev-inspector into a Vite project
devTo use
react-dev-inspectorin a Vite project, you need to install the@react-dev-inspector/vite-pluginpackage and add theinspectorServerplugin to yourvite.config.ts. This plugin registers a middleware that enables the local editor launch functionality. It is compatible with Vite versions 2, 3, 4, and 5.npm i -D @react-dev-inspector/vite-pluginConfigure @react-dev-inspector/vite-plugin in vite.config.ts
devAdd
inspectorServer()to thepluginsarray in yourvite.config.tsfile. This plugin registers aninspectorServermiddleware that allows the local editor to launch via an endpoint. It is compatible withvite@5,vite@4,vite@3, andvite@2.import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import { inspectorServer } from '@react-dev-inspector/vite-plugin' // https://vitejs.dev/config/ export default defineConfig({ ... plugins: [ react(), /** * react-dev-inspector server config for vite */ inspectorServer(), ], })Configure Dev Server Middleware for IDE integration
devTo enable the ability to open source files directly in your local IDE/Editor from the React Dev Inspector, you must implement a Dev Server Middleware. This middleware acts as a bridge that receives API requests from the Inspector Component and triggers the local editor on the server side.
Prerequisites: Before setting up the middleware, you must have already completed Part 1: adding the Inspector Component to your project.
Configure Nvim or Vim as the editor
devTo use Neovim or Vim, set the
REACT_EDITORenvironment variable tonvimorvimin your shell configuration file.export REACT_EDITOR=nvim # or export REACT_EDITOR=vim