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 })],