caoxiemeihao / nuxt-electron

Integrate Nuxt and Electron
MIT License
191 stars 21 forks source link

Nuxt dev mode do not stop on app quit #54

Closed Ax3l3rator closed 7 months ago

Ax3l3rator commented 1 year ago

When i press 'x' in window nuxt dev mode just stays in console app code

import { app } from 'electron';
import { restoreOrCreateWindow } from './mainWindow';
import { platform } from 'node:process';

const isSingleInstance = app.requestSingleInstanceLock();
if (!isSingleInstance) {
  app.quit();
  process.exit(0);
}
app.on('second-instance', restoreOrCreateWindow);

app.disableHardwareAcceleration();

app.on('window-all-closed', () => {
  if (platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', restoreOrCreateWindow);

app
  .whenReady()
  .then(restoreOrCreateWindow)
  .catch((e) => console.error('Failed create window:', e));

window creation code

import { app, BrowserWindow } from 'electron';
import { join, resolve } from 'node:path';

async function createWindow() {
  const browserWindow = new BrowserWindow({
    show: false,
    webPreferences: {
      // nodeIntegration: false,
      // contextIsolation: true,
      // sandbox: false,
      // webviewTag: false,
      // preload: join(app.getAppPath(), 'preload.{ts,js}'),
    },
  });

  browserWindow.on('ready-to-show', () => {
    browserWindow?.show();
    if (import.meta.env.DEV) {
      browserWindow.webContents.on('before-input-event', (_, input) => {
        if (input.type === 'keyDown' && input.key === 'F12') {
          browserWindow.webContents.openDevTools({ mode: 'detach' });
        }
      });
    }
  });

  if (
    import.meta.env.DEV &&
    import.meta.env.VITE_DEV_SERVER_URL !== undefined
  ) {
    await browserWindow.loadURL(import.meta.env.VITE_DEV_SERVER_URL);
  } else {
    await browserWindow.loadFile(
      resolve(__dirname, '../.output/public/index.html'),
    );
  }
  browserWindow.removeMenu();
  browserWindow.setMenu(null);

  return browserWindow;
}

export async function restoreOrCreateWindow() {
  let window = BrowserWindow.getAllWindows().find((w) => !w.isDestroyed());
  if (window === undefined) {
    window = await createWindow();
  }

  if (window.isMinimized()) {
    window.restore();
  }

  window.focus();
}

EDIT: Did some reseach dsown in comments, trying to figure out what exactly causes the issue UPD 2: crutch-fix, but works fine

Ax3l3rator commented 1 year ago

UPD: package.json

{
  "name": "hse-app-desktop",
  "private": true,
  "scripts": {
    "build": "nuxt build --prerender && electron-builder",
    "dev": "nuxi dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@nuxt/devtools": "latest",
    "electron": "^27.0.2",
    "electron-builder": "^24.6.4",
    "nuxt": "^3.8.0",
    "nuxt-electron": "^0.6.0",
    "sass": "^1.69.5",
    "vite-plugin-electron": "^0.14.1",
    "vite-plugin-electron-renderer": "^0.14.5",
    "vite-plugin-vuetify": "^1.0.2",
    "vue": "^3.3.7",
    "vue-router": "^4.2.5"
  },
  "main": "dist-electron/index.js",
  "version": "0.0.1",
  "dependencies": {
    "@mdi/font": "^7.3.67",
    "vuetify": "^3.3.23"
  }
}
Ax3l3rator commented 1 year ago

Note: quickstart project with following packagfe versions also do not stop nuxt dev server with electron app.quit()/app.exit()

{
  "name": "nuxt-electron-quick-start",
  "version": "0.0.0",
  "private": true,
  "author": "草鞋没号 <308487730@qq.com>",
  "license": "MIT",
  "main": "dist-electron/main.js",
  "scripts": {
    "dev": "nuxi dev",
    "build": "nuxi build --prerender && electron-builder"
  },
  "dependencies": {},
  "devDependencies": {
    "electron": "^27.0.2",
    "electron-builder": "^24.6.4",
    "nuxt": "^3.6.3",
    "nuxt-electron": "^0.6.0",
    "vite-plugin-electron": "^0.14.1",
    "vite-plugin-electron-renderer": "^0.14.5"
  }
}

Old project with following versions works good

{
  "name": "HSEAppXWin",
  "private": true,
  "scripts": {
    "build": "nuxt build --prerender && electron-builder",
    "dev": "set DEV_URL=http://localhost:3001/&&set scrtk=hseDummyKeyLol&&nuxt dev",
    "generate": "nuxt generate && electron-builder",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@mdi/font": "^7.2.96",
    "@types/electron": "^1.6.10",
    "@types/jsonwebtoken": "^9.0.3",
    "@types/node": "^18",
    "electron": "^26.2.2",
    "electron-builder": "^24.6.4",
    "nuxt": "^3.4.3",
    "vite-electron-plugin": "^0.8.2",
    "vite-plugin-electron-renderer": "^0.14.1",
    "vite-plugin-vuetify": "^1.0.2"
  },
  "dependencies": {
    "@vee-validate/nuxt": "^4.9.0",
    "@vueuse/nuxt": "^10.1.2",
    "electron-store": "^8.1.0",
    "jsonwebtoken": "^9.0.2",
    "keytar": "^7.9.0",
    "nuxt-electron": "^0.4.5",
    "sass": "^1.62.1",
    "vuetify": "^3.2.2"
  },
  "main": "dist-electron/electron/main.js",
  "version": "1.1.2",
  "build": {
    "icon": "./build/icon.png"
  }
}
dvwzj commented 1 year ago
app.on('window-all-closed', () => {
  if (platform !== 'darwin') {
    app.quit();
  }
  process.kill(process.pid, 'SIGTERM'); // add this
})

this work for me

Ax3l3rator commented 1 year ago

Hey @dvwzj, huge thanks for your reply! Do u use process.kill in production mode too, or only for dev mode?

yejimeiming commented 11 months ago

vite-plugin-electron@0.15.x supports use tree-kill exits complated Electron app for dev mode.

Ax3l3rator commented 10 months ago

Hey, @yejimeiming, huge thanks for your reply, your advice partly fixed my problem, looking forward for vite-plugin-electron support tree-kill natively! Closing issue.

Ax3l3rator commented 10 months ago

Reopening, installing tree-kill didn't help, console process is still up when i close window in dev mode(if i don't send SIGKILL manually)

yejimeiming commented 9 months ago

You can upgrade to v0.28.4