caoxiemeihao / vite-electron-plugin

High-performance, esbuild-based Vite Electron plugin
MIT License
54 stars 7 forks source link

imported method (main process) is undefined #30

Closed johnheroy closed 2 years ago

johnheroy commented 2 years ago

Thanks for the reply in https://github.com/electron-vite/vite-electron-plugin/issues/17!

I have an Electron app and want to define the implementation for an IPC api in another file besides main/index.ts. I defined some functions i.e.

export function helloWorld() {
}

in another file electron/main/ipc/hello-world.ts and import this in main/index.ts (import { helloWorld } from 'electron/main/ipc/hello-world) but the function is undefined at runtime.

Is there any config to support including other code as ESM using this plugin?

this is what my vite config looks like:

plugins: [
    react(),
    electron({
      include: [
        'electron',
      ],
      transformOptions: {
        sourcemap: !!process.env.VSCODE_DEBUG,
      },
      // Will start Electron via VSCode Debug
      plugins: process.env.VSCODE_DEBUG
        ? [customStart(debounce(() => console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')))]
        : undefined,
    }),
    renderer({
      nodeIntegration: false,
    }),
  ],
johnheroy commented 2 years ago

i think I am having this issue because I'm using absolute paths for imports in my tsconfig but the require path doesn't resolve in the transformed .js by esbuild

caoxiemeihao commented 2 years ago

Add to include option

johnheroy commented 2 years ago

here is a trivial example: https://github.com/johnheroy/electron-test-app

stack trace when running npm run dev:

(node:32245) UnhandledPromiseRejectionWarning: TypeError: (0 , import_subpackage.helloWorld) is not a function
    at /Users/johnheroy/Workspace/electron-vite-test/dist-electron/electron/main/index.js:45:36
johnheroy commented 2 years ago

wondering if there's a particular way to configure this plugin or esbuild to support writing electron process code with absolute import paths, I'm also happy to help contribute if you can point me in the right direction! 谢谢🙏

johnheroy commented 2 years ago

Closing because I found a way using the alias plugin.