React Dev Inspector
Introduction
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
online demo: https://react-dev-inspector.zthxxx.me
press hotkey (ctrl⌃ + shift⇧ + commmand⌘ + c
), then click the HTML element you wish to inspect.
screen record gif (8M size):
Installation
npm i -D react-dev-inspector
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
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:
// https://github.com/zthxxx/react-dev-inspector#inspector-component-props
keys={['control', 'shift', 'command', 'c']}
disableLaunchEditor={false}
onHoverElement={(params: InspectParams) => {}}
onClickElement={(params: InspectParams) => {}}
>
<YourComponent>
...
</YourComponent>
</InspectorWrapper>
)
}
2. Set up Webpack Config
There are 4 ways to set up webpack config, please pick the one fit your project best.
In common cases (like create-react-app), you can use #raw-webpack-config,
If your project happen to use webpack-chain / umi2 / umi3, you can try out our integrated plugins / helper in #usage-with-webpack-chain, #usage-with-umi2 and #usage-with-umi3.
raw webpack config
You should add:
- an "inspector-loader"
- an api server middleware, "devServer", to open local IDE
to your current webpack config.
Example:
import { Configuration } from 'webpack'
import { launchEditorMiddleware } from 'react-dev-inspector/plugins/webpack'
const config: Configuration = {
module: {
rules: [
{
enforce: 'pre',
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: {},
}],
},
],
},
],
},
devServer: {
before: (app) => {
app.use(launchEditorMiddleware)
},
},
}
usage with webpack-chain
This is almost equivalent to raw webpack config
, except, it will NOT override devServer.before
, it only adds middleware before origin devServer.before
.
import { inspectorChainWebpack } from 'react-dev-inspector/plugins/webpack'
webpackChainConfig = inspectorChainWebpack(webpackChainConfig, {
exclude: [],
babelPlugins: [],
babelOptions: {},
})
usage with Umi3
This is 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
Example .umirc.dev.js
:
import { inspectorChainWebpack } from 'react-dev-inspector/plugins/webpack'
export default {
chainWebpack(config) {
inspectorChainWebpack(config, {
})
return config
},
}
Example Project Code
Configuration
<Inspector>
Component Props
checkout TS definition under react-dev-inspector/es/Inspector.d.ts
.
interface InspectParams {
element: HTMLElement,
fiber?: React.Fiber,
codeInfo?: {
lineNumber: string,
columnNumber: string,
relativePath: string,
},
name?: string,
}
Inspector Loader Props
interface InspectorConfig {
exclude?: (string | RegExp)[],
babelPlugins?: ParserPlugin[],
babelOptions?: ParserOptions,
}
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
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 want, just set env to shell
export REACT_EDITOR=vim
How It Works
Analysis of Theory
License
MIT LICENSE