React Dev Inspector
This package allows users to jump to local IDE code directly from browser React component by just a simple click, which is similar to Chrome inspector but more advanced.
Preview
press hotkey (
ctrl⌃ + shift⇧ + commmand⌘ + c
), then click the HTML element you wish to inspect.
screen record gif (8M size):
Installation
Usage
Users need to add React component and apply webpack config before connecting your React project with 'react-dev-inspector'.
Note: You should NOT use this package, and React component, webpack config in production mode
1. Add Inspector React Component
2. Set up Inspector Config
You should add:
- an inspector babel plugin, to inject source code location info
react-dev-inspector/plugins/babel
- an server api middleware, to open local IDE
import { launchEditorMiddleware } from 'react-dev-inspector/plugins/webpack'
to your current project development config.
Such as add babel plugin into your .babelrc
or webpack babel-loader
config,
add api middleware into your webpack-dev-server
config or other server setup.
There are some example ways to set up, please pick the one fit your project best.
In common cases, if you're using webpack, you can see #raw-webpack-config,
If your project happen to use vite / nextjs / create-react-app and so on, you can also try out our integrated plugins / examples with
- #usage-with-vite2
- #usage-with-next.js
- #usage-with-create-react-app
- #usage-with-umi3
- #usage-with-umi2
- #usage-with-ice.js
raw webpack config
Example:
usage with Vite2
example project see: https://github.com/zthxxx/react-dev-inspector/tree/master/examples/vite2
example vite.config.ts
:
usage with Next.js
use Next.js Custom Server + Customizing Babel Config
example project see: https://github.com/zthxxx/react-dev-inspector/tree/master/examples/nextjs
in server.js
, example:
in package.json
, example:
in .babelrc.js
, example:
usage with create-react-app
cra + react-app-rewired + customize-cra example config-overrides.js
:
example project see: https://github.com/zthxxx/react-dev-inspector/tree/master/examples/cra
usage with Umi3
example project see: https://github.com/zthxxx/react-dev-inspector/tree/master/examples/umi3
Example .umirc.dev.ts
:
usage with Umi2
Example .umirc.dev.js
:
usage with Ice.js
Example build.json
:
Examples Project Code
- vite2
- next.js
- create-react-app
- umi3
Configuration
<Inspector>
Component Props
checkout TS definition under react-dev-inspector/es/Inspector.d.ts
.
Property | Description | Type | Default |
---|---|---|---|
keys | inspector hotkeys supported keys see: https://github.com/jaywcjlove/hotkeys#supported-keys |
string[] |
['control', 'shift', 'command', 'c'] |
disableLaunchEditor | disable editor launching (launch by default in dev Mode, but not in production mode) |
boolean |
false |
onHoverElement | triggered when mouse hover in inspector mode | (params: InspectParams) => void |
- |
onClickElement | triggered when mouse hover in inspector mode | (params: InspectParams) => void |
- |
Inspector Babel Plugin Options
Inspector Loader Props
IDE / Editor config
This package uses react-dev-utils
to launch your local IDE application, but, which one will be open?
In fact, it uses an environment variable named REACT_EDITOR
to specify an IDE application, but if you do not set this variable, it will try to open a common IDE that you have open or installed once it is certified.
For example, if you want it always open VSCode when inspection clicked, set export REACT_EDITOR=code
in your shell.
VSCode
-
install VSCode command line tools, see the official docs
-
set env to shell, like
.bashrc
or.zshrc
WebStorm
- just set env with an absolute path to shell, like
.bashrc
or.zshrc
(only MacOS)
OR
-
install WebStorm command line tools
-
then set env to shell, like
.bashrc
or.zshrc
Vim
Yes! you can also use vim if you want, just set env to shell
How It Works
-
Stage 1 - Compile Time
- [babel plugin] inject source file path/line/column to JSX data attributes props
-
Stage 2 - Web React Runtime
-
[React component]
Inspector
Component in react, for listen hotkeys, and request api to dev-server for open IDE.Specific, when you click a component DOM, the
Inspector
will try to obtain its source file info (path/line/column), then request launch-editor api (in stage 3) with absolute file path.
-
-
Stage 3 - Dev-server Side
-
[middleware] setup
launchEditorMiddleware
in webpack dev-server (or other dev-server), to open file in IDE according to the request params.Only need in development mode,and you want to open IDE when click a component element.
Not need in prod mode, or you just want inspect dom without open IDE (set
disableLaunchEditor={true}
to Inspector component props)
-
Analysis of Theory
- [chinese] ? 我点了页面上的元素,VSCode 乖乖打开了对应的组件?原理揭秘
License
[MIT LICENSE]