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] =?UTF-8?q?feat:=20=E9=96=8B=E7=99=BA=E3=82=B5=E3=83=BC?= =?UTF-8?q?=E3=83=90=E3=83=BC=E8=B5=B7=E5=8B=95=E6=99=82=E3=81=AB=E3=82=A8?= =?UTF-8?q?=E3=83=87=E3=82=A3=E3=82=BF=E3=82=92=E8=B5=B7=E5=8B=95=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=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 })],