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

feat: electron:serve起動時にエディタを起動しないオプションを追加 #2309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
38 changes: 34 additions & 4 deletions .vscode/launch.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"port": 9222,
"request": "attach",
"type": "chrome",
"webRoot": "${workspaceFolder}",
"webRoot": "${workspaceFolder}/src",
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/**"
],
"timeout": 20000, // 20 * 1000 ms程度あればビルド時間は間に合うはず
},
{
Expand All @@ -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": [
"<node_internals>/**"
]
},
{
"name": "Attach by Process ID",
// .bin viteを指定するとElectronのMain Processのデバッグが可能
Expand All @@ -35,13 +54,24 @@
"<node_internals>/**"
],
"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
}
]
}
}
28 changes: 28 additions & 0 deletions .vscode/tasks.template.json
Original file line number Diff line number Diff line change
@@ -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\\."
}
}
}
]
}
4 changes: 2 additions & 2 deletions src/backend/electron/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:";
Expand Down
4 changes: 2 additions & 2 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions tests/e2e/electron/example.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 7 additions & 2 deletions vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"]);
}
},
Expand All @@ -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 })],
Expand Down
Loading