简体   繁体   English

Electron主js脚本中如何调用Webpack绑定的function

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

I'm trying to access a function from my (webpack'ed) bundle, but the function is undefined.我正在尝试从我的(webpack'ed)包中访问 function,但是 function 是未定义的。 (The entire object in empty) (In electron-main.js see appBundle.test() ) (整个 object 为空)(在 electron-main.js 中参见appBundle.test()

I've been searching all over the web, but can't find the answer I'm looking for.我找遍了web,都没有找到我要找的答案。 Tried all the different libraryTarget options, but none of them worked the way I expected.尝试了所有不同的 libraryTarget 选项,但没有一个按我预期的方式工作。

Here is the source code:这是源代码:

webpack.dev.js: 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', }), ]), })

index.ts:指数.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'); }

electron-main.js:电子-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(); }); ... }...

Apparently the import "./application/index.tsx";显然是import "./application/index.tsx"; call somehow messed up the exports.调用以某种方式搞砸了出口。 When I just used a clean file as the entry point it worked fine.当我只使用一个干净的文件作为入口点时,它工作正常。

Anyway I ended up just building the electron project together with the app bundle into one package.无论如何,我最终只是将 electron 项目与应用程序包一起构建为一个 package。

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

相关问题 如何在electronic中的main.js中从另一个脚本调用函数 - How to call a function in another script from main.js in electron 是否可以从 web 到电子调用 main.js 文件中的函数? - Is it possible to call a function in main.js file from web to electron? electron “MAIN”:要求您拥有 js 文件并从中调用 function - electron “MAIN” : requiring you own js file and call function from it 如何使用 contextBridge 从 Electron main.js 调用 React 组件 function - How to call React component function from Electron main.js using contextBridge electron js如何调用索引中呈现的function - electron js how to call a function rendered in index 如何将 webpack 捆绑的函数导出到全局 - How to export a webpack-bundled function to global 当模块已被 Z424516CA53B4AD4BEFZ37ED0 捆绑时,如何从 HTML 的模块中调用 javascript function? - How can I call a javascript function in a module from HTML when the module has been bundled by webpack? 如何在捆绑的 Electron 应用程序中调用服务器相对 URL? - How can I call server relative URLs in a bundled Electron app? 从console.log调用webpack捆绑的函数/类 - Call webpack bundled function/class from console.log Webpack 4 – 简单的 js 功能在打包文件后不起作用 - Webpack 4 – simple js function not working after bundled files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM