Electron & ES6 how to implement require remote/ipc when using gulp and ES6 modules
In my case, I'm using browserify with babelify, if I tried:
var remote = require('remote')
I would got error:
Error: Cannot find module 'remote' from xxx
But if I tried
var remote = window.require('remote')
It works.
import remote from 'remote'
doesn't work, seems like browserify can't find those native modules not defined in package.json
.
Well been playing and I have managed to get this to work in a way:
Basically i set the remote and ipc modules within the html page, then pass in those, into my class for that page.
<script> var remote = require('remote'); var ipc = require('ipc'); new Main(ipc); </script>
export default class Main extends Vue{ constructor(ipc) { .... ipc.send('listener here','message here'); .....
The files can be viewed within this Branch:
Honestly, the easiest way to solve this is to not minify your binaries or use browserify. Electron already has require
in the global scope - all you need to do is run your files through Babel to ES6 => ES5 compile them (electron-compile makes this trivially easy too). Your import
statement will get translated to a require
, which Electron will automatically handle out of the box.
In general, a lot of optimization strategies that you're used to on the web like minification or concatenation are unnecessary or don't make sense in Electron, you can mostly just not do them!