Electron-builder: How to use different codebase for different build targets?

Created on 7 Oct 2017  Â·  5Comments  Â·  Source: electron-userland/electron-builder

Hi guys,

Is it possible for electron-builder to build app with different codebase based on specific target?

For specific OS platforms we can use it the following way:

if (os.platform == 'linux') { console.log('Hello Linux') }
else if (os.platform == 'win32') { console.log ('Hello Windows') }
else if (os.platform == 'darwin') { console.log('Hello macOS') }

Can we accomplish something similar with electron-builder:

if (target == 'appx') { removeUpdates() }
else if (target == 'appimage') { console.log('I love Linux') }  

I believe there should be the way. Just kindly ask you for the direction.

Thanks!

discuss question

Most helpful comment

@vasyl-shumskyi I guess you mean within the application code, not in the build process, right?
I guess we could add some env var telling which target was used but I'm not sure if we should.

A rather easy way would be to have some separate file that'll be copied for certain targets. You could have a .target file containing the target as a string. Or you could, prior to building, write some const TARGET='whatever' right in your application code.
There might also be a way to just detect whether the app was installed via store or not. The windows apps dir mimics the native directory structure but maybe you can get the full (real) path to your application and see whether it's within a special directory or not. There could also be some win API or store helper functionality.
I think any of these possible solutions would be better than having electron-builde fiddle with the runtime or vars.

If I were you first thing I'd do is play around with fs and see what's the installation path. Or take a look at something like this: https://www.npmjs.com/package/@nodert-win10/windows.applicationmodel.store.preview

Hope that helps.

All 5 comments

@vasyl-shumskyi I guess you mean within the application code, not in the build process, right?
I guess we could add some env var telling which target was used but I'm not sure if we should.

A rather easy way would be to have some separate file that'll be copied for certain targets. You could have a .target file containing the target as a string. Or you could, prior to building, write some const TARGET='whatever' right in your application code.
There might also be a way to just detect whether the app was installed via store or not. The windows apps dir mimics the native directory structure but maybe you can get the full (real) path to your application and see whether it's within a special directory or not. There could also be some win API or store helper functionality.
I think any of these possible solutions would be better than having electron-builde fiddle with the runtime or vars.

If I were you first thing I'd do is play around with fs and see what's the installation path. Or take a look at something like this: https://www.npmjs.com/package/@nodert-win10/windows.applicationmodel.store.preview

Hope that helps.

Our official recommendation — use https://github.com/electron-userland/electron-webpack I mean — use webpack. It allows you do whatever you want and more. e.g. you can define ELECTRON_WEBPACK_APP_YOUR_NAME env and webpack will eliminate dead code branches.

electron-builder itself are not going to reinvent the wheel. It is our official position and rule — i.e. it is why we use proven and existing solutions like FPM, NSIS. The same applies here.

The another issue — is that you cannot build more than 1 target, because app is packed only once during build per platform, and then targets applies. This issue will be fixed only when some user will ask us to fix it and provide details :) For example, if it will be required for webpack — it is a little bit different story due to internal reasons than generic case.

Thanks for your answers guys!

I revised my code logic and added build specific true / false switches to package.json like so -> appx: true, snap: true etc

Thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xingoxu picture xingoxu  Â·  3Comments

omarkilani picture omarkilani  Â·  3Comments

JohnWeisz picture JohnWeisz  Â·  3Comments

alexstrat picture alexstrat  Â·  3Comments

StickNitro picture StickNitro  Â·  3Comments