javascript - Webpack无法在Electron应用程序中从Nodejs打包https

标签 javascript node.js reactjs webpack electron

我的webpack配置似乎可以成功构建,但是尽管在配置中定义了导入,但我仍获得了未定义的对https包的引用。
我不确定我的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
  }
});

关于javascript - Webpack无法在Electron应用程序中从Nodejs打包https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66201397/

相关文章:

javascript - 当Reducers更新状态时,路由重定向不会发生

javascript - 保持提交按钮禁用,直到填充动态创建的必填字段

javascript - 脚本中所需的代码不起作用

javascript - 无法使用 Sequelize 在架构 (postgresql) 中关联模型

javascript - 具有多个入口点的 Webpack watch() - 为未更改的文件发送 bundle ?

javascript - 如何在 React 中动态更改正文背景颜色?

JavaScript - 获取右键单击上下文菜单的元素的父 iframe

javascript - 在WebView中执行JS脚本

node.js - Sequelize 如何运作?

javascript - 禁用 div onClick