简体   繁体   English

Electron,react 和 parcel 应用

[英]Electron, react and parcel application

I'm currently trying to compile my electron application into a dist for mac.我目前正在尝试将我的 electron 应用程序编译为 mac 的 dist。 I am using the boilerplate found here https://github.com/kumarryogeshh/electron-react-parcel-boilerplate我正在使用此处找到的样板https://github.com/kumarryogeshh/electron-react-parcel-boilerplate

Currently trying to modularise parts of the code, specifically looking at the electron side of things.目前正在尝试模块化部分代码,特别是查看 electron 方面。 So the intention is to have the following structure.因此,意图是具有以下结构。

- src 
  - components 
  - electron_components 
      window.js 
  electron.js 
  App.js 
  custom.css 
  index.js 

My application is going to have multiple windows, therefore having the windows.js class makes sense, see below:我的应用程序将有多个 windows,因此具有windows.js class 是有意义的,见下文:

const electron = require('electron');
const { BrowserWindow } = electron;
const isDev = require("electron-is-dev");

class Window extends BrowserWindow {
  constructor(urldev, urlLive, height, width, resizable = true, parent = null, modal = false) {
    super({
      height: height,
      width: width,
      frame: true,
      resizable: resizable,
      show: true,
      parent: parent,
      modal: modal,
      webPreferences: {
                        //allows application to run in the background
                        backgroundThrottling: true,
                        nodeIntegration: true
                        }
    });
    let promise = this.loadURL(
    isDev
      ? urldev
      : `file://${path.join(__dirname, urlLive)}`
  );
  }
}

module.exports = Window;


The electron.js file is below: electron.js文件如下:

const electron = require("electron");
const app = electron.app;

const path = require("path");
const window = require(path.join(__dirname, './electron_components/window'))

let mainWindow;

function createWindow() {
  mainWindow = new window('http://localhost:3000', "./build/index.html", 800, 600)
  mainWindow.on("closed", () => (mainWindow = null));
}

app.on("ready", createWindow);

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

app.on("activate", () => {
  if (mainWindow === null) {
    createWindow();
  }
});

So when I run the application by the command yarn start it runs and works.因此,当我通过命令yarn start运行应用程序时,它会运行并工作。 This is because it is the development environment.这是因为它是开发环境。 However, when I build the dist, by running yarn build it comes up with the following error:但是,当我构建 dist 时,通过运行yarn build它会出现以下错误:

Uncaught Exception:
Error: Cannot find module '/Volumes/electron-react-parcel 1.0.0 1/electron-react-parcel.app/Contents/Resources/app.asar/src/electron_components/window'
Require stack:
- /Volumes/electron-react-parcel 1.0.0 1/electron-react-parcel.app/Contents/Resources/app.asar/src/electron.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:797:17)
    at Function../lib/common/reset-search-paths.ts.Module._resolveFilename (electron/js2c/browser_init.js:7736:16)
    at Module._load (internal/modules/cjs/loader.js:690:27)
    at Module._load (electron/js2c/asar.js:738:28)
    at Function.Module._load (electron/js2c/asar.js:738:28)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/Volumes/electron-react-parcel 1.0.0 1/electron-react-parcel.app/Contents/Resources/app.asar/src/electron.js:5:16)
    at Module._compile (internal/modules/cjs/loader.js:967:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1004:10)

Obviously something is going wrong with the component importation and therefore safe to assume that the import statement for the window class at the top of the electron.js file isn't right.显然,组件导入出现问题,因此可以安全地假设electron.js文件顶部的window class的导入语句不正确。 I'm not sure where I'm going wrong but your assistance would be great.我不确定我哪里出错了,但你的帮助会很棒。

Thanks谢谢

So I chose to change to a different boilerplate that used electron forge.所以我选择更改为使用 electron forge 的不同样板。 Whilst not answering the original question.虽然没有回答原来的问题。 It rectified my issue.它纠正了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM