Hello guys, I have a question about electron build.
I'm using Vite + react + electron now. but in local condition, everything is working well.
But after build, There is an Error that Fail to load resouce (I think this is the reason, and the extension is main.jsx not main.js)
Could anyone help this problem??Hello guys, I have a question about electron build.
I'm using Vite + react + electron now. but in local condition, everything is working well.
But after build, There is an Error that Fail to load resouce (I think this is the reason, and the extension is main.jsx not main.js)
Could anyone help this problem??
#[solved]problem build electron with Vite , react
190 messages · Page 1 of 1 (latest)
what does ur dist look like?
My dist is like that
And what is the resource that fail to load?
I didn't touch other config
try to add extends: null to your build config
It prevents electron-builder from using CRA config but I'm not sure you're using CRA here
thanks for applying!! I'm trying to build now
It's same.. fail to load main.jsx
why the file extion is jsx? not js
Good question
It's a .js in dist
Yes, but Why they use jsx..
how your index.html looks like?
in dist?
you have index.html in dist?
yes i have
and exe files in dist?
Could you try with this in your build conf and see if it works?
extraMetadata: {
main: "public/main.js"
},
when you load the window url
in prod
the index is in the same folder as main
so you should remove ../
jus 'index.html' right:?
okay... I guess you had the right path
but I don't understand
oh okay
do you bundle your files?
with webpack?
No. I use Vite instead of webpack
Oops sorry, I didn't realize Vite could replace webpack
I also not sure.. but they are same purpose I think..
Okay, but do you have a build folder with vite?
No just use dist folder
What does vite build do?
hehe
it builds in a folder named dist
just like electron-builder
let's try to change this
you mean change folder name to build?
yes
and then only pack the build folder with electron-builder using the config:
files: ["./build/**/*"],
You will also have to set :
extraMetadata: {
main: "build/main.js"
},
So your main entry point is the file in the build folder and not in public
And you will need to check the path to load your window in prod is correct according to the build folder too
what error do you have?
app\build\main.js does not exist
The result of vite build is in build but electron-builder couldn't know..
electron-builder knows with the config I sent you
The contents are same with dist folder
not possible
build is for vite
dist for electron-builder
you should only have your compiled source file in the build folder
So seperate folder between vite and electron?
yes of course, if you compile everything in a single folder you will have conflicts
that's why you need to change the output folder of vite since it sports the same name as electron-builder's
or change electron-builder's output folder name if you prefer
i did like this, Is it wrong?
Where do you compile for Vite?
remove
"directories": {
"output": "build"
}
if Vite compiles in build then electron-builder should pack in another folder
default compile option for vite is dist
So if i add directories option like that they are using each folder, isn't it??
err...
Sorry I didn't know well..
It's okay
Let's try again
Vite default build folder is named dist
electron-builder default build folder is also named dist
yes at first
when you run npm/yarn build, you call both vite build and electron-builder
which mean that everything is merged to the dist folder
which is bad, because you might get conflicts
okay i understand
so, you need to change either the config of Vite OR the config of electron-builder so one of them use another folder than dist
once it's done, you want to tell electron-builder to use the file bundled with Vite
for that, you will give it the path to main in the output folder of Vite with the option extraMetadata
if you don't do that, electron-builder will use the main option of your package.json
but the path is public/main.js which is a non-bundled file so you don't want to use that
and finally, since Vite does all the nice job to bundle your files, you want electron-builder to only pack these files and not your source files (which are not bundled). So you can use the option files to tell it what folder you want to pack (= the output folder of Vite)
Do you understand?
okay i understand!
So i change vite build folder to build, and tell the electron-builder files are build and extraMetadata is build/main.js
yes
So should i also add the asset folder to electron-builder option?
like this?
"extraMetadata": {
"main": "build/main.js",
"assets": "build/assets"
}
no
because there is another error they couldn't load file index.js and index.css
extraMetadata is an option to override package.json
how are your build and dist folders now?
in the build file there is index.html , main.js, assets folder
and what path do you use the load index.html in your window?
wrong path
You said main.js is in the same folder as index.html
try with file://${path.join(__dirname, 'index.html')}
Can you show the error please?
Okay, can you check in the network tab what is the path for these files?
what's are the paths in index.html in build?
don't know.. just use __dirname
what file? index.html??
yes
These paths are managed by Vite
I don't know if it will works but try to add "homepage": "./" in your package json
I already add that option
okay
I guess you can try
these script and link tags in the html are added by Vite, right? Not by you?
I feel like we're missing something
At least the main and index.html are correctly loaded now
yes, you are right!
and you do have the assets folder in dist?
no i don't have
Ohhh
ah ah
But it should be here, since it's in build
What do you have in dist?
in dist i have exe file, win-unpacked folder and two Yaml files
in win-unpacked folder there are two folder and some files. in the resource folder i have app folder
inside of app there is build folder and that folder is our destination
index.html , main.js and assets folder that includes css and js files
Okay, so the assets folder is here
I'm trying to set the assetsDirectory in vite option
I was looking at the base option
But I'm really not familiar with vite
base: "./"
Finally!
Thanks!! my master!!!!!!!
lol
Really Thanks!!! without you i couldn't do that!!!
don't forget to set back homepage to ./ if you changed it because it's used for routing
I'm glad I could help
yes My load!!
I never checked Vite before so it was interesting
I don't know who you are, I'll pray for you are always be happy and be nice!!
Ah ah thank you
👍
[solved]problem build electron with Vite , react
@cosmic island once you've solved your issue, you can "Close Post" in the discord forum menu