capacitor-community / sqlite

Community plugin for native & electron SQLite databases
MIT License
426 stars 104 forks source link

Electron Vite: Error resolving capacitor.config.js #505

Closed ygarg465 closed 4 months ago

ygarg465 commented 4 months ago

Describe the bug I am using electron-vite for the electron platform which builds the application to the build folder with respective folders for main, preload and renderer. And I am using capacitor.config.ts which is not automatically included in build folder by electron-vite so I use tsc to compile it to that folder, as this file is required by capacitor-sqlite plugin.

But even after doing all of this I get an error when running with electron-vite dev that Error: Could not dynamically require "path\to\electron\build\capacitor.config.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.

So I added this to electron.vite.config.ts

commonjsOptions: { dynamicRequireTargets: ["build/capacitor.config.js"], },

Now electron.vite.config.ts file looks like this

import { defineConfig, externalizeDepsPlugin } from "electron-vite";
import { resolve } from "path";

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
    build: {
      outDir: "./build/main",
      rollupOptions: {
        input: {
          index: resolve(__dirname, "src/main/index.ts"),
        },
      },
      commonjsOptions: {
        dynamicRequireTargets: ["build/capacitor.config.js"],
      },
    },
  },
  preload: {
    plugins: [externalizeDepsPlugin()],
    build: {
      outDir: "./build/preload",
    },
  },
  renderer: {
    build: {
      rollupOptions: {
        external: ["app"],
        input: {
          index: "./src/renderer/index.html",
        },
      },
    },
  },
});

Now this below error pops up.

build the electron main process successfully

-----

"__require" is not exported by "build/capacitor.config.js", imported by "commonjs-dynamic-modules".
file: commonjs-dynamic-modules:1:9
1: import { __require as require0 } from "...\\electron\\build\\capacitor.c...
            ^
2: 
3: var dynamicModules;

Expected behavior It should compile and run successfully and resolve capacitor.config.js file

Desktop (please complete the following information):