electron / forge

:electron: A complete tool for building and publishing Electron applications
https://electronforge.io
MIT License
6.5k stars 520 forks source link

react v16, electron-forge v6, webpack v5, electron v22 not working after adding sqlite3 #3205

Open mohit-solutelabs opened 1 year ago

mohit-solutelabs commented 1 year ago

Pre-flight checklist

Electron Forge version

6.0.4

Electron version

22.0.1

Operating system

macOS 13.4

Last known working Electron Forge version

No response

Expected behavior

adding sqlite3 should not break my renderer

Actual behavior

if we only remove accessing sqlite3 from electron.js, then the app works fine (sqlite3 is installed, but not being used).

but as soon as we start using (not install, but start) sqlite3, the renderer is throwing

Steps to reproduce

create a electron forge app with following files

// webpack.main.js

module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: "./electron/electron.js",
  // Put your normal webpack config below here
  module: {
    rules: [
      {
        // We're specifying native_modules in the test because the asset
        // relocator loader generates a "fake" .node file which is really
        // a cjs file.
        test: /native_modules\/.+\.node$/,
        use: "node-loader",
      },
      {
        test: /\.(m?js|node)$/,
        parser: { amd: false },
        use: {
          loader: "@vercel/webpack-asset-relocator-loader",
          options: {
            outputAssetBase: "native_modules",
          },
        },
      },
    ],
  },
  externals: {
    sqlite3: "commonjs sqlite3",
  },
};

// electron.js
const { app, BrowserWindow } = require("electron");
const sqlite3 = require("sqlite3").verbose();
const path = require("path");

export let db;
const dbPath = path.resolve(__dirname, "app.db");

if (require("electron-squirrel-startup")) {
  app.quit();
}

const isDev = require("electron-is-dev");

function createWindow() {
  // Create the browser window.
  let mainWindow = new BrowserWindow({
    icon: "./src/images/logo.png",
    title: "my App",
    titleBarOverlay: true,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      nodeIntegrationInSubFrames: true,
    },
  });

  mainWindow.webContents.setWindowOpenHandler(() => {
    return {
      action: "allow",
      overrideBrowserWindowOptions: {
        width: 1280,
        height: 720,
        webPreferences: { nodeIntegration: true, contextIsolation: false, nodeIntegrationInSubFrames: true },
      },
    };
  });

  mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
  db = new sqlite3.Database(dbPath, (err) => {
    if (err) {
      console.error(err.message);
    }
    console.log("Connected to the database.");
  });

  if (isDev) {
    mainWindow.webContents.openDevTools({ mode: "detach" });
  }
  mainWindow.maximize();

  mainWindow.on("closed", () => {
    mainWindow = null;
  });
}

app.whenReady().then(createWindow);

app.on("window-all-closed", () => {
  db.close((err) => {
    if (err) {
      console.error(err.message);
    }
    console.log("Disconnected from the database.");
  });

  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

// forge.config.js

module.exports = {
  packagerConfig: {
    icon: "./src/images/icons/icon",
  },
  rebuildConfig: {},
  plugins: [
    {
      name: "@electron-forge/plugin-webpack",
      port: 8050,
      config: {
        devContentSecurityPolicy:
          "default-src * self blob: data: gap:; style-src * self 'unsafe-inline' blob: data: gap:; script-src * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:; object-src * 'self' blob: data: gap:; img-src * self 'unsafe-inline' blob: data: gap:; connect-src self * 'unsafe-inline' blob: data: gap:; frame-src * self blob: data: gap:;",
        mainConfig: "./webpack.main.js",
        devServer: {
          port: 8050,
          historyApiFallback: true,
          liveReload: true,
          hot: true,
          devMiddleware: {
            writeToDisk: true,
          },
        },
        renderer: {
          config: "./webpack.prod.js",
          nodeIntegration: true,
          contextIsolation: false,
          entryPoints: [
            {
              name: "main_window",
              html: "./src/index.html",
              js: "./src/index.js",
            },
          ],
        },
      },
    },
  ],
  makers: [
    {
      name: "@electron-forge/maker-squirrel",
      config: { name: "app", setupIcon: "./src/images/icons/icon.ico" },
    },
    {
      name: "@electron-forge/maker-zip",
    },
    {
      name: "@electron-forge/maker-deb",
      config: {
        options: {
          icon: "./src/images/icons/256x256.png",
        },
      },
    },
    {
      name: "@electron-forge/maker-rpm",
      config: {},
    },
  ],
};

Additional information

No response

Quoaymag commented 1 year ago

Go