Git Product home page Git Product logo

react-dev-inspector's Introduction

React Dev Inspector

NPM Version NPM Downloads Node.js License

Introduction

react-dev-inspector is the tool for seamlessly code navigation from browser to IDE.

With just a simple click, you can jump from a React component on the browser to its source code in your local IDE instantly. Think of it as a more advanced version of Chrome's Inspector.

See the document on: https://react-dev-inspector.zthxxx.me

Why React Dev Inspector

Have you ever run into any of these issues 🤔:

  • You've got to fix some bugs in your team's project, but you have no idea where the heck page/component's code is located.
  • You're eager to dive into an open-source project that interests you, but it's hard to find where the code for the page/component is implemented.
  • You may thinking about a component and want to quickly peek at its code, but don't want to memorize or manually expand those never-ending deep file paths.

That's exactly why react-dev-inspector was created.


Quick Look

Just check out this demo below and you'll get it in a snap.

screen record gif (8M size):

Showcase: https://react-dev-inspector.zthxxx.me/showcase

inspector-gif

Wanna try out the demo right now? Sure, here is the online demo:

Open in StackBlitz


How to Use and Configure

According to the working pipeline below, the Part.1 and Part.2 are what you need configure to use.

Basically, it's includes:

  1. add the <Inspector/> component in your page
  2. integrate the middleware in your framework's dev-server

Check the Documentation Site for more details


How It Works

Here is the working pipeline of react-dev-inspector:

Working Pipeline


0. Inject JSX Source

The compiler's plugin records source path info into React components during development stage.

Note: The 0 of Part.0 implies that this section is generally OPTIONAL. Most React frameworks offer this feature out-of-the-box, which means you usually don't need to manually configure it additionally.

1. Inspector Component

The react-dev-inspector provide a <Inspector/> component to reads the source info, and sends it to the dev-server when you inspect elements on browser.

2. Dev Server Middleware

The react-dev-inspector provide some middlewares for dev-server in most frameworks to receives source path info from API, then call your local IDE/Editor to open the source file.



Articles about it



License

MIT LICENSE

Thanks for @zthxxx @sonacy @sl1673495

react-dev-inspector's People

Contributors

nzreal avatar renovate-bot avatar renovate[bot] avatar sl1673495 avatar symind avatar thrallzhou avatar wenjingyuer avatar zhouhaimei avatar zthxxx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-dev-inspector's Issues

vscode insiders

If anyone is using vscode insiders, change window.open(`vscode://file/${absolutePath}:${lineNumber}:${columnNumber}`) to window.open(`vscode-insiders://file/${absolutePath}:${lineNumber}:${columnNumber}`).

Don't forget "s" in "insiders".


如果有谁在用vscode insiders版,把 window.open(`vscode://file/${absolutePath}:${lineNumber}:${columnNumber}`) 改成 window.open(`vscode-insiders://file/${absolutePath}:${lineNumber}:${columnNumber}`)

不要忘记"insider"里面的“s“

@babel/types should be listed in dependencies instead of devDependencies

Currently, in src/plugins/babel/visitor.ts, we directly use the utils imported from @babel/types/lib/builders/generated, which means @babel/types/lib/builders/generated is not only needed during development, we should put it under dependencies instead of devDependencies.

import {
  jsxAttribute,
  jsxIdentifier,
  stringLiteral,
} from '@babel/types/lib/builders/generated'
//...

const lineAttr: JSXAttribute | null = isNil(line)
    ? null
    : jsxAttribute(
      jsxIdentifier('data-inspector-line'),
      stringLiteral(line.toString()),
    )

考虑支持 esbuild 吗

项目编译 js 部分最近会整体从 babel 迁移至 esbuild,打包还是用 webpack,作者考虑支持 esbuild 吗?

按README文档使用,Add Inspector React Component部分似乎有点问题

README中创建 Add Inspector React Component 这一步按照示例会报错

import React from 'react'
import { Inspector, InspectParams } from 'react-dev-inspector'

const isDev = process.env.NODE_ENV === 'development'

export const Layout = () => {
  // ...

  return (
    <>
      <YourComponent />

      {isDev && (
        <Inspector
          // props see docs:
          // https://github.com/zthxxx/react-dev-inspector#inspector-component-props
          keys={['control', 'shift', 'command', 'c']}
          onHoverElement={(inspect: InspectParams) => {}}
          onClickElement={(inspect: InspectParams) => {}}
        >
      )}
    </>
  )
}

image

通过以下方式使用时正常的

const WrapperComponent =
  process.env.NODE_ENV === 'development' ? Inspector : Fragment

