【问题标题】:Visual Studio Code Chrome Debugger doesn't set breakpoints inside generator function in ReactVisual Studio Code Chrome 调试器不会在 React 的生成器函数中设置断点
【发布时间】:2018-05-31 22:20:00
【问题描述】:

我正在使用最新的 Visual Studio Code 和 Chrome 调试器扩展,我的代码是 React SPA。
当我试图在generator functions 中设置断点时 (即 function* ),断点被移动到函数的顶部,我无法在我想要的时候停止。

Step Over 也不起作用,但将我移至某个低级库。

对于正常功能,断点工作正常。

我错过了什么吗?这是已知的限制还是错误?是否有任何工具(例如 Edge/Firefox 或原生 Chrome 调试器)可以更好地调试生成器功能?

【问题讨论】:

标签: reactjs visual-studio-code vscode-debugger chrome-debugging


【解决方案1】:

我目前看到同样的情况,正在寻找解决方案。这是我目前发现的,可能会有所帮助。

在我们的例子中,我们使用 babel 转译我们的代码,并且查看 babel 生成的代码,我们可以看到生成器正在为 @babel/preset-env 读取的 browerslist 所针对的浏览器进行转译。因此,作为初始测试,我们从开发版本中删除了@babel/preset-env,并在 Chrome 70 中进行了本地测试。生成器不再被转译,我们可以在 VSCode 中成功设置断点。

因此,对我们而言,解决方案是不为开发版本转译,而是为我们的目标浏览器转译以用于生产版本。

作为参考,这里是我们用来测试这个解决方案的 babel 配置:

module.exports = {
  presets: [
    '@babel/preset-react'
  ],
  plugins: [
    [
      '@babel/plugin-proposal-object-rest-spread',
      {
        useBuiltIns: true
      }
    ],
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-transform-modules-commonjs'
  ],
  env: {
    production: {
      presets: [
        [
          '@babel/preset-env',
          {
            debug: false,
            useBuiltIns: 'usage'
          }
        ]
      ],
      plugins: [
        '@babel/plugin-transform-runtime'
      ]
    }
  }
}

我们可以在任何我们想要针对我们支持的浏览器的生产 npm 脚本中设置BABEL_ENV=production。

{
  "name": "testapp-ui",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "rm -rf dist && mkdir dist && NODE_ENV=production BABEL_ENV=production npm run build:webpack",
    "build:dev": "rm -rf dist && mkdir dist && NODE_ENV=development npm run build:webpack",
    "build:webpack": "webpack --progress --debug",
    ...

【讨论】:

    【解决方案2】:

    感谢Bernard Leech's answer,我更新了 .babelrc 配置文件(https://babeljs.io/docs/en/configuration 中描述的可能格式之一)。
    它允许在开发环境中设置断点。

    .babelrc 中更改的行被注释掉:

    {
        "presets": [
          //[
          // "es2015",
          // {
          //   "modules": false
          // }
          //],
          "react",
          "stage-0"
        ],
        "env": {
          "test": {
            "presets": ["react"]
           "prod": {
            "presets": [["es2015"], "react"]
          }
        }
      } 
    

    要为 dev 和 prod 使用不同的配置,您应该设置例如 BABEL_ENV=prod 并且在“env”元素中有不同的部分(来自 How do you configure babel to run with different configurations in different environments)

    相关链接:
    How to debug ES6 NodeJS with VSCode
    Debugging with babel-register + NodeJs does not work #5728

    【讨论】:

      猜你喜欢
      • 2019-03-19
      • 2020-05-09
      • 2019-09-30
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多