alex8088 / electron-vite

Next generation Electron build tooling based on Vite 新一代 Electron 开发构建工具,支持源代码保护
https://electron-vite.org
MIT License
3.57k stars 153 forks source link

Multiple issues building project #524

Closed krystianotto closed 5 months ago

krystianotto commented 5 months ago

Describe the bug

I have a few issues moving from webpack to vite. First, one of my dependencies has a dynamic import defined like this:

function getNativeNode(){
  return require("./build/package/module.node");
}

I then import it as follows:

import { getNativeNode } from 'test-module';

When I run electron-vite build I get an error:

[vite:node-asset] Could not load C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node?commonjs-proxy (imported by main/node_modules/test-module/index.js): The argument 'path' must be a string or Uint8Array without null bytes. Received '\\x00C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node'
TypeError [PLUGIN_ERROR]: Could not load C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node?commonjs-proxy (imported by main/node_modules/test-module/index.js): The argument 'path' must be a string or Uint8Array without null bytes. Received '\\x00C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node'
    at open (node:internal/fs/promises:586:10)
    at Object.readFile (node:internal/fs/promises:1025:20)
    at Object.load (file:///C:/Users/krystian/Desktop/test/node_modules/electron-vite/dist/chunks/lib-iT7MOERj.mjs:634:47)
    at Object.handler (file:///C:/Users/krystian/Desktop/test/node_modules/vite/dist/node/chunks/dep-BKbDVx1T.js:68900:19)
    at file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:19774:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:19674:28)
    at async file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:18845:33
    at async Queue.work (file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:19884:32)

Since I'm able to maintain this dependency I can actually change it to:

const path = require('path');

function getNativeNode(){
  return require(path.resolve(__dirname, "./build/package/module.node"));
}

It works, but only because I can change the code, which is not pleasant. Unfortunately, there is now another error regarding dynamic import.

Screenshot 2024-06-03 140229

I was able to fix this by moving this dependency to the root package.json, but again this is not desirable. After fixing these two dependencies manually, another problem appeared after packaging the application with electron-builder:

Screenshot 2024-06-03 140507

Here is repo to reproduce the issues.

Electron-Vite Version

2.2.0

Electron Version

25.9.8

Vite Version

5.12.2

Validations

alex8088 commented 5 months ago

Since you installed the module through the github address, the module is not recognized by the externalizeDep plugin. You can externalize test-module by specifying it via the include option of the externalizeDeps plugin.

https://electron-vite.org/guide/dev#dependencies-vs-devdependencies

krystianotto commented 5 months ago

@alex8088 it still shows module not found after I pack the application using electron-builder. Any idea why that is?

alex8088 commented 5 months ago

How did you configure it?

krystianotto commented 5 months ago

@alex8088 Here: https://github.com/krystianotto/test-electron-vite/blob/master/main/electron-builder.config.js

alex8088 commented 5 months ago

Your electron-vite config

alex8088 commented 5 months ago
export default defineConfig(() => {
  return {
    main: {
      plugins: [
        externalizeDepsPlugin({ include: ['test-module'] }),
      ],
      ...
    },
    ...
});
krystianotto commented 5 months ago

@alex8088 Yes - I have already done it, but after npm package when I run the application I still get the error from the last screenshot attached in the description :/ I wonder if that's an issue with Vite or electron-builder.

alex8088 commented 5 months ago

You should move your electron-builder config file to project root dir.

alex8088 commented 5 months ago

It is recommended that you use npm create @quick-start/electron@latest to create the project.

krystianotto commented 5 months ago

@alex8088 I already tried with the electron-builder configuration in the root directory. That didn't help. Also, creating a new project is out of the question, as this repo is just an example of a problem I encountered in an existing one.

krystianotto commented 5 months ago

I was able to make it work by adding this code to electron-builder: files: ["node_modules/test-module/**/*"] To be fair I'm not too fond of this approach and don't get why it is necessary to move this dependency manually with electron-builder