【发布时间】:2020-03-30 12:05:18
【问题描述】:
我有一个使用 create react app 创建的 React 项目。有时我想在一个单独的文件中使用打字稿代码并对其进行调试。
VSCode需要在调试配置中设置生成的.js文件夹,否则会出现如下提示。
Cannot launch program 'File.ts' setting the 'outFiles' attribute might help.
或者如果设置为 "outFiles": ["${fileDirname}/*.js"]:
Cannot launch program 'File.ts' because corresponding JavaScript cannot be found.
我找不到项目生成 .js 文件的位置。似乎创建反应应用程序使用 webpack,它应该使用ts-loader。它会生成一个 bundle.js 文件,但我什至在工作区文件夹中也找不到它。
作为参考,我的完整launch.json。任何帮助表示赞赏。谢谢!
{
"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"
}
]
}
【问题讨论】:
-
你有 Typescript.json 文件吗?
-
如果它是一个与节点分开启动的“独立” ts 文件,那么 CRA/webpack 构建到底起什么作用?前者是节点调试,后者是客户端调试。
-
我有一个 tsconfig.json,可以在这里找到。这是由创建反应应用程序自动创建的:gist.github.com/ricardoekm/e69bc615d95903d86b95c95571f00bba。关于 Webpack,我的猜测是它直接从我的源文件夹加载打字稿文件,而不是让 ts 编译器编译它们然后加载它。这增加了热重载能力。
标签: reactjs typescript visual-studio-code create-react-app