vite-plugin-electron-renderer icon indicating copy to clipboard operation
vite-plugin-electron-renderer copied to clipboard

Usage with electron-util

Open Stanzilla opened this issue 1 year ago • 0 comments

I'm trying to use https://www.npmjs.com/package/electron-util and getting the following error:

 Build failed in 14.28s
error during build:
node_modules/electron-util/distribution/main/active-window.js (1:9): "BrowserWindow" is not exported by "node_modules/.vite-electron-renderer/electron.mjs", imported by "node_modules/electron-util/distribution/main/active-window.js".
file: /home/stan/projects/personal/wago-app/node_modules/electron-util/distribution/main/active-window.js:1:9

wondering if that is my fault or actually a bug. Here is my vite config:

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { sentryVitePlugin } from '@sentry/vite-plugin';
import markdown from '@vavt/vite-plugin-import-markdown';
import react from '@vitejs/plugin-react-swc';
import { defineConfig } from 'vite';
import type { UserConfig } from 'vite';
import electron from 'vite-plugin-electron';
import renderer from 'vite-plugin-electron-renderer';

import pkg from './package.json';

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  const isBuild = command === 'build';
  const sourcemap = isBuild;

  const __filename = fileURLToPath(import.meta.url);
  const __dirname = path.dirname(__filename);

  return {
    root: __dirname,
    plugins: [
      react(),
      markdown(),
      electron([
        {
          // Main-Process entry file of the Electron App.
          entry: 'src/main/main.ts',
          onstart(options) {
            if (process.env.VSCODE_DEBUG) {
              console.log(
                /* For `.vscode/.debug.script.mjs` */ '[startup] Electron App',
              );
            } else {
              options.startup();
            }
          },
          vite: {
            build: {
              sourcemap,
              minify: isBuild,
              outDir: 'dist-electron/main',
              rollupOptions: {
                external: Object.keys(
                  'dependencies' in pkg ? pkg.dependencies : {},
                ),
                output: {
                  format: 'esm',
                  entryFileNames: '[name].mjs',
                },
              },
            },
          },
        },
        {
          entry: 'src/preload/index.ts',
          onstart(options) {
            // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
            // instead of restarting the entire Electron App.
            options.reload();
          },
          vite: {
            build: {
              sourcemap: sourcemap ? 'inline' : undefined,
              minify: isBuild,
              outDir: 'dist-electron/preload',
              rollupOptions: {
                external: Object.keys(
                  'dependencies' in pkg ? pkg.dependencies : {},
                ),
                output: {
                  format: 'esm',
                  entryFileNames: '[name].mjs',
                },
              },
            },
          },
        },
      ]),
      renderer({
        resolve: {
          regedit: { type: 'cjs' },
          'fs-extra': { type: 'cjs' },
          'electron-util': { type: 'esm' },
        },
      }),
      sentryVitePlugin({
        authToken: process.env.SENTRY_AUTH_TOKEN,
        org: 'method-gaming-limited',
        project: 'wago-app',
        telemetry: false,
      }),
    ],
    build: {
      sourcemap,
      target: 'es2022',
      rollupOptions: {
        output: {
          assetFileNames: 'assets/[name].[ext]',
        },
      },
    },
    server: process.env.VSCODE_DEBUG
      ? (() => {
          const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL);
          return {
            host: url.hostname,
            port: +url.port,
            hrm: true,
          };
        })()
      : { hmr: true },
    clearScreen: false,
  } satisfies UserConfig;
});

electron-util is in dependencies, not devDependencies and I never import BrowserWindow in the renderer, only main.

Stanzilla avatar Jul 26 '24 22:07 Stanzilla