久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

即使在 webpack.DefinePlugin 中設置了環境變量,也沒

Environment variable is undefined in electron even it has been set inside webpack.DefinePlugin(即使在 webpack.DefinePlugin 中設置了環境變量,也沒有在電子中定義)
本文介紹了即使在 webpack.DefinePlugin 中設置了環境變量,也沒有在電子中定義的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個要求,我們需要根據它是在生產環境中還是在開發環境中執行來設置 dll 路徑.所以我決定將該值放在環境變量中,并嘗試使用 webpack.DefinePlugin({}) 來實現.

I have a requirement where we need to set dll path based upon whether it is executing in production or in development environment. So I decided to place that value in environment variable and tried to achieve that using webpack.DefinePlugin({}).

方法一:

webpack.config.json

plugins: [
new webpack.DefinePlugin({
    'process.env.NODE_ENV' : JSON.stringify('production')
})

然后我嘗試在電子的主進程中獲取該值,在我的例子中是 elec.js

And then I tried to get that value in electron's main process, In my case elec.js

elec.js

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

var dllPath = "";

function createWindow() {
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    title: "Test",
    icon: "Test.ico"
  });

  win.setMenu(null);

  win.loadURL(
    url.format({
      pathname: path.join(__dirname, "../renderer/index.html"),
      protocol: "file:",
      slashes: true
    })
  );

if (process.env.NODE_ENV ==='production') {
    dllPath = path.join(
      __dirname,
      "./../../dll/test.dll"
    );
  } else {
    dllPath = path.join(
      __dirname,
      "./../../../dll/test.dll"
    );
  }
}

app.on("ready", createWindow);

但問題是,當我嘗試在 createWindow() 函數中訪問該值時,它是未定義的,因此流程總是轉到 else 塊.

But problem is that when I try to access that value in createWindow() function it is undefined so flow always goes to else block.

我有什么遺漏嗎?

方法二:

我嘗試使用跨環境節點包來實現相同的目標,但沒有運氣.請在下面找到我嘗試使用 cross-env 的代碼塊.

I tried to achieve the same using cross-env node package, but no luck. Please find below code block which I tried using cross-env.

package.json

 "scripts": {
          "build": "cross-env process.env.NODE_ENV=production && rimraf ./dist/ && webpack --progress && node-sass 
           ./src/renderer/scss/ -o ./dist/renderer/ && rimraf ./dist/renderer/includes/"
    }

推薦答案

iva2k 提供的洞察力使我能夠為同樣的問題找到解決方案.

The insight provided by iva2k is what allowed me to come to a solution for this same problem.

使用 dotenv 為我的配置創建一個 .env 文件我到了我想去的地方的一半(設置一些環境變量以在生產環境中使用).然后問題就變成了 Electron 默認沒有將那些從 Main 進程傳遞到 Renderer 進程.

Using dotenv to create a .env file for my config got me halfway to where I wanted to be (setting up a few environment variables for use in a production setting). The problem then became that Electron wasn't passing those from the Main process down to the Renderer process by default.

解決方法很簡單:使用 Electron 自己的 ipcMainipcRenderer 模塊在兩者之間傳遞 dotenv 對象.

The work-around is simple: use Electron's own ipcMain and ipcRenderer modules to pass the dotenv object between the two.

在您的主文件(例如您的 elec.js 文件)中,在需要模塊后放置一個 ipcMain 事件監聽器:

In your main file (e.g. your elec.js file), place an ipcMain event listener after requiring the module:

const config = require('dotenv').config();
const electron = require('electron');
const { app, BrowserWindow, ipcMain } = electron;

...

ipcMain.on('get-env', (event) => {
    event.sender.send('get-env-reply', config);
});

在應用程序的渲染端,將其放置在任何需要的地方:

Elsewhere, in your application's rendering-side, place this anywhere necessary:

async function getConfig() 
{
    const { ipcRenderer } = window.require('electron');
    let config = null;
    ipcRenderer.on('get-env-reply', (event, arg) => {
        // The dotenv config object should return an object with
        // another object inside caled "parsed". Change this if need be.
        config = arg.parsed;
    });
    ipcRenderer.send('get-env');

    return config;
}

這基本上允許我在主進程文件中聲明一個事件,然后在我想要的任何進程端文件中重新使用它,從而允許我在與構建一起的文件中混淆配置變量,但不是如果不打開開發工具,最終用戶將無法訪問.

This basically allowed me to declare one event in the Main process file, and then re-use it in any process-side file I wanted, thus allowing me to obfuscate config variables in a file that goes with the build, but isn't accessible to end-users without opening up the dev-tools.

這篇關于即使在 webpack.DefinePlugin 中設置了環境變量,也沒有在電子中定義的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 久久亚洲综合 | 国产一区在线视频 | 日p视频免费看 | 亚洲激情综合 | 欧美日韩一区二区三区在线观看 | 农村黄性色生活片 | 美女在线视频一区二区三区 | 午夜视频在线观看网址 | 久久久久资源 | 99re国产视频| 久久精品网 | 国产精品久久久久免费 | 久久99久久99 | 九九av| 亚洲欧美一区二区三区视频 | av一级久久 | 婷婷一级片 | 国产九九九九 | 真人女人一级毛片免费播放 | 亚洲视频一区二区三区四区 | 99精品欧美一区二区三区 | 免费在线一区二区 | 日本大香伊一区二区三区 | 亚洲成人播放器 | 午夜久久久久久久久久一区二区 | 亚洲一区中文字幕 | 久久久免费 | 91社区视频 | 国产一区免费视频 | 99九色 | 久久男人 | 狠狠色狠狠色综合日日92 | 久久久精品一区 | 97精品超碰一区二区三区 | 九九热这里| 久久久久91 | 亚洲精品一区二区 | 亚洲精品一区二区三区蜜桃久 | 日韩毛片播放 | 日日操夜夜操天天操 | 久久精品国产一区老色匹 |