From e5fcdb98e6849bb720ee6162f00c329f8e771330 Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Wed, 16 Oct 2024 07:53:40 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E9=96=8B=E7=99=BA=E3=82=B5?= =?UTF-8?q?=E3=83=BC=E3=83=90=E3=83=BC=E8=B5=B7=E5=8B=95=E6=99=82=E3=81=AB?= =?UTF-8?q?=E3=82=A8=E3=83=87=E3=82=A3=E3=82=BF=E3=82=92=E8=B5=B7=E5=8B=95?= =?UTF-8?q?=E3=81=97=E3=81=AA=E3=81=84=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.template.json | 38 ++++++++++++++++++++++++++---- .vscode/tasks.template.json | 28 ++++++++++++++++++++++ src/backend/electron/ipc.ts | 4 ++-- src/backend/electron/main.ts | 4 ++-- src/vite-env.d.ts | 1 + tests/e2e/electron/example.spec.ts | 4 ---- vite.config.mts | 9 +++++-- 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 .vscode/tasks.template.json diff --git a/.vscode/launch.template.json b/.vscode/launch.template.json index 969c53d659..b863c0728e 100644 --- a/.vscode/launch.template.json +++ b/.vscode/launch.template.json @@ -10,7 +10,11 @@ "port": 9222, "request": "attach", "type": "chrome", - "webRoot": "${workspaceFolder}", + "webRoot": "${workspaceFolder}/src", + "resolveSourceMapLocations": [ + "${workspaceFolder}/**", + "!**/node_modules/**" + ], "timeout": 20000, // 20 * 1000 ms程度あればビルド時間は間に合うはず }, { @@ -26,6 +30,21 @@ ], "type": "node" }, + { + "name": "Launch Electron without electron:serve", + "request": "launch", + "type": "node", + "runtimeExecutable": "npx", + "args": [ + "electron", + ".", + "--no-sandbox" + ], + "preLaunchTask": "Electron Serve Only", + "skipFiles": [ + "/**" + ] + }, { "name": "Attach by Process ID", // .bin viteを指定するとElectronのMain Processのデバッグが可能 @@ -35,13 +54,24 @@ "/**" ], "type": "node" - }, + } ], "compounds": [ { "name": "Launch Electron Main/Renderer", - "configurations": ["Attach to Renderer Process", "Launch Electron Main Process via NPM"], + "configurations": [ + "Attach to Renderer Process", + "Launch Electron Main Process via NPM" + ], + "stopAll": true + }, + { + "name": "Launch Electron Main/Renderer without electron:serve", + "configurations": [ + "Attach to Renderer Process", + "Launch Electron without electron:serve" + ], "stopAll": true } ] -} \ No newline at end of file +} diff --git a/.vscode/tasks.template.json b/.vscode/tasks.template.json new file mode 100644 index 0000000000..1d6bcafff3 --- /dev/null +++ b/.vscode/tasks.template.json @@ -0,0 +1,28 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Electron Serve Only", + "type": "npm", + "script": "electron:serve", + "options": { + "env": { + "SKIP_LAUNCH_EDITOR": "1" + } + }, + "isBackground": true, + "problemMatcher": { + "pattern": { + "regexp": "" + }, + "background": { + "activeOnStart": true, + "beginsPattern": "building for development\\.\\.\\.", + "endsPattern": "main process build is complete\\." + } + } + } + ] +} diff --git a/src/backend/electron/ipc.ts b/src/backend/electron/ipc.ts index 588af9de98..02df0c6032 100644 --- a/src/backend/electron/ipc.ts +++ b/src/backend/electron/ipc.ts @@ -68,8 +68,8 @@ export const ipcMainSendProxy = new Proxy( const validateIpcSender = (event: IpcMainInvokeEvent) => { let isValid: boolean; const senderUrl = new URL(event.senderFrame.url); - if (process.env.VITE_DEV_SERVER_URL != undefined) { - const devServerUrl = new URL(process.env.VITE_DEV_SERVER_URL); + if (import.meta.env.VITE_DEV_SERVER_URL != undefined) { + const devServerUrl = new URL(import.meta.env.VITE_DEV_SERVER_URL); isValid = senderUrl.origin === devServerUrl.origin; } else { isValid = senderUrl.protocol === "app:"; diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index 42a908ecca..57bd91b9f9 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -145,7 +145,7 @@ protocol.registerSchemesAsPrivileged([ { scheme: "app", privileges: { secure: true, standard: true, stream: true } }, ]); -const firstUrl = process.env.VITE_DEV_SERVER_URL ?? "app://./index.html"; +const firstUrl = import.meta.env.VITE_DEV_SERVER_URL ?? "app://./index.html"; // engine const vvppEngineDir = path.join(app.getPath("userData"), "vvpp-engines"); @@ -280,7 +280,7 @@ async function createWindow() { } // ソフトウェア起動時はプロトコルを app にする - if (process.env.VITE_DEV_SERVER_URL == undefined) { + if (import.meta.env.VITE_DEV_SERVER_URL == undefined) { protocol.handle("app", (request) => { // 読み取り先のファイルがインストールディレクトリ内であることを確認する // ref: https://www.electronjs.org/ja/docs/latest/api/protocol#protocolhandlescheme-handler diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 2112976e79..c7fbf04555 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -5,6 +5,7 @@ interface ImportMetaEnv { readonly VITE_APP_NAME: string; readonly VITE_APP_VERSION: string; readonly VITE_DEFAULT_ENGINE_INFOS: string; + readonly VITE_DEV_SERVER_URL: string | undefined; readonly VITE_OFFICIAL_WEBSITE_URL: string; readonly VITE_LATEST_UPDATE_INFOS_URL: string; readonly VITE_GTM_CONTAINER_ID: string; diff --git a/tests/e2e/electron/example.spec.ts b/tests/e2e/electron/example.spec.ts index 56fb7733cf..9829fc75c0 100644 --- a/tests/e2e/electron/example.spec.ts +++ b/tests/e2e/electron/example.spec.ts @@ -44,10 +44,6 @@ test("起動したら「利用規約に関するお知らせ」が表示され const app = await electron.launch({ args: ["."], timeout: process.env.CI ? 0 : 60000, - env: { - ...process.env, - VITE_DEV_SERVER_URL: "http://localhost:7357", - }, }); const sut = await app.firstWindow({ diff --git a/vite.config.mts b/vite.config.mts index 69cf4cc135..6327f0a5cb 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -45,6 +45,8 @@ export default defineConfig((options) => { const sourcemap: BuildOptions["sourcemap"] = shouldEmitSourcemap ? "inline" : false; + const launchEditor = + process.env.SKIP_LAUNCH_EDITOR !== "1" && options.mode !== "test"; return { root: path.resolve(__dirname, "src"), envDir: __dirname, @@ -87,7 +89,8 @@ export default defineConfig((options) => { entry: "./src/backend/electron/main.ts", // ref: https://github.com/electron-vite/vite-plugin-electron/pull/122 onstart: ({ startup }) => { - if (options.mode !== "test") { + console.log("main process build is complete."); + if (launchEditor) { void startup([".", "--no-sandbox"]); } }, @@ -103,7 +106,9 @@ export default defineConfig((options) => { // ref: https://electron-vite.github.io/guide/preload-not-split.html entry: "./src/backend/electron/preload.ts", onstart({ reload }) { - reload(); + if (launchEditor) { + reload(); + } }, vite: { plugins: [tsconfigPaths({ root: __dirname })], From d80b4d6e4dc57e5b16ac95983f4e6bb4e34545f9 Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:28:58 +0900 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E5=90=8D=E5=89=8D=E3=82=92=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.template.json | 10 ++++++---- .vscode/tasks.template.json | 5 +++-- vite.config.mts | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.template.json b/.vscode/launch.template.json index b863c0728e..4e0ed85bf7 100644 --- a/.vscode/launch.template.json +++ b/.vscode/launch.template.json @@ -31,7 +31,8 @@ "type": "node" }, { - "name": "Launch Electron without electron:serve", + "name": "Launch Electron Main Process without hot reload", + // Electronのみを起動 "request": "launch", "type": "node", "runtimeExecutable": "npx", @@ -40,7 +41,8 @@ ".", "--no-sandbox" ], - "preLaunchTask": "Electron Serve Only", + // 事前にElectronを起動せずにバックグラウンドで"electron:serve"を実行する + "preLaunchTask": "Electron Serve without Launch Electron", "skipFiles": [ "/**" ] @@ -66,10 +68,10 @@ "stopAll": true }, { - "name": "Launch Electron Main/Renderer without electron:serve", + "name": "Launch Electron Main/Renderer without hot reload", "configurations": [ "Attach to Renderer Process", - "Launch Electron without electron:serve" + "Launch Electron Main Process without hot reload" ], "stopAll": true } diff --git a/.vscode/tasks.template.json b/.vscode/tasks.template.json index 1d6bcafff3..0bc1568c4f 100644 --- a/.vscode/tasks.template.json +++ b/.vscode/tasks.template.json @@ -4,12 +4,13 @@ "version": "2.0.0", "tasks": [ { - "label": "Electron Serve Only", + "label": "Electron Serve without Launch Electron", + // Electronを起動せずにバックグラウンドで"electron:serve"を実行する "type": "npm", "script": "electron:serve", "options": { "env": { - "SKIP_LAUNCH_EDITOR": "1" + "SKIP_LAUNCH_ELECTRON": "1" } }, "isBackground": true, diff --git a/vite.config.mts b/vite.config.mts index 6327f0a5cb..63408d5cb6 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -46,7 +46,7 @@ export default defineConfig((options) => { ? "inline" : false; const launchEditor = - process.env.SKIP_LAUNCH_EDITOR !== "1" && options.mode !== "test"; + process.env.SKIP_LAUNCH_ELECTRON !== "1" && options.mode !== "test"; return { root: path.resolve(__dirname, "src"), envDir: __dirname, From cc9fb829d411d66bfb1ad3c9e97c0163bb534373 Mon Sep 17 00:00:00 2001 From: Hiroshiba Kazuyuki Date: Tue, 29 Oct 2024 04:14:40 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=E3=83=89=E3=82=AD=E3=83=A5=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88=E8=B6=B3=E3=81=97=E3=81=9F=E3=82=8A=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.template.json | 12 ++++++------ .vscode/tasks.template.json | 3 +-- vite.config.mts | 14 +++++++++----- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.template.json b/.vscode/launch.template.json index 4e0ed85bf7..483689b55b 100644 --- a/.vscode/launch.template.json +++ b/.vscode/launch.template.json @@ -6,14 +6,13 @@ "configurations": [ { "name": "Attach to Renderer Process", - // NOTE: background.tsで指定しているremote-debugging-port - "port": 9222, + "port": 9222, // NOTE: background.tsで指定しているremote-debugging-port "request": "attach", "type": "chrome", "webRoot": "${workspaceFolder}/src", "resolveSourceMapLocations": [ - "${workspaceFolder}/**", - "!**/node_modules/**" + "${workspaceFolder}/**", + "!**/node_modules/**" ], "timeout": 20000, // 20 * 1000 ms程度あればビルド時間は間に合うはず }, @@ -31,8 +30,9 @@ "type": "node" }, { + // 直接Electronのみを起動し、バックグラウンドで"electron:serve"を実行する + // NOTE: ホットリロードできない代わりに、デバッグ起動が軽い "name": "Launch Electron Main Process without hot reload", - // Electronのみを起動 "request": "launch", "type": "node", "runtimeExecutable": "npx", @@ -41,7 +41,6 @@ ".", "--no-sandbox" ], - // 事前にElectronを起動せずにバックグラウンドで"electron:serve"を実行する "preLaunchTask": "Electron Serve without Launch Electron", "skipFiles": [ "/**" @@ -68,6 +67,7 @@ "stopAll": true }, { + // ホットリロードできない代わりにデバッグ起動が軽いモード "name": "Launch Electron Main/Renderer without hot reload", "configurations": [ "Attach to Renderer Process", diff --git a/.vscode/tasks.template.json b/.vscode/tasks.template.json index 0bc1568c4f..f79d5158ce 100644 --- a/.vscode/tasks.template.json +++ b/.vscode/tasks.template.json @@ -1,11 +1,10 @@ { - // See https://go.microsoft.com/fwlink/?LinkId=733558 - // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "Electron Serve without Launch Electron", // Electronを起動せずにバックグラウンドで"electron:serve"を実行する + // NOTE: デバッグ起動を軽くできる "type": "npm", "script": "electron:serve", "options": { diff --git a/vite.config.mts b/vite.config.mts index 63408d5cb6..4da4de6cef 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -26,7 +26,6 @@ export default defineConfig((options) => { ); } - const shouldEmitSourcemap = ["development", "test"].includes(options.mode); // 型を曖昧にして下の[process.platform]のエラーを回避する const sevenZipBinNames: Record = { win32: "7za.exe", @@ -42,11 +41,16 @@ export default defineConfig((options) => { ? path.join(__dirname, "build", "vendored", "7z") + path.sep : "") + sevenZipBinName; process.env.VITE_APP_VERSION = process.env.npm_package_version; + + const shouldEmitSourcemap = ["development", "test"].includes(options.mode); const sourcemap: BuildOptions["sourcemap"] = shouldEmitSourcemap ? "inline" : false; - const launchEditor = - process.env.SKIP_LAUNCH_ELECTRON !== "1" && options.mode !== "test"; + + // ref: electronの起動をスキップしてデバッグ起動を軽くする + const skipLahnchElectron = + options.mode === "test" || process.env.SKIP_LAUNCH_ELECTRON === "1"; + return { root: path.resolve(__dirname, "src"), envDir: __dirname, @@ -90,7 +94,7 @@ export default defineConfig((options) => { // ref: https://github.com/electron-vite/vite-plugin-electron/pull/122 onstart: ({ startup }) => { console.log("main process build is complete."); - if (launchEditor) { + if (skipLahnchElectron) { void startup([".", "--no-sandbox"]); } }, @@ -106,7 +110,7 @@ export default defineConfig((options) => { // ref: https://electron-vite.github.io/guide/preload-not-split.html entry: "./src/backend/electron/preload.ts", onstart({ reload }) { - if (launchEditor) { + if (skipLahnchElectron) { reload(); } }, From e3b555e2b055e5e320c5ce2c74fb7c29bb14e0d8 Mon Sep 17 00:00:00 2001 From: Hiroshiba Kazuyuki Date: Tue, 29 Oct 2024 04:16:36 +0900 Subject: [PATCH 4/5] =?UTF-8?q?README=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c8e5a0e8ea..ae1d1f2f6d 100644 --- a/README.md +++ b/README.md @@ -297,7 +297,9 @@ npx openapi-generator-cli version-manager list npm scripts の `serve` や `electron:serve` などの開発ビルド下では、ビルドに使用している vite で sourcemap を出力するため、ソースコードと出力されたコードの対応付けが行われます。 -`.vscode/launch.template.json` をコピーして `.vscode/launch.json` を作成することで、開発ビルドを VS Code から実行し、デバッグを可能にするタスクが有効になります。 +`.vscode/launch.template.json` をコピーして `.vscode/launch.json` を、 +`.vscode/tasks.template.json` をコピーして `.vscode/tasks.json` を作成することで、 +開発ビルドを VS Code から実行し、デバッグを可能にするタスクが有効になります。 ## ライセンス From d9a0b49a2253061e74c7740cc4da412ee283c3af Mon Sep 17 00:00:00 2001 From: Hiroshiba Kazuyuki Date: Tue, 29 Oct 2024 04:22:05 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=E9=80=86=E3=81=A0=E3=81=A3=E3=81=9F?= =?UTF-8?q?=EF=BD=A5=EF=BD=A5=EF=BD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vite.config.mts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vite.config.mts b/vite.config.mts index 4da4de6cef..123886b699 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -94,7 +94,7 @@ export default defineConfig((options) => { // ref: https://github.com/electron-vite/vite-plugin-electron/pull/122 onstart: ({ startup }) => { console.log("main process build is complete."); - if (skipLahnchElectron) { + if (!skipLahnchElectron) { void startup([".", "--no-sandbox"]); } }, @@ -110,7 +110,7 @@ export default defineConfig((options) => { // ref: https://electron-vite.github.io/guide/preload-not-split.html entry: "./src/backend/electron/preload.ts", onstart({ reload }) { - if (skipLahnchElectron) { + if (!skipLahnchElectron) { reload(); } },