function App() {
  return (
    <div className="App">
      <WrapperComponent keys={["control", "shift", "alt", "f"]}>
         <YourComponent />
      </WrapperComponent>
    </div>
  )
}

这块是文档上的错误,还是我的使用方式哪里错了呢 :(

app中间件的引用错误

image
另外建议 devServer 和 createLaunchEditorMiddleware 这里重点标注一下。看readme去配置webpack的时候直接忽略掉这一点了结果肯定没办法用。

umi 3.x 在 window 10 环境无法唤醒 vscode

使用 ant-design-pro 开发的时候发现了 react-dev-inspector 插件。后来自己使用 umi 3.x 搭框架时,同样的环境、配置和操作,ant-design-pro 表现正常,可是自己搭的 umi 3.x 一直无法唤醒 vscode ,请问这个问题怎么解决?

环境和依赖

  • window版本:10
  • node版本:14.15.3
  • chrome版本:91.0.4472.101
  • umi版本:3.4.25
  • react版本:17.0.2
  • react-dev-inspector版本:1.6.0

操作步骤

第一步

安装 react-dev-inspector 依赖。

tyarn add react-dev-inspector --dev

第二步

config/config.dev.ts 中引入插件。

import { defineConfig } from 'umi';

export default defineConfig({
  plugins: [
    // https://github.com/zthxxx/react-dev-inspector
    'react-dev-inspector/plugins/umi/react-inspector',
  ],
  // https://github.com/zthxxx/react-dev-inspector#inspector-loader-props
  inspectorConfig: {
    excludes: [],
    babelPlugins: [],
    babelOptions: {},
  },
});

第三步

src/app.tsx 使用 Inspector

import { Fragment } from 'react';
import { Inspector } from 'react-dev-inspector';
import type { InspectParams } from 'react-dev-inspector';
const InspectorWrapper = process.env.NODE_ENV === 'development' ? Inspector : Fragment;

/**
 * 框架布局配置
 * @see https://procomponents.ant.design/components/layout
 */
export const layout: RunTimeLayoutConfig = ({ initialState }) => ({
  childrenRender: children => (
    <InspectorWrapper
      // https://github.com/zthxxx/react-dev-inspector#inspector-component-props
      keys={['control', 'shift', 'command', 'c']}
      disableLaunchEditor={false}
      onHoverElement={(params: InspectParams) => { }}
      onClickElement={(params: InspectParams) => { }}
    >
      {children}
    </InspectorWrapper>
  ),
})

最终结果

貌似一切正常,就是无法唤起 vscode

image
image

create-react-app 5.0.0 无法唤起 vscode

您好,我按照
https://github.com/zthxxx/react-dev-inspector#usage-with-create-react-app
https://github.com/zthxxx/react-dev-inspector#vscode 里面的配置,
配置好我的项目(用 create-react-app 建的 demo 项目),其它的都没有问题,就是点击的时候无法唤起 vscode
我打开控制台也看到发出请求了
http://localhost:3000/__open-stack-frame-in-editor/relative?fileName=src%2FHelloWord.js&lineNumber=5&colNumber=6
命令行输入 code 也能打开 vscode
请问还有哪里没有配置好?

[email protected] 在 vite2 下跳转 editor 失败

给出如下错误:

browser-external:querystring:3 Uncaught Error: Module "querystring" has been externalized for browser compatibility and cannot be accessed in client code.
    at Object.get (browser-external:querystring:3)
    at gotoEditor (inspect.js:129)
    at Object.handleClickElement [as onClick] (Inspector.js:35)
    at onClick (hightlight.js:55)

Enhancement required list

  • short domain (cname) to inspector.zthxxx.me
  • redesign website ui
  • add logo
  • refactor inspector hotkey response (trigger to display inspector mask immediately) #108
  • add changelog
  • docs for set windows environment variables for REACT_EDITOR
  • build cjs for ssr like used in nextjs
  • inspector level comment
  • support for monorepo and move listen mouse event from window to Inspector contaniner

在基于icejs 的项目中的使用

你好,我们团队现在有个基于ice js的项目,我想尝试在其中此插件,但各种尝试配置折腾了半天后却无法生效.
大佬有空的时候可以看下在ice.js中如何使用吗,秋泥膏
配置如下:
ice.js 的配置文档:https://ice.alibaba-inc.com/docs/guide/basic/build
//build.config.js

import { launchEditorMiddleware } from 'react-dev-inspector/plugins/webpack'
module.exports = {
    define: {
      env: process.env.NODE_ENV,
    },
    plugins: [
      ['build-plugin-fusion', {
        themePackage:"@alifd/theme-design-pro",
      }],
      (api) => {
        api.onGetWebpackConfig((config) => {
          config.entry('src/index.js');
        });
      },
      "@ali/build-plugin-ice-def",
    ],
    devServer: {
        before: (app) => {
          console.log('launchEditorMiddleware')
          app.use(launchEditorMiddleware)
        }
    },
    babelPlugins: [
        'react-dev-inspector/plugins/babel'
      ],
  }

visual indicator of dev-inspector is "active"

First of all thanks for such an amazing project, i do have a suggestion that there should be a UI indicator or sorts, since currently, when i press the hotkey combination, if i don't move the mouse, i do not know that the inspector is active.

node版本兼容性问题

package.json中的node版本要求必须是>=12.0.0

这样低于12.0.0的项目都会安装失败

这样的版本要求是必须的吗?

It might be unnecessary to setup an extension to open files in VS Code.

This project is creative! Perhaps it can get rid of the extension for VS Code users to simplify the workflow.

VSCode sets up the vscode:// protocol handler that works in browsers. URLs that starts with file will be handled as Open file command.
For example, a link with the URL vscode://file/Users/username/Downloads/test.txt would open /Users/username/Downloads/test.txt for you, if it exists on your drive.

请问在微前端架构下,需要怎么配置?

我们的架构是一个主应用,加 n 多个子应用,所有的子应用都通过路由配置加载到主应用上(所以无法在子应用上包裹 Inspector 组件

我是这么进行配置的:
1、在主应用里面增加了 Inspector 组件来包裹入口。
2、在主应用的 webpack 配置里增加对应的 dev-server, loader, process.env.pwd 配置。
3、在子应用里配置 webpack 的 loader 和 process.env.pwd 配置。

但是这样就会导致子应用里面的文件无法跳转,原因是因为 loader 解析的时候,附加在 dom 上的只是一个相对路径

const relativePathAttr: JSXAttribute = jsxAttribute(
jsxIdentifier('data-inspector-relative-path'),
stringLiteral(relativePath),
)

只是在点击的时候,调用的 gotoEditor 方法里面进行了 process.env.pwd 和相对路径的拼接
export const gotoEditor = (source?: CodeInfo) => {
// PWD auto defined in webpack plugin `config-inspector`
const pwd = process.env.PWD
if (!source || !pwd) return
const { relativePath, lineNumber, columnNumber } = source
const fileName = path.join(pwd, relativePath)

那我在子应用的 webpack 里面的process.env.pwd 配置就相当于没有效果了。(因为只有主应用才有 Inspector,所以对应的 process.env.pwd 也是从主应用获取的。

请问是否可以直接在 loader 解析的时候,附加在 dom 上的就是一个绝对地址呢?这样会不会有什么问题?
或者可以直接把 pwd 加在 loader 的配置中,然后在 loader 解析的时候和相对地址拼接(相当于把 gotoEditor 的拼接提前到了 loader 解析阶段,这样是否可行?

获取不到absolutePath

问题描述,用的umi3,且有[@babel/plugin-transform-react-jsx-source]
image
image
获取不到absolutePath,有遇到相同问题的吗,很疑惑不知道是哪的问题

无法唤起vscode

To set up the editor integration, add something like REACT_EDITOR=atom to the .env.local file in your project folder and restart the development server. Learn more: 老哥这是怎么回事啊? 添加了REACT_EDITOR=code 也没用

提问:配置问题

你好,我的项目配置是typescript,webpack使用ts-loader进行编译,请问babel-plugin是在ts-loader之前还是在之后呢

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: .github/renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid schedule: 'Invalid schedule: Failed to parse "schedule:monthly"'

An example about use in some frameworks like blitz.js

Thanks for review.
In some frameworks like blitz.js,there are a intermediate state for compilation, so the process.cwd() in middleware function "launchEditorMiddleware" will get the error base workplace, so perhaps can provide some options for user customizing their config. Of course, you can have a trick like this

const fixLaunchEditorMiddleware = (req, res, next) => {
const cwd = process.cwd
process.cwd = () => path.resolve(cwd(), "../../")
launchEditorMiddleware(req, res, next)
process.cwd = cwd
}

微前端里使用失效

qiankun结合react-dev-inspector,在主域中点击子项目的组件,不能打开vscode,不能自动定位组件所在代码。
但是如果单独打开子项目,点击组件,可以自定定位到所在代码。
有什么解决办法吗

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.