alex8088 / electron-vite

Next generation Electron build tooling based on Vite 新一代 Electron 开发构建工具,支持源代码保护
https://electron-vite.org
MIT License
3.58k stars 153 forks source link

渲染进程中导入electron-store失败 #585

Closed Pandaver closed 3 months ago

Pandaver commented 3 months ago

Describe the bug

// electron/main.ts

import { app, BrowserWindow } from 'electron'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { ipcMain } from 'electron'
import Store from 'electron-store'

const require = createRequire(import.meta.url)
const __dirname = path.dirname(fileURLToPath(import.meta.url))

// The built directory structure
//
// ├─┬─┬ dist
// │ │ └── index.html
// │ │
// │ ├─┬ dist-electron
// │ │ ├── main.js
// │ │ └── preload.mjs
// │
process.env.APP_ROOT = path.join(__dirname, '..')

// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - Vite@2.x
export const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron')
export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')

process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL ? path.join(process.env.APP_ROOT, 'public') : RENDERER_DIST

let win: BrowserWindow | null

function createWindow() {
  win = new BrowserWindow({
    height: 640,
    width: 1024,
    icon: path.join(process.env.VITE_PUBLIC, 'electron-vite.svg'),
    webPreferences: {
      preload: path.join(__dirname, 'preload.mjs'),
    },
    frame: false,
  })

  // Test active push message to Renderer-process.
  win.webContents.on('did-finish-load', () => {
    win?.webContents.send('main-process-message', (new Date).toLocaleString())
  })

  win.webContents.openDevTools()

  if (VITE_DEV_SERVER_URL) {
    win.loadURL(VITE_DEV_SERVER_URL)
  } else {
    // win.loadFile('dist/index.html')
    win.loadFile(path.join(RENDERER_DIST, 'index.html'))
  }

}

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
    win = null
  }
})

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock svg-icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})

app.whenReady().then(createWindow)

// 最小化
ipcMain.on('window-min', function() {
  win?.minimize();
})

// 窗口化
ipcMain.on('window-change', function () {
  if (!win) return
  if (win.isMaximized()) {
    win.restore()
  } else {
    win.maximize()
  }
})

// 关闭
ipcMain.on('window-close', function () {
  win?.close()
})

Store.initRenderer()
// src/main.ts

import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import {BrowserRouter} from "react-router-dom";
import {ConfigProvider} from "antd";
import zhCN from 'antd/lib/locale/zh_CN'
import 'virtual:svg-icons-register'
import Store from 'electron-store'
const store = new Store()

ReactDOM.createRoot(document.getElementById('root')!).render(
  <BrowserRouter>
    <ConfigProvider locale={zhCN}>
      <App />
    </ConfigProvider>
  </BrowserRouter>
)

// Use contextBridge
window.ipcRenderer.on('main-process-message', (_event, message) => {
  console.log(message)
})

error.log

build started... (x2)
✓ 1 modules transformed.
dist-electron/preload.mjs  0.67 kB │ gzip: 0.27 kB
built in 63ms.
transforming (165) node_modules\conf\node_modules\ajv\dist\voca✓ 563 modules transformed.
dist-electron/main.js  534.51 kB │ gzip: 117.85 kB
built in 978ms.

[16228:0812/161549.761:ERROR:CONSOLE(1)] "Request Autofill.enable failed. {"code":-32601,"message":"'Autofill.enable' wasn't found"}", source: devtools://devtools/bundled/core/protocol_client/protocol_client.js (1)
X [ERROR] No matching export in "node_modules/.vite-electron-renderer/electron.mjs" for import "app"

    node_modules/electron-store/index.js:4:1:
      4 │   app,
        ╵   ~~~

X [ERROR] No matching export in "node_modules/.vite-electron-renderer/electron.mjs" for import "ipcMain"

    node_modules/electron-store/index.js:5:1:
      5 │   ipcMain,
        ╵   ~~~~~~~

16:15:50 [vite] error while updating dependencies:
Error: Build failed with 2 errors:
node_modules/electron-store/index.js:4:1: ERROR: No matching ex
port in "node_modules/.vite-electron-renderer/electron.mjs" for import "app"
node_modules/electron-store/index.js:5:1: ERROR: No matching ex
port in "node_modules/.vite-electron-renderer/electron.mjs" for import "ipcMain"
    at failureErrorWithLog (C:\Users\Administrator\WebstormProjects\fss\node_modules\esbuild\lib\main.js:1472:15)
    at C:\Users\Administrator\WebstormProjects\fss\node_modules\esbuild\lib\main.js:945:25
    at C:\Users\Administrator\WebstormProjects\fss\node_modules\esbuild\lib\main.js:1353:9
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Electron-Vite Version

2.3.0

Electron Version

30.0.1

Vite Version

5.1.6

Validations

alex8088 commented 3 months ago

electron-store 为node模块, 并不支持渲染进程,非要在渲染进程使用需开启node集成。推荐 https://github.com/alex8088/electron-conf

Pandaver commented 3 months ago

electron-store 为node模块, 并不支持渲染进程,非要在渲染进程使用需开启node集成。推荐 https://github.com/alex8088/electron-conf。

请问我需要如何修改配置才能使用?electron-conf 我看了,但我需要electron-store的监听和加密功能