index.htmlhtml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
aaa
<script src='main.js'></script>
</body>
</html>
复制代码
main.js前端
var a = 1
var b = 2
console.log(b)
复制代码
须要先安装插件Debugger for Chromevue
{
"name": "Launch index.html",
"type": "chrome",
"request": "launch",
"sourceMaps": false,
"file": "${workspaceRoot}/chrome/index.html" // 你的index.html地址
}
复制代码
这里要选咱们刚刚建立的那个配置,即name字段node
能够看到,程序运行至断点处webpack
app.jsgit
var a = 1
var b = 3
console.log(b)
复制代码
{
"type": "node",
"request": "launch",
"name": "Launch node program",
"program": "${workspaceRoot}/app.js",
"runtimeExecutable": "node"
},
复制代码
注意: 若是程序报找不到node,则须要加上下面这句github
```
"runtimeVersion": "10.14.2", // 与你当前使用的node版本一致,若是你是使用nvm进行node版本管理,则须要加上这个,不然可能找不到node
```
这种场景通常出如今:你使用nvm管理node,但没有全局安装node
复制代码
通常咱们的项目命令都写在npm script里,咱们这里讲一些怎么跑这些scriptweb
接上一节,咱们再建立一个package.json,注意里面的scriptschrome
注意9229这个端口typescript
{
"name": "npm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"debugger": "node --nolazy --inspect-brk=9229 app.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
复制代码
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeVersion": "10.14.2",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debugger" // 需跟package里定义的一致
],
"port": 9229 // 需与package里定义的inspect-brk一致
},
复制代码
接上一节
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeVersion": "10.14.2",
"runtimeExecutable": "nodemon",
"args": ["${workspaceRoot}/app.js"],
"restart": true,
"protocol": "inspector", //至关于--inspect了
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
},
复制代码
这时咱们修改b变量就能从新刷新了
rollup相似,这对于调试源码很是有用
这里咱们以app.js为入口文件
var b =1
console.log(b)
module.exports = {
entry: './app.js',
};
复制代码
cnpm i webpack webpack-cli -S
复制代码
"webpack": "webpack",
"build": "node --inspect-brk=9230 ./node_modules/.bin/webpack --config webpack.config.js",
复制代码
注意:咱们要用./node_modules/.bin/webpack来启动服务
{
"type": "node",
"runtimeVersion": "10.14.2",
"request": "launch",
"name": "webpack debugger",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "build"],
"port": 9230
},
复制代码
因为ts文件不能直接运行和调试,咱们须要先将其转为js再进行调试
cnpm i typescript ts-node -S
复制代码
其中ts-node能够直接用来执行ts文件
index.ts
const t: number = 0;
console.log(t)
复制代码
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"sourceMap": true
},
"include": ["."]
}
复制代码
{
"type": "node",
"request": "launch",
"name": "ts debugger",
"runtimeVersion": "10.14.2",
"runtimeExecutable": "node",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/index.ts"
]
},
复制代码
这里的意思是经过 node 来启动 /src/index.ts,在启动时为 node 注入一个 ts-node/register 模块,以即可以执行 ts 类型的文件
能够直接参考官网在 VS Code 中调试
以上代码能够在 github.com/repototest/… 找到
更多优秀项目