简体   繁体   English

使用 VScode 在 React 项目中调试独立的 Typescript 文件

[英]Debugging standalone Typescript file in React project using VScode

I've a React project created using create react app .我有一个使用create react app创建的 React 项目。 Sometimes I want to play with typescript code in a separate file and debug it.有时我想在单独的文件中使用打字稿代码并调试它。

VSCode requires the generated .js folder to be set in the debug configuration, otherwise the following message appears. VSCode 需要在调试配置中设置生成的 .js 文件夹,否则会出现如下提示。

Cannot launch program 'File.ts' setting the 'outFiles' attribute might help.

Or if set to "outFiles": ["${fileDirname}/*.js"]:或者如果设置为 "outFiles": ["${fileDirname}/*.js"]:

Cannot launch program 'File.ts' because corresponding JavaScript cannot be found.

I can't find where the project is generating the .js files.我找不到项目生成 .js 文件的位置。 It seems create react app uses webpack, which should use ts-loader .似乎 create react app 使用 webpack,它应该使用ts-loader It generates a bundle.js file, but I can't even find it in the workspace folder either.它生成了一个 bundle.js 文件,但我什至在工作区文件夹中也找不到它。

For reference, my full launch.json.作为参考,我的完整launch.json。 Any help is appreciated.任何帮助表示赞赏。 Thanks!谢谢!

{
   "version": "0.2.0",
   "configurations": [   
      // Not working  
      {
         "type": "node",
         "request": "launch",
         "name": "Debug File",
         "program": "${file}",
         "outFiles": ["${fileDirname}/*.js"]
      },
      {
         "type": "node",
         "name": "Run tests",
         "request": "launch",
         "args": [
            "test",
            "--runInBand"
         ],
         "cwd": "${workspaceFolder}",
         "console": "integratedTerminal",
         "internalConsoleOptions": "neverOpen",
         "disableOptimisticBPs": true,
         "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/react-scripts",
         "protocol": "inspector"
      },
      {
         "name": "Chrome",
         "type": "chrome",
         "request": "launch",
         "url": "http://localhost:3000",
         "webRoot": "${workspaceRoot}/src"
     }
   ]
}

You can either build the project as a .ts project and support the legacy js files, or you can include the typescript files output folder so you compile the .ts into .js, and the .js module will be included in your React app.您可以将项目构建为 .ts 项目并支持旧版 js 文件,也可以包含 typescript 文件输出文件夹,以便将 .ts 编译为 .js,并且 .js 模块将包含在您的 React 应用程序中。 But you cannot just drop a ts file in a js project and expect React to make that connection for you.但是你不能只是在 js 项目中删除一个 ts 文件,然后期望 React 为你建立连接。

To keep your workspace organized, I recommend move the ts to a separate folder, init that folder as a ts project and include it back in your React project as a library (even if it's a single file)为了让您的工作区井井有条,我建议将 ts 移动到一个单独的文件夹,将该文件夹初始化为 ts 项目并将其作为库包含在您的 React 项目中(即使它是单个文件)

You can then use testing frameworks such as mocha + chai and jest to test it.然后可以使用mocha + chaijest等测试框架进行测试。 Both mocha and jest can test your .ts directly without requiring your to compile it (they're doing it for you, using packages such as jest-ts ) mocha 和 jest 都可以直接测试你的 .ts 而不需要你编译它(他们正在为你做,使用诸如jest-ts包)

Lerna or Yarn workspaces can help with the linkage, and vs-code can be easily configured to support multiple projects (packages) in the workspace. LernaYarn workspaces可以帮助进行链接,并且可以轻松配置 vs-code 以支持工作区中的多个项目(包)。 including for testing.包括用于测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM