#[solved]problem build electron with Vite , react

190 messages · Page 1 of 1 (latest)

cosmic island
#

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??

fringe beacon
#

what does ur dist look like?

cosmic island
#

My dist is like that

slate pendant
#

And what is the resource that fail to load?

cosmic island
#

Main.jsx is the problem i think..

fringe beacon
#

it looks like your app code is trying to load a main.jsx

#

where is that declared?

cosmic island
#

I didn't touch other config

slate pendant
#

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

cosmic island
#

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

slate pendant
#

Good question

fringe beacon
#

that sounds like something with your app code

#

do you have a main.jsx file?

cosmic island
#

yes

#

but i compile all files using vite

#

So it should be js file right?

slate pendant
#

It's a .js in dist

cosmic island
#

Yes, but Why they use jsx..

alpine trail
#

how your index.html looks like?

cosmic island
#

in dist?

alpine trail
#

you have index.html in dist?

cosmic island
#

yes i have

alpine trail
#

and exe files in dist?

cosmic island
#

yes i have both

#

and all js, css files in assets folder

slate pendant
#

Could you try with this in your build conf and see if it works?

extraMetadata: {
  main: "public/main.js"
},
cosmic island
#

okay I'm trying now

#

It's same fail..

slate pendant
#

hu... weird

#

Oh wait

#

one of your path is wrong

cosmic island
#

I have a question, Should i add git repository?

#

where?!!

slate pendant
#

when you load the window url

#

in prod

#

the index is in the same folder as main

#

so you should remove ../

cosmic island
#

jus 'index.html' right:?

slate pendant
#

I think so

#

but I don't know if it will solve the problem

cosmic island
#

okay another trying!!

#

not allowed to load local resource

#

maybe path is wrong

slate pendant
#

okay... I guess you had the right path

#

but I don't understand

#

oh okay

#

do you bundle your files?

#

with webpack?

cosmic island
#

No. I use Vite instead of webpack

slate pendant
#

Oops sorry, I didn't realize Vite could replace webpack

cosmic island
#

I also not sure.. but they are same purpose I think..

slate pendant
#

Okay, but do you have a build folder with vite?

cosmic island
#

No just use dist folder

slate pendant
#

What does vite build do?

#

hehe

#

it builds in a folder named dist

#

just like electron-builder

#

let's try to change this

cosmic island
#

you mean change folder name to build?

slate pendant
#

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

cosmic island
#

wait for a minutes.. build error hehe

#

It's not working..

slate pendant
#

what error do you have?

cosmic island
#

app\build\main.js does not exist

slate pendant
#

okay

#

how is your build folder?

cosmic island
#

The result of vite build is in build but electron-builder couldn't know..

slate pendant
#

electron-builder knows with the config I sent you

cosmic island
#

The contents are same with dist folder

slate pendant
#

not possible

#

build is for vite

#

dist for electron-builder

#

you should only have your compiled source file in the build folder

cosmic island
#

So seperate folder between vite and electron?

slate pendant
#

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

cosmic island
#

i did like this, Is it wrong?

slate pendant
#

Where do you compile for Vite?

#

remove

"directories": {
  "output": "build"
}
#

if Vite compiles in build then electron-builder should pack in another folder

cosmic island
#

default compile option for vite is dist

slate pendant
#

Yes

#

so you either need to change Vite's or electron-builder's output folder

cosmic island
#

So if i add directories option like that they are using each folder, isn't it??

slate pendant
#

err...

cosmic island
#

Sorry I didn't know well..

slate pendant
#

It's okay

#

Let's try again

#

Vite default build folder is named dist

#

electron-builder default build folder is also named dist

cosmic island
#

yes at first

slate pendant
#

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

cosmic island
#

okay i understand

slate pendant
#

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?

cosmic island
#

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

slate pendant
#

yes

cosmic island
#

So should i also add the asset folder to electron-builder option?

#

like this?
"extraMetadata": {
"main": "build/main.js",
"assets": "build/assets"
}

slate pendant
#

no

cosmic island
#

because there is another error they couldn't load file index.js and index.css

slate pendant
#

extraMetadata is an option to override package.json

#

how are your build and dist folders now?

cosmic island
#

in the build file there is index.html , main.js, assets folder

slate pendant
#

and what path do you use the load index.html in your window?

cosmic island
#

file://${path.join(__dirname, '../build/index.html')}

#

I use this one

slate pendant
#

wrong path

#

You said main.js is in the same folder as index.html

#

try with file://${path.join(__dirname, 'index.html')}

cosmic island
#

okay try again now

#

it's same they couldn't find css and js file

slate pendant
#

Can you show the error please?

cosmic island
#

folder is this

slate pendant
#

Okay, can you check in the network tab what is the path for these files?

cosmic island
#

Only path is wrong now!

#

should i change ' file://' to ' app://' ?

slate pendant
#

what's are the paths in index.html in build?

cosmic island
#

don't know.. just use __dirname

slate pendant
#

open the file and look

#

in your editor

cosmic island
#

what file? index.html??

slate pendant
#

yes

cosmic island
#

I certain use __dirname why the path is like that?

slate pendant
#

These paths are managed by Vite

#

I don't know if it will works but try to add "homepage": "./" in your package json

cosmic island
#

I already add that option

slate pendant
#

okay

cosmic island
#

how about change path to ./index.html ?

#

for relative path

slate pendant
#

I guess you can try

#

these script and link tags in the html are added by Vite, right? Not by you?

cosmic island
#

yse by VIte

#

ahh same result..

slate pendant
#

I feel like we're missing something

#

At least the main and index.html are correctly loaded now

cosmic island
#

yes, you are right!

slate pendant
#

and you do have the assets folder in dist?

cosmic island
#

no i don't have

slate pendant
#

Ohhh

#

ah ah

#

But it should be here, since it's in build

#

What do you have in dist?

cosmic island
#

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

slate pendant
#

Okay, so the assets folder is here

cosmic island
#

I'm trying to set the assetsDirectory in vite option

slate pendant
#

I was looking at the base option

#

But I'm really not familiar with vite

#

base: "./"

cosmic island
#

My try is wrong.. your solution trying now

#

shit!!!!!!!!! It work!!!!!!!!!!

slate pendant
#

Finally!

cosmic island
#

Thanks!! my master!!!!!!!

slate pendant
#

lol

cosmic island
#

Really Thanks!!! without you i couldn't do that!!!

slate pendant
#

don't forget to set back homepage to ./ if you changed it because it's used for routing

#

I'm glad I could help

cosmic island
#

yes My load!!

slate pendant
#

I never checked Vite before so it was interesting

cosmic island
#

I don't know who you are, I'll pray for you are always be happy and be nice!!

slate pendant
#

Ah ah thank you

cosmic island
#

👍

cosmic island
#

[solved]problem build electron with Vite , react

fringe beacon
#

@cosmic island once you've solved your issue, you can "Close Post" in the discord forum menu