简体   繁体   English

电子在生产模式下运行

[英]Electron run in production mode

I'm using the npm package https://github.com/sindresorhus/electron-is-dev我正在使用 npm 包https://github.com/sindresorhus/electron-is-dev

For some reason, isDev always returns true.出于某种原因, isDev总是返回 true。

My npm script looks as follows:我的 npm 脚本如下所示:

"start:prod": "cross-env NODE_ENV=production && electron dist/main.js"

main.js:主要.js:

import isDev from 'electron-is-dev';

app.on('ready', () => {
  console.log('isDev', isDev);
  if (!isDev) {
    const {session} = require('electron');
    session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
      callback({responseHeaders: `default-src http: ws:`})
    })
  }
  const win = createWindow();
  createMenu(win);
});

The console outputs:控制台输出:

isDev true

Documentation mentions:文档提到:

You can force development mode by setting the ELECTRON_IS_DEV environment variable to 1.您可以通过将 ELECTRON_IS_DEV 环境变量设置为 1 来强制开发模式。

But I don't feel that putting the variable to zero should be necessary.但我不认为将变量设置为零是必要的。

Outputting process.env.ELECTRON_IS_DEV logs undefined.输出process.env.ELECTRON_IS_DEV日志未定义。

I found the following thread that I found confusing: https://github.com/electron/electron/issues/7714我发现了以下令人困惑的线程: https : //github.com/electron/electron/issues/7714

I don't see the use of an extra environment variable, when you set the NODE_ENV on startup... Unless there is a compelling reason not to, I will just check on process.env.NODE_ENV as I'm used to.当您在启动时设置 NODE_ENV 时,我没有看到使用额外的环境变量......除非有令人信服的理由不这样做,否则我只会像我习惯的那样检查process.env.NODE_ENV

I don't use that npm package but I'll share what I do – (no idea if it is right or wrong but it works. I use it to run electron-reload during development).我不使用那个 npm 包,但我会分享我所做的 - (不知道它是对还是错,但它有效。我在开发过程中使用它来运行electron-reload )。

package.json – set an env var in my 'start' script package.json – 在我的“开始”脚本中设置一个环境变量

  "scripts": {
    "start": "APP_DEV=true electron ."
   }

main.js - check for the env var in "main.js" main.js - 检查“main.js”中的环境变量

var isDev = process.env.APP_DEV ? (process.env.APP_DEV.trim() == "true") : false;

use it用它

if (isDev) {
    require('electron-reload')(__dirname, {
        electron: upath.toUnix(upath.join(__dirname, 'node_modules', '.bin', 'electron'))
    });
}

UPDATE: 4.28.20更新: 4.28.20

Had to bring my project over to a Windows machine to work on some Windows-specific issues and the method above does not work there.不得不将我的项目带到 Windows 机器上以解决一些特定于 Windows 的问题,而上述方法在那里不起作用。 Here is my question on that and an answer which gets it working on Windows: Setting an env var in package.json for use with electron-reload?这是我对此的问题以及使其在 Windows 上运行的答案:在 package.json 中设置一个环境变量以用于电子重新加载?

I have noticed that the process.env variable in development contains information about npm_package which is absent in production build.我注意到开发中的process.env变量包含有关 npm_package 的信息,这在生产构建中是不存在的。 Checking for process.env.npm_node_execpath gives me info about prod or dev.检查process.env.npm_node_execpath为我提供了有关 prod 或 dev 的信息。

You can use electron builtin variable for this that is app.isPackaged It can be found here https://www.electronjs.org/docs/api/app#appispackaged-readonly Add the below line to check it yourself您可以为此使用电子内置变量app.isPackaged可以在此处找到https://www.electronjs.org/docs/api/app#appispackaged-readonly添加以下行以自行检查

console.log(app.isPackaged);

Else别的

If you are using Electron-is-dev then use如果您使用的是 Electron-is-dev,则使用

"start": "set ELECTRON_IS_DEV=0 && electron ."

Which will set the environment to production.这会将环境设置为生产。 And for setting it back to DEV replace that 0 with 1.并将其设置回 DEV 将 0 替换为 1。

References - https://www.geeksforgeeks.org/manage-staging-environments-in-electronjs/参考资料 - https://www.geeksforgeeks.org/manage-staging-environments-in-electronjs/

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

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