Skip to content

Commit 9b2a7ec

Browse files
yamachusevenc-nanashiHiroshiba
authored
ViteでビルドをしながらVSCodeでDebugを行えるようにする (#1221)
* inline sourcemapをserveなどのDebugビルドでは有効にする * debug launch用のtemplateファイルを追加 * defineConfigから渡されるmodeでsourcemapの出し分けを判定する * Update .vscode/launch.template.json Co-authored-by: Nanashi. <[email protected]> * Update .vscode/launch.template.json Co-authored-by: Hiroshiba <[email protected]> * render -> renderer * Electronの更新機サンプルにあるように、9222にremote portを変更 * READMEにそれとなくVS Codeでデバッグ実行できることを載せておく * vite関係のimportをまとめる * Update vite.config.ts Co-authored-by: Nanashi. <[email protected]> * developmentとtest環境でのみsourcemapを出力する --------- Co-authored-by: Nanashi. <[email protected]> Co-authored-by: Hiroshiba <[email protected]>
1 parent 82c074e commit 9b2a7ec

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

.vscode/launch.template.json

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Attach to Renderer Process",
9+
// NOTE: background.tsで指定しているremote-debugging-port
10+
"port": 9222,
11+
"request": "attach",
12+
"type": "chrome",
13+
"webRoot": "${workspaceFolder}",
14+
"timeout": 20000, // 20 * 1000 ms程度あればビルド時間は間に合うはず
15+
},
16+
{
17+
"name": "Launch Electron Main Process via NPM",
18+
"request": "launch",
19+
"runtimeArgs": [
20+
"run",
21+
"electron:serve"
22+
],
23+
"runtimeExecutable": "npm",
24+
"skipFiles": [
25+
"<node_internals>/**"
26+
],
27+
"type": "node"
28+
},
29+
{
30+
"name": "Attach by Process ID",
31+
// .bin viteを指定するとElectronのMain Processのデバッグが可能
32+
"processId": "${command:PickProcess}",
33+
"request": "attach",
34+
"skipFiles": [
35+
"<node_internals>/**"
36+
],
37+
"type": "node"
38+
},
39+
],
40+
"compounds": [
41+
{
42+
"name": "Launch Electron Main/Renderer",
43+
"configurations": ["Attach to Renderer Process", "Launch Electron Main Process via NPM"],
44+
"stopAll": true
45+
}
46+
]
47+
}

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ npx openapi-generator-cli generate \
149149
npm run fmt
150150
```
151151

152+
## VS Code でのデバッグ実行
153+
154+
npm scripts の `serve``electron:serve` などの開発ビルド下では、ビルドに使用している vite で sourcemap を出力するため、ソースコードと出力されたコードの対応付けが行われます。
155+
156+
`.vscode/launch.template.json` をコピーして `.vscode/launch.json` を作成することで、開発ビルドを VS Code から実行し、デバッグを可能にするタスクが有効になります。
157+
152158
## ライセンス
153159

154160
LGPL v3 と、ソースコードの公開が不要な別ライセンスのデュアルライセンスです。

src/background.ts

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type SingleInstanceLockData = {
4949
const isDevelopment = import.meta.env.DEV;
5050
const isTest = import.meta.env.MODE === "test";
5151

52+
if (isDevelopment) {
53+
app.commandLine.appendSwitch("remote-debugging-port", "9222");
54+
}
55+
5256
let suffix = "";
5357
if (isTest) {
5458
suffix = "-test";

vite.config.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ import electron from "vite-plugin-electron";
77
import tsconfigPaths from "vite-tsconfig-paths";
88
import vue from "@vitejs/plugin-vue";
99
import checker from "vite-plugin-checker";
10-
import { defineConfig } from "vite";
10+
import { BuildOptions, defineConfig } from "vite";
1111
import { quasar } from "@quasar/vite-plugin";
1212

1313
rmSync(path.resolve(__dirname, "dist"), { recursive: true, force: true });
1414

1515
const isElectron = process.env.VITE_IS_ELECTRON === "true";
1616

1717
export default defineConfig((options) => {
18+
const shouldEmitSourcemap = ["development", "test"].includes(options.mode);
19+
const sourcemap: BuildOptions["sourcemap"] = shouldEmitSourcemap
20+
? "inline"
21+
: false;
1822
return {
1923
root: path.resolve(__dirname, "src"),
2024
build: {
2125
outDir: path.resolve(__dirname, "dist"),
2226
chunkSizeWarningLimit: 10000,
27+
sourcemap,
2328
},
2429
publicDir: path.resolve(__dirname, "public"),
2530
css: {
@@ -74,6 +79,7 @@ export default defineConfig((options) => {
7479
plugins: [tsconfigPaths({ root: __dirname })],
7580
build: {
7681
outDir: path.resolve(__dirname, "dist"),
82+
sourcemap,
7783
},
7884
},
7985
}),

0 commit comments

Comments
 (0)