簡體   English   中英

Webpack 未能在 Electron 應用程序中從 Nodejs 打包 https

[英]Webpack failing to pack https from Nodejs in Electron application

我的 webpack 配置似乎已成功構建,但是盡管在配置中定義了導入,但我得到了對 https package 的未定義引用。

我不確定我的 webpack 是否配置不正確,或者我在定義 axios 實例時是否以某種方式錯誤地調用了 https。 任何指針將不勝感激。

webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const BUILD_DIR = path.resolve(__dirname, "./build/app");
const SRC_DIR = path.resolve(__dirname, "./src/app");

module.exports = {
  entry: {
    main: SRC_DIR + "/index.tsx",
  },
  output: {
    filename: "bundle.js",
    path: BUILD_DIR,
  },
  resolve: {extensions: [".ts", ".tsx", ".js", ".jsx"]},
  module: {
    rules: [
      {test: /\.tsx?$/, include: SRC_DIR, use: "awesome-typescript-loader"},
      {test: /\.(j|t)sx?$/, enforce: "pre", use: "source-map-loader"},
      {test: /\.css$/, use: ["style-loader", "css-loader"]},
      {test: /\.(jpg|png|gif|jpeg|woff|woff2|eot|ttf|svg)$/, use: "url-loader?limit=100000"},
      {test: /\.html$/, use: "html-loader"},
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/app/html/index.html",
      filename: "./index.html",
    }),
    new webpack.ExternalsPlugin("commonjs", ["electron", "https"]),
  ],
  target: "electron-renderer",
};

Client Instance

import Axios from "axios";
import https from "https";

export const clientInstance = Axios.create({
  baseURL: "https://127.0.0.1:2999/liveclientdata/allgamedata",
  httpsAgent: new https.Agent({ // <---- this is where it fails. the reference to https.
    rejectUnauthorized: false,
  }),
});

error

Uncaught ReferenceError: require is not defined
    at Object.https (bundle.js:7743)
    at __webpack_require__ (bundle.js:7766)
    at eval (webpack://express-electron-core/./src/app/components/plugins/axios.ts?:9)
    at Object../src/app/components/plugins/axios.ts (bundle.js:316)
    at __webpack_require__ (bundle.js:7766)
    at eval (webpack://express-electron-core/./src/app/components/hooks/useAssociatedAccounts.ts?:5)
    at Object../src/app/components/hooks/useAssociatedAccounts.ts (bundle.js:173)
    at __webpack_require__ (bundle.js:7766)
    at eval (webpack://express-electron-core/./src/app/components/hooks/index.ts?:4)
    at Object../src/app/components/hooks/index.ts (bundle.js:140)

main.ts

const mainWindow = new BrowserWindow({
  webPreferences:{
    contextIsolation: true //this was the issue
  }
});

暫無
暫無

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

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