React Dev Inspector
dev-tool for inspect react components and jump to local IDE for component code.
Preview
Online demo: https://react-dev-inspector.zthxxx.me
Screen record (gif 8M):
Install
npm i -D react-dev-inspector
Usage
There are 3 steps required to use react-dev-inspector
, here will list some typical manual config of webpack, and this lib also provide some integrated plugins to make you easy to use
Typical webpack config
Include step-1 and step-3, compile Time and dev-server Side
import { Configuration, DefinePlugin } from 'webpack'
import { createLaunchEditorMiddleware } from 'react-dev-inspector/plugins/webpack'
const config: Configuration = {
module: {
rules: [
{
test: /\.[jt]sx$/,
exclude: [
/node_modules/,
/file-you-want-exclude/,
],
use: [
{
loader: 'react-dev-inspector/plugins/webpack/inspector-loader',
options: [{
exclude: [
'xxx-file-will-be-exclude',
/regexp-to-match-file /,
],
babelPlugins: [],
babelOptions: {},
}],
},
],
},
],
},
plugins: [
new DefinePlugin({
'process.env.PWD': JSON.stringify(process.cwd()),
}),
],
devServer: {
before: (app) => {
app.use(createLaunchEditorMiddleware())
},
},
}
Usage with Webpack Chain
Include step 1 and step 3, it's almost equivalent to Typical webpack config
above,
but it will NOT override devServer.before
, just add middleware before origin devServer.before
import { inspectorChainWebpack } from 'react-dev-inspector/plugins/webpack'
webpackChainConfig = inspectorChainWebpack(webpackChainConfig, {
exclude: [],
babelPlugins: [],
babelOptions: {},
})
Usage with Umi3
Include step 1 and step 3, also equivalent to Usage with Webpack Chain
Example .umirc.dev.ts
:
import { defineConfig } from 'umi'
export default defineConfig({
plugins: [
'react-dev-inspector/plugins/umi/react-inspector',
],
inspectorConfig: {
exclude: [],
babelPlugins: [],
babelOptions: {},
},
})
Usage with Umi2
Include step 1 and step 3
Example .umirc.dev.js
:
import { inspectorChainWebpack } from 'react-dev-inspector/plugins/webpack'
export default {
chainWebpack(config) {
inspectorChainWebpack(config, {
})
return config
},
}
Use in React
Include step-2, react runtime
import React from 'react'
import { Inspector, InspectParams } from 'react-dev-inspector'
const InspectorWrapper = process.env.NODE_ENV === 'development'
? Inspector
: React.Fragment
export const Layout = () => {
return (
<InspectorWrapper
// props docs see below
keys={['control', 'shift', 'command', 'c']}
disableLaunchEditor={false}
onHoverElement={(params: InspectParams) => {}}
onClickElement={(params: InspectParams) => {}}
>
<YourComponent>
...
</YourComponent>
</InspectorWrapper>
)
}
after <Inspector>
component was mounted,you can use window.__REACT_DEV_INSPECTOR_TOGGLE__()
to toggle inspector.
Config of Loader / Component / IDE
Inspector Loader Props
interface InspectorConfig {
exclude?: (string | RegExp)[],
babelPlugins?: ParserPlugin[],
babelOptions?: ParserOptions,
}
<Inspector>
Component Props
typescript define you can see in react-dev-inspector/es/Inspector.d.ts
Property | Description | Type | Default |
---|
keys | inspector toggle hotkeys
supported keys see: https://github.com/jaywcjlove/hotkeys#supported-keys | string[] | ['control', 'shift', 'command', 'c'] |
disableLaunchEditor | whether disable click react component to open IDE for view component code
(launchEditor by default only support be used with react-dev-inpector plugins in dev) | boolean | false |
onHoverElement | triggered while inspector start and mouse hover in a HTMLElement | (params: InspectParams) => void | - |
onClickElement | triggered while inspector start and mouse click on a HTMLElement | (params: InspectParams) => void | - |
interface InspectParams {
element: HTMLElement,
fiber?: React.Fiber,
codeInfo?: {
lineNumber: string,
columnNumber: string,
relativePath: string,
},
name?: string,
}
IDE / Editor config
this lib use react-dev-utils
to launch your local IDE app, but which one app will be open?
In fact, it uses an environment variable named REACT_EDITOR
, but if you not set this variable, it will guess a IDE in what you opened now or what you installed.
For example, if you want it always open VSCode when inspect 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
export REACT_EDITOR=code
WebStorm
OR
-
install WebStorm command line tools
-
then set env to shell, like .bashrc
or .zshrc
export REACT_EDITOR=webstorm
Vim
yes, you can also use vim if you prefer it, just set env to shell
export REACT_EDITOR=vim
Example Project Code
code see: https://github.com/zthxxx/react-dev-inspector/tree/master/site
project preview: https://react-dev-inspector.zthxxx.me
Analysis of Theory
License
MIT LICENSE