Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViteでビルドをしながらVSCodeでDebugを行えるようにする #1221

Merged
merged 13 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .vscode/launch.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Renderer Process",
// NOTE: background.tsで指定しているremote-debugging-port
"port": 9222,
"request": "attach",
"type": "chrome",
"webRoot": "${workspaceFolder}",
"timeout": 20000, // 20 * 1000 ms程度あればビルド時間は間に合うはず
},
{
"name": "Launch Electron Main Process via NPM",
"request": "launch",
"runtimeArgs": [
"run",
"electron:serve"
],
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
{
"name": "Attach by Process ID",
// .bin viteを指定するとElectronのMain Processのデバッグが可能
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
],
"compounds": [
{
"name": "Launch Electron Main/Renderer",
"configurations": ["Attach to Renderer Process", "Launch Electron Main Process via NPM"],
"stopAll": true
}
]
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ npx openapi-generator-cli generate \
npm run fmt
```

## VS Code でのデバッグ実行

npm scripts の `serve` や `electron:serve` などの開発ビルド下では、ビルドに使用している vite で sourcemap を出力するため、ソースコードと出力されたコードの対応付けが行われます。

`.vscode/launch.template.json` をコピーして `.vscode/launch.json` を作成することで、開発ビルドを VS Code から実行し、デバッグを可能にするタスクが有効になります。

## ライセンス

LGPL v3 と、ソースコードの公開が不要な別ライセンスのデュアルライセンスです。
Expand Down
4 changes: 4 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type SingleInstanceLockData = {
const isDevelopment = import.meta.env.DEV;
const isTest = import.meta.env.MODE === "test";

if (isDevelopment) {
app.commandLine.appendSwitch("remote-debugging-port", "9222");
}

let suffix = "";
if (isTest) {
suffix = "-test";
Expand Down
6 changes: 5 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import electron from "vite-plugin-electron";
import tsconfigPaths from "vite-tsconfig-paths";
import vue from "@vitejs/plugin-vue";
import checker from "vite-plugin-checker";
import { defineConfig } from "vite";
import { BuildOptions, defineConfig } from "vite";
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

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

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

export default defineConfig((options) => {
const isDevelopment = options.mode === "development";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テスト時もオンにしても良さそう?(テスト環境だとmodeが"test"になる)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テスト環境は本番に近い内容のが良いという考え方はあるかも。でもsource mapは別かも。
Webフロントめちゃ詳しそうな @yamachu さん的にはどうでしょ 👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sourcemapの出力ぐらいで特に挙動が変わるみたいのはないので、e2eテストとかをしている間にどうしてもわからん壊れ方してるのを調査したいって時には役に立つかもですね
productionで出なければいいので、変数名を変えてテスト環境でも出していいかも

const sourcemap: BuildOptions["sourcemap"] = isDevelopment ? "inline" : false;
return {
root: path.resolve(__dirname, "src"),
build: {
outDir: path.resolve(__dirname, "dist"),
chunkSizeWarningLimit: 10000,
sourcemap,
},
publicDir: path.resolve(__dirname, "public"),
css: {
Expand Down Expand Up @@ -71,6 +74,7 @@ export default defineConfig((options) => {
plugins: [tsconfigPaths({ root: __dirname })],
build: {
outDir: path.resolve(__dirname, "dist"),
sourcemap,
},
},
}),
Expand Down