0

I would like to build an app using Angular + Electron. My app should be able to run both on desktop and browser platforms. I'm considering to use angular-electron starter kit (but I'm open to other possibilities).

What concerns my is the way I read and write data. The data must be stored in a MySQL database. Ideally I would like to:

  • make the app call an api when NOT running on Electron (browser mode)
  • make the app query directly the mysql database when running on Electron (desktop mode)

I know I could check for window && window.process && window.process.type to let the app understand wheter running on Electron or not, however I'm a bit concerned about how to handle this. Also because I probably need to import node packages like mysqljs but ONLY in the desktop mode.

Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252

1 Answers1

1

You can simply pass in different environment files in Angular during build this allow you to control the environment variables so that you can tell that it is a web app build or an electron app build.

https://angular.io/guide/build

EXTRA

But if you are importing binary packages this is where it gets tricky. I dont think there is a clean way for you to do conditional imports. I did not manage to find a way to do it cleanly and sort of maintained another repository for all my services that needs to import binary files.

To import the binary files you will also need to edit some webpack settings to tell angular to not compile/include the binary files during the build process so that you can use you librarys like mysqljs that require binary files. There is also some settings on the electron end to make binary files compatible for different platforms ie Windows, Mac , Linux. Basically it is really a pain to do it.

Link to how to edit webpack settings for angular 7+

https://github.com/manfredsteyer/ngx-build-plus

I will totally suggest you not to do it unless you really have a very good reason that you need to use these libraries.

EDIT 10/1/19

Okay I was referring to the MySqlJs but it seems like it does not have native modules modules. Native/binary modules basically means javascript code that relies on c++ compiled binaries(Or any native language like rust...).

For my case I was using the grpc modules which has a native dependency. Had to switch to grpc-web in the end.

I will add some footnotes here if you ever need them

https://electronjs.org/docs/tutorial/using-native-node-modules

Node.js / npm - anyway to tell if a package is pure JS or not?

chronolegend
  • 474
  • 3
  • 13