|
1 | 1 | # electron-typescript-vscode |
2 | 2 |
|
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. |
4 | 4 |
|
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. |
6 | 6 |
|
| 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 | +  |
| 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 | +  |
| 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 | +  |
| 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 | +  |
| 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 | +``` |
0 commit comments