pglite icon indicating copy to clipboard operation
pglite copied to clipboard

Vite Electron bundler broken

Open hyperknot opened this issue 7 months ago • 2 comments

  1. pnpm create vite@latest
  2. Select Others / Electron / Vanilla
  3. pnpm i @electric-sql/pglite
  4. pnpm dev works
  5. add pglite to electron/main.ts:
import { PGlite } from '@electric-sql/pglite'
function createWindow() {
  new PGlite()
  ...

It terminates with:

(node:76092) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at splitPath (node:electron/js2c/node_init:2:879)
    at t.promises.readFile (node:electron/js2c/node_init:2:9696)
    at Er (file:///Users/user/Documents/dev/temp/___/x/dist-electron/main-D4Mqq_Kt.js:606:54)
(Use `Electron --trace-warnings ...` to show where the warning was created)
(node:76092) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:76092) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at splitPath (node:electron/js2c/node_init:2:879)
    at t.promises.readFile (node:electron/js2c/node_init:2:9696)
    at Er (file:///Users/user/Documents/dev/temp/___/x/dist-electron/main-D4Mqq_Kt.js:606:54)
(node:76092) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

The trace-warning can be enabled like this in vite.config.ts Also added optimizeDeps but it didn't make a difference.

import { defineConfig } from 'vite'
import path from 'node:path'
import electron from 'vite-plugin-electron/simple'

// https://vitejs.dev/config/
export default defineConfig({
  optimizeDeps: {
    exclude: ['@electric-sql/pglite'],
  },
  plugins: [
    electron({
      main: {
        // Shortcut of `build.lib.entry`.
        entry: 'electron/main.ts',

        onstart: ({ startup }) => {
          // Check if we are in development mode (optional, but good practice)
          if (process.env.NODE_ENV === 'development') {
            // Call the provided startup function with the --trace-warnings flag
            startup(['.', '--no-sandbox', '--trace-warnings'])
          } else {
            // For other environments (build, etc.), use the default startup
            startup(['.', '--no-sandbox'])
          }
        },
      },
      preload: {
        // Shortcut of `build.rollupOptions.input`.
        // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
        input: path.join(__dirname, 'electron/preload.ts'),
      },
      // Ployfill the Electron and Node.js API for Renderer process.
      // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
      // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
      renderer: process.env.NODE_ENV === 'test'
        // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
        ? undefined
        : {},
    }),
  ],
})

With trace warning, it returns:

(node:76192) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at splitPath (node:electron/js2c/node_init:2:879)
    at t.promises.readFile (node:electron/js2c/node_init:2:9696)
    at Er (file:///Users/user/Documents/dev/temp/___/x/dist-electron/main-D4Mqq_Kt.js:606:54)
    at emitUnhandledRejectionWarning (node:internal/process/promises:298:15)
    at warnWithErrorCodeUnhandledRejectionsMode (node:internal/process/promises:406:5)
    at processPromiseRejections (node:internal/process/promises:470:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)
(node:76192) TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at splitPath (node:electron/js2c/node_init:2:879)
    at t.promises.readFile (node:electron/js2c/node_init:2:9696)
    at Er (file:///Users/user/Documents/dev/temp/___/x/dist-electron/main-D4Mqq_Kt.js:606:54)
(node:76192) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at splitPath (node:electron/js2c/node_init:2:879)
    at t.promises.readFile (node:electron/js2c/node_init:2:9696)
    at Er (file:///Users/user/Documents/dev/temp/___/x/dist-electron/main-D4Mqq_Kt.js:606:54)
    at emitUnhandledRejectionWarning (node:internal/process/promises:298:15)
    at warnWithErrorCodeUnhandledRejectionsMode (node:internal/process/promises:406:5)
    at processPromiseRejections (node:internal/process/promises:470:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)
(node:76192) TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at splitPath (node:electron/js2c/node_init:2:879)
    at t.promises.readFile (node:electron/js2c/node_init:2:9696)
    at Er (file:///Users/user/Documents/dev/temp/___/x/dist-electron/main-D4Mqq_Kt.js:606:54)

new PGlite('memory://') also doesn't help

hyperknot avatar Apr 21 '25 23:04 hyperknot

I also tried vite-plugin-native, which is necessary for sqlite3 and better-sqlite3, but made no change here.

import native from 'vite-plugin-native'

...
electronSimple({
      main: {
        // Shortcut of `build.lib.entry`.
        entry: 'electron/main/main.ts',
        vite: {
          plugins: [
            native({
              webpack: {},
            }),
          ],
        },

hyperknot avatar Apr 22 '25 10:04 hyperknot

See https://github.com/electric-sql/pglite/issues/398

jcuenod avatar Apr 22 '25 15:04 jcuenod