Skip to content

Commit 17866c3

Browse files
author
Andreas Bartho
committed
Extended README.md
1 parent b6693a0 commit 17866c3

File tree

6 files changed

+126
-8
lines changed

6 files changed

+126
-8
lines changed

.vscode/launch.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
"outputCapture": "std",
1818
"sourceMaps": true,
1919
"resolveSourceMapLocations": [
20-
"${workspaceFolder}/**", //use source maps for files in workspace folder
21-
"!**/node_modules/**" //but ignore everything in the node_modules folder
20+
"${workspaceFolder}/**", //use source maps for files in workspace folder
21+
"!**/node_modules/**" //but ignore everything in the node_modules folder
2222
],
23-
"preLaunchTask": "npm: compile" //recompile before debugging (execute the compile script defined in package.json)
23+
"preLaunchTask": "npm: compile" //recompile before debugging (execute the compile script defined in package.json)
2424
},
2525
{
2626
"name": "Electron: Renderer",
27-
"type": "chrome", //use the Debugger for Chrome (extension)
27+
"type": "chrome", //use the Debugger for Chrome (extension)
2828
"request": "attach",
29-
"port": 9223, //use debug port opened in Electron: Main configuration
29+
"port": 9223, //use debug port opened in Electron: Main configuration
3030
"webRoot": "${workspaceFolder}",
3131
"timeout": 30000
3232
}
3333
],
34-
"compounds": [ //launch multiple configurations concurrently
34+
"compounds": [ //launch multiple configurations concurrently
3535
{
3636
"name": "Electron: All",
3737
"configurations": [

README.md

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,124 @@
11
# electron-typescript-vscode
22

3-
The purpose of this project is to provide a minimal working example of how to set up Visual Studio Code to develop an Electron app in Typescript, including debug support.
3+
This project provides a minimal working example of how to set up Visual Studio Code to develop an Electron app in Typescript, including debug support.
44

5-
Visual Studio Code comes with Electron support and Typescript support out of the box, but bringing the two together requires some deeper understanding of the internals that beginners may not have. This repository contains a Visual Studio Code project with the bare minimum configuration to show how Visual Studio Code, Node, Electron and Typescript must be configured to work together. I aim to keep it up to date as new versions of the used tools and libraries are released.
5+
Visual Studio Code comes with Electron support and Typescript support out of the box, but bringing the two together requires some knowledge of the available configuration options that beginners may not have. This repository contains a Visual Studio Code project with the bare minimum configuration to show how Visual Studio Code, Node, Electron and Typescript must be configured to work together.
66

7+
The latest example has been created and tested on Linux with
8+
- Node v15.6.0
9+
- Electron v11.2.3
10+
- Typescript v4.1.5
11+
- Visual Studio Code v1.53.0
12+
- Debugger for Chrome extension v4.12.12
13+
14+
## Install application
15+
```sh
16+
# Clone repository
17+
git clone https://github.com/abartho/electron-typescript-vscode.git
18+
19+
# Change into directory
20+
cd electron-typescript-vscode
21+
22+
# Install dependencies
23+
npm install
24+
25+
# Optional: test if the application is running
26+
npm start
27+
```
28+
29+
## Set up Visual Studio Code and start debugging
30+
31+
1) Install the "Debugger for Chrome" extension.
32+
33+
It is required for debugging renderer processes.
34+
35+
![Debugger for Chrome](./doc/install_debugger_for_chrome.png)
36+
37+
1) In the Run view, select the "Electron: All" configuration.
38+
39+
This is a compound configuration that will start both the "Electron: Main" and "Electron: Renderer" configurations.
40+
41+
![Select configuration](./doc/select_configuration.png)
42+
43+
1) Set a breakpoint in `src/main.ts` and `src/renderer.ts`
44+
45+
1) Run the "Run" -> "Start Debugging" command (<kbd>F5</kbd>)
46+
- The breakpoint in the `main.ts` will be hit.
47+
- Click Continue (<kbd>F5</kbd>)
48+
- In the Electron example app, click the "Turn page red" button.
49+
- The breakpoint in `renderer.ts` should be hit.
50+
51+
- If the breakpoint is not hit and marked as unbound instead:
52+
![Disable Preview JavaScript Debugger](./doc/unbound_breakpoint.png)
53+
54+
you have likely come across a bug in the new JavaScript debugger for Node.js and Chrome, which is currently in preview and enabled by default. It is used by recent versions of the "Debugger for Chrome" extension. Disabling the preview debugger will solve the problem (https://github.com/microsoft/vscode/issues/102493).
55+
- Open Settings (<kbd>Ctrl+,</kbd>)
56+
- Search for `debug.javascript.usePreview`
57+
- Set option to false
58+
![Disable Preview JavaScript Debugger](./doc/disable_preview_javascript_debugger.png)
59+
- Run the "Electron: All" configuration again
60+
61+
62+
63+
## How does it work?
64+
65+
Electron has two kinds of processes: a main process and renderer processes (one for each tab). They need different launch configurations, which are shown below. The code snippets are taken from the [launch configuration](.vscode/launch.json).
66+
67+
### Main process
68+
The main process can be debugged with the pwa-node debugger that ships with Visual Studio Code.
69+
70+
The launch configuration looks like this:
71+
72+
```json
73+
{
74+
"name": "Electron: Main",
75+
"type": "pwa-node", //use the node debugger that comes with VS Code
76+
"request": "launch",
77+
"cwd": "${workspaceFolder}",
78+
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
79+
"runtimeArgs": [
80+
"--remote-debugging-port=9223" //open debugging port for renderer process
81+
],
82+
"args" : ["."],
83+
"outputCapture": "std",
84+
"sourceMaps": true,
85+
"resolveSourceMapLocations": [
86+
"${workspaceFolder}/**", //use source maps for files in workspace folder
87+
"!**/node_modules/**" //but ignore everything in the node_modules folder
88+
],
89+
"preLaunchTask": "npm: compile" //recompile before debugging (execute the compile script defined in package.json)
90+
}
91+
```
92+
In the `sourceMaps` and `resolveSourceMapLocations` sections, we enable the creation of source maps for our code. Source maps must be generated to enable the debugger to map locations inside the JavaScript code back to TypeScript.
93+
94+
In the `runtimeArgs` section, we open a port for the renderer process. There is a counterpart defined in the renderer process configuration as shown below.
95+
96+
### Renderer process
97+
Debugging a renderer process requires the "Debugger for Chrome" extension.
98+
99+
```json
100+
{
101+
"name": "Electron: Renderer",
102+
"type": "chrome", //use the Debugger for Chrome (extension)
103+
"request": "attach",
104+
"port": 9223, //use debug port opened in Electron: Main configuration
105+
"webRoot": "${workspaceFolder}",
106+
"timeout": 30000
107+
}
108+
```
109+
In the `port` section, we specify the debugging port that we chose in the main process configuration.
110+
111+
### Compound configuration
112+
Visual Studio Code can only run a single configuration at a time, but we need to run the Main and the Renderer configurations at the same time. The solution are compound configurations (found in [vscode-recipes](https://github.com/Microsoft/vscode-recipes/tree/master/Electron))
113+
114+
```json
115+
"compounds": [ //launch multiple configurations concurrently
116+
{
117+
"name": "Electron: All",
118+
"configurations": [
119+
"Electron: Main",
120+
"Electron: Renderer"
121+
]
122+
}
123+
]
124+
```
22.6 KB
Loading
64.8 KB
Loading

doc/select_configuration.png

15.3 KB
Loading

doc/unbound_breakpoint.png

18.7 KB
Loading

0 commit comments

Comments
 (0)