VS Code launch.json正则替换路径实现TS源文件对应dist目录Mocha调试
解决方案
方案1:使用VS Code原生变量替换(无需额外扩展)
VS Code从1.64版本开始内置支持变量正则替换能力,不需要安装任何第三方扩展即可实现路径裁剪替换需求。你可以直接修改launch.json中的参数即可:
{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Mocha Current File", "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", "args": [ "--timeout", "999999", "--colors", "${workspaceFolder}/${relativeFile/^src\\//dist\\//i/\\.ts$/.js/i}" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" } ] }
上述配置中的变量替换规则会自动完成两个转换:
- 匹配相对路径开头的
src/前缀,替换为dist/ - 匹配文件后缀的
.ts,替换为.js
刚好对应你src源码路径到dist编译输出路径的映射规则。
方案2:使用Command Variable扩展实现(兼容旧版本VS Code)
你使用的Command Variable扩展实际支持任意正则替换,并非只能向上裁剪目录,你可以通过扩展提供的转换命令完成src前缀裁剪:
{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Mocha Current File", "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", "args": [ "--timeout", "999999", "--colors", "${workspaceFolder}/${input:targetTestFile}" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" } ], "inputs": [ { "id": "targetTestFile", "type": "command", "command": "extension.commandvariable.transform", "args": { "text": "${relativeFile}", "find": "^src/(.*)\\.ts$", "replace": "dist/$1.js" } } ] }
两种方案都可以实现打开src目录下的TS源文件时,自动匹配对应dist目录下的编译后JS文件运行Mocha调试。
内容的提问来源于stack exchange,提问作者J. Reynolds
相关产品推荐
相关产品推荐

