Skip to content

Commit 572b3cc

Browse files
authored
Debugging in VS Code Cookbook Recipe (#663)
* add in debugging in vscode * change tips to new format
1 parent 5aee5c8 commit 572b3cc

File tree

6 files changed

+129
-1
lines changed

6 files changed

+129
-1
lines changed

src/.vuepress/config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const sidebar = {
33
{
44
title: 'Cookbook',
55
collapsable: false,
6-
children: ['/cookbook/', '/cookbook/editable-svg-icons']
6+
children: [
7+
'/cookbook/',
8+
'/cookbook/editable-svg-icons',
9+
'/cookbook/debugging-in-vscode'
10+
]
711
}
812
],
913
guide: [
610 KB
Loading
536 KB
Loading
518 KB
Loading
Loading

src/cookbook/debugging-in-vscode.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Debugging in VS Code
2+
3+
Every application reaches a point where it's necessary to understand failures, small to large. In this recipe, we explore a few workflows for VS Code users who would like to debug their application in the browser.
4+
5+
This recipe shows how to debug [Vue CLI](https://github.com/vuejs/vue-cli) applications in VS Code as they run in the browser.
6+
7+
## Prerequisites
8+
9+
Make sure you have VS Code and the browser of your choice installed, and the latest version of the corresponding Debugger extension installed and enabled:
10+
11+
- [Debugger for Chrome](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome)
12+
- [Debugger for Firefox](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-firefox-debug)
13+
14+
Install and create a project with the [vue-cli](https://github.com/vuejs/vue-cli), following the instructions in the [Vue CLI Guide](https://cli.vuejs.org/). Change into the newly created application directory and open VS Code.
15+
16+
### Displaying Source Code in the Browser
17+
18+
Before you can debug your Vue components from VS Code, you need to update the generated Webpack config to build sourcemaps. We do this so that our debugger has a way to map the code within a compressed file back to its position in the original file. This ensures that you can debug an application even after your assets have been optimized by Webpack.
19+
20+
If you use Vue CLI 2, set or update the `devtool` property inside `config/index.js`:
21+
22+
```json
23+
devtool: 'source-map',
24+
```
25+
26+
If you use Vue CLI 3, set or update the `devtool` property inside `vue.config.js`:
27+
28+
```js
29+
module.exports = {
30+
configureWebpack: {
31+
devtool: 'source-map',
32+
},
33+
}
34+
```
35+
36+
### Launching the Application from VS Code
37+
38+
::: info
39+
We're assuming the port to be `8080` here. If it's not the case (for instance, if `8080` has been taken and Vue CLI automatically picks another port for you), just modify the configuration accordingly.
40+
:::
41+
42+
Click on the Debugging icon in the Activity Bar to bring up the Debug view, then click on the gear icon to configure a launch.json file, selecting **Chrome/Firefox: Launch** as the environment. Replace content of the generated launch.json with the corresponding configuration:
43+
44+
![Add Chrome Configuration](/images/config_add.png)
45+
46+
```json
47+
{
48+
"version": "0.2.0",
49+
"configurations": [
50+
{
51+
"type": "chrome",
52+
"request": "launch",
53+
"name": "vuejs: chrome",
54+
"url": "http://localhost:8080",
55+
"webRoot": "${workspaceFolder}/src",
56+
"breakOnLoad": true,
57+
"sourceMapPathOverrides": {
58+
"webpack:///src/*": "${webRoot}/*"
59+
}
60+
},
61+
{
62+
"type": "firefox",
63+
"request": "launch",
64+
"name": "vuejs: firefox",
65+
"url": "http://localhost:8080",
66+
"webRoot": "${workspaceFolder}/src",
67+
"pathMappings": [{ "url": "webpack:///src/", "path": "${webRoot}/" }]
68+
}
69+
]
70+
}
71+
```
72+
73+
## Setting a Breakpoint
74+
75+
1. Set a breakpoint in **src/components/HelloWorld.vue** on `line 90` where the `data` function returns a string.
76+
77+
![Breakpoint Renderer](/images/breakpoint_set.png)
78+
79+
2. Open your favorite terminal at the root folder and serve the app using Vue CLI:
80+
81+
```
82+
npm run serve
83+
```
84+
85+
3. Go to the Debug view, select the **'vuejs: chrome/firefox'** configuration, then press F5 or click the green play button.
86+
87+
4. Your breakpoint should now be hit as a new browser instance opens `http://localhost:8080`.
88+
89+
![Breakpoint Hit](/images/breakpoint_hit.png)
90+
91+
## Alternative Patterns
92+
93+
### Vue Devtools
94+
95+
There are other methods of debugging, varying in complexity. The most popular and simple of which is to use the excellent Vue.js devtools [for Chrome](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd) and [for Firefox](https://addons.mozilla.org/en-US/firefox/addon/vue-js-devtools/). Some of the benefits of working with the devtools are that they enable you to live-edit data properties and see the changes reflected immediately. The other major benefit is the ability to do time travel debugging for Vuex.
96+
97+
![Devtools Timetravel Debugger](/images/devtools-timetravel.gif)
98+
99+
Please note that if the page uses a production/minified build of Vue.js (such as the standard link from a CDN), devtools inspection is disabled by default so the Vue pane won't show up. If you switch to an unminified version, you may have to give the page a hard refresh to see them.
100+
101+
### Simple Debugger Statement
102+
103+
The example above has a great workflow. However, there is an alternative option where you can use the [native debugger statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) directly in your code. If you choose to work this way, it's important that you remember to remove the statements when you're done.
104+
105+
```js
106+
<script>
107+
export default {
108+
data() {
109+
return {
110+
message: ''
111+
}
112+
},
113+
mounted() {
114+
const hello = 'Hello World!'
115+
debugger
116+
this.message = hello
117+
}
118+
};
119+
</script>
120+
```
121+
122+
## Acknowledgements
123+
124+
This recipe was based on a contribution from [Kenneth Auchenberg](https://twitter.com/auchenberg), [available here](https://github.com/Microsoft/VSCode-recipes/tree/master/vuejs-cli).

0 commit comments

Comments
 (0)