caoxiemeihao / nuxt-electron

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

dist-electron/main.js not being created #79

Closed sev-b closed 6 months ago

sev-b commented 6 months ago

I am stumped: Have been trying to add this lib to an existing nuxt/electron app today and everything seems to work fine when running npx nuxi dev initially but for some reason only preload.js gets created in dist-electron but no main.js leading to the error:

Cannot find module dist-electron\main.js. Please verify that the package.json has a valid "main" entry

image

Nuxt 3.11.2 with Nitro 2.9.6                                                                                                                                                                              
WARN  The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api deprecated for more details.

  ➜ Local:    http://localhost:3000/
  ➜ Network:  use --host to expose

  ➜ DevTools: press Shift + Alt + D in the browser (v1.3.1)                                                                                                                                               

vite v5.2.11 building for development...                                                                                                                                                                  

watching for file changes...
vite v5.2.11 building for development...     
watching for file changes...

build started...

build started... (x2)

✓ 1 modules transformed.

dist-electron/preload.js  0.38 kB │ gzip: 0.22 kB                                                                                                                                                         
built in 6188ms.  

ℹ Vite client warmed up in 9487ms                                                                                                                                                                        
✓ 301 modules transformed.                                                                                                                                                                                

 ERROR  [vite]: Rollup failed to resolve import "fsevents" from "D:/GIT/ut-wizard/front-end/node_modules/rollup/dist/es/shared/node-entry.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
build.rollupOptions.external

✔ Nuxt Nitro server built in 11606 ms

nuxt.config.mts:

import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import pkg from './package.json'

import { defineNuxtConfig } from 'nuxt/config'
import vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
import eslint from 'vite-plugin-eslint2'
import renderer from 'vite-plugin-electron-renderer'

fs.rmSync(path.join(__dirname, 'dist-electron'), { recursive: true, force: true })

export default defineNuxtConfig({
  app: {
    baseURL: process.env.NODE_ENV === 'development' ? '/' : './',
  },
  devtools: { enabled: true },
  modules: [
    'nuxt-electron',
    '@nuxt/eslint',
    '@pinia/nuxt',
    (_options, nuxt) => {
      nuxt.hooks.hook('vite:extendConfig', (config) => {
        // @ts-expect-error
        config.plugins.push(vuetify({ autoImport: true }))
      })
    },
  ],
  electron: {
    build: [
      {
        entry: 'electron/main.mts',
      },
      {
        entry: 'electron/preload.mts',
        onstart(options) {
          options.reload()
        },
      },
    ],
  },
  ssr: false,
  router: {
    hashMode: true,
  },
  build: {
    transpile: ['vuetify'],
  },
  vite: {
    build: {
      minify: process.env.NODE_ENV === 'production',
      rollupOptions: {
        external: [
          ...Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
        ],
      },
    },
    vue: {
      template: {
        transformAssetUrls,
      },
    },
    plugins: [
      eslint({
        fix: true,
        cache: false,
      }),
      renderer(),
    ],
  },
})

package.json:

{
  "name": "app",
  "version": "1.0.0",
  "description": "app",
  "author": "app",
  "main": "dist-electron/main.js",
  "type": "module",
  "private": true,
  "scripts": {
    "start": "set NODE_ENV=development&& nuxi dev --no-fork && electron-forge start",
    "make": "pyinstaller --distpath ../ ../start_server.spec && npx nuxi build && electron-forge make",
  },
  "dependencies": {
    "electron-squirrel-startup": "^1.0.0",
    "socket.io-client": "^4.7.2"
  },
  "devDependencies": {
    "@electron-forge/cli": "^7.3.0",
    "@electron-forge/maker-squirrel": "^7.3.0",
    "@mdi/font": "^7.4.47",
    "@nuxt/devtools": "^1.3.1",
    "@nuxt/eslint": "^0.3.12",
    "@nuxt/eslint-config": "^0.3.12",
    "@pinia/nuxt": "^0.5.1",
    "electron": "^29.1.0",
    "electron-context-menu": "^3.6.1",
    "electron-devtools-installer": "^3.2.0",
    "eslint": "^8.57.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-vue": "^9.26.0",
    "nuxt": "^3.6.5",
    "nuxt-electron": "^0.7.0",
    "pinia": "^2.1.7",
    "plotly.js-dist": "^2.32.0",
    "prettier": "3.2.5",
    "sass": "^1.60.0",
    "vite": "^5.2.11",
    "vite-plugin-electron": "^0.28.7",
    "vite-plugin-electron-renderer": "^0.14.5",
    "vite-plugin-eslint2": "^4.4.0",
    "vite-plugin-vuetify": "^2.0.3",
    "vue": "^3.4.24",
    "vuetify": "^3.5.17"
  }
}