簡體   English   中英

Electron主js腳本中如何調用Webpack綁定的function

[英]How to call function bundled by Webpack in Electron main js script

我正在嘗試從我的(webpack'ed)包中訪問 function,但是 function 是未定義的。 (整個 object 為空)(在 electron-main.js 中參見appBundle.test()

我找遍了web,都沒有找到我要找的答案。 嘗試了所有不同的 libraryTarget 選項,但沒有一個按我預期的方式工作。

這是源代碼:

webpack.dev.js:

 ... module.exports = Merge(CommonConfig, { devtool: "inline-source-map", watch: true, entry: path.resolve(__dirname, "src/index.ts"), output: { filename: "bundle.js", path: __dirname + "/wwwroot/resources/js/components", publicPath: "/resources/js/components/", library: "appBundle", libraryTarget: "commonjs2" }, plugins: ([ new webpack.DefinePlugin({ "process.env": { "NODE_ENV": JSON.stringify("development") } }), // Etract CSS new MiniCssExtractPlugin({ filename: '[name].css', chunkFilename: '[id].css', }), ]), })

指數.ts:

 import 'react-app-polyfill/ie11'; import 'react-app-polyfill/stable'; // Styling import './application-styling.scss'; // Application import "./application/index.tsx"; export default function test() { console.log('hello'); }

電子-main.js:

 const { BrowserWindow } = require('electron'); const createAppWindow = require('../main/app-process'); const authService = require('../services/auth-service'); global.window = {}; // <- Need to define this manually, else the require call throws errors const appBundle = require('app-bundle'); // Trying to import the bundle.js here let win = null; function createAuthWindow() { destroyAuthWin(); win = new BrowserWindow({ width: 1000, height: 600, webPreferences: { nodeIntegration: false, enableRemoteModule: false } }); win.loadURL(authService.getAuthenticationURL()); const { session: { webRequest } } = win.webContents; const filter = { urls: [ 'http://localhost/callback*' ] }; webRequest.onBeforeRequest(filter, async ({ url }) => { console.log(appBundle); // <- undefined or empty object, depending on libraryTarget console.log(appBundle.test()); // <- error, test is undefined await authService.loadTokens(url); createAppWindow(); return destroyAuthWin(); }); ... }...

顯然是import "./application/index.tsx"; 調用以某種方式搞砸了出口。 當我只使用一個干凈的文件作為入口點時,它工作正常。

無論如何,我最終只是將 electron 項目與應用程序包一起構建為一個 package。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM