24

I am experimenting with electron. I see a lot of examples that use webpack.

But why use something like webpack? Because as far as I can tell electron supports require('module').

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
Rutger
  • 1,163
  • 3
  • 12
  • 29
  • Electron is on top of node. To inlcude a node module you use the require function. https://nodejs.org/dist/latest-v5.x/docs/api/modules.html – apxp Apr 14 '16 at 06:00
  • I know so the only reason to use webpack (in this scenario) is when you want to bundle your files? – Rutger Apr 14 '16 at 06:47
  • 4
    Yes. And what if your files are not JavaScript and needed to compile? For example react components have the ending jsx and need to be compiled into JS. – apxp Apr 14 '16 at 07:55
  • 1
    Webpack is used to bundle front-end JS code, it has nothing to do with Electron, but you can use to compile the app you'll bundle and use along with Electron – martpie Apr 14 '16 at 08:03
  • @KeitIG How would I bundle that? Any example code? – Socrates May 11 '18 at 09:25
  • I was having hard time with webpack. In the end, I just need babel, and it works great – onmyway133 Aug 16 '18 at 12:19

4 Answers4

16

Webpack is not just a JS module bundler; it can be used for bundling static assets (inline base64 of images, for example), compiling Sass/Less/Stylus/CSS-Modules, dead code elimination, tree-shaking, and more. With the proper loaders and config, one only needs to require('any-type-of-file.extension') when actively developing. In my personal experience, however, more than all of that, Webpack is valuable because of it's dev-server and Hot Module Replacement (HMR), which makes Live Reload feel like something from the dark ages.

To recap, you get all the combined power of Gulp/Browserify/Rollup, but with HMR on top, all within a single tool (and lots and lots and lots and lots of loaders ;).

Setting up Webpack is a PITA, no doubt, but if you plan on working on an Electron app for a good amount of time, the time saved from HMR alone is well worth it.

Lokua
  • 576
  • 5
  • 15
  • 3
    electron-compile, an already popular development library for electron, appears to support HMR: https://github.com/electron/electron-compile#live-reload--hot-module-reloading – Scott Coates Aug 29 '17 at 23:03
  • 5
    As @ScottCoates has already said, there are a few options for hot reloading in Electron which just watch the filesystem for changes. The whole reason HMR is needed in webpack is because there isn't any way for the browser to be notified of changes. This isn't necessary for Electron and it adds a TON of unnecessary complexity to get webpack HMR working in Electron. – Osman Sep 05 '17 at 01:52
12

It is not webpack which is used in electron. The require function is part of the node.js, which is the base for electron.

More information about the modules from the node.js docs are available here.

But, as long as webpack is also available as a node module, it is also possible to use webpack with electron. At this point you are also able to use the build on the fly in production, because node and chrome are availiable inside one app.

Why use webpack with electron?

When you use react or vue.js components maybe it is a good idea to separate the components. To bundle your code into one app you need browserfy or webpack. That would be, for example, a good case on why to use it.

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
apxp
  • 5,240
  • 4
  • 23
  • 43
8

There is no reason to use Webpack in Electron, check out electron-compile to use Babel and LESS in Electron.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
4

Good documentation to get started with: Webpack Electron Build

  • Use of webpack for source code bundling
  • Use of webpack-dev-server for development
  • HMR for both renderer and main processes
  • Use of @babel/preset-env that is automatically configured based on your electron version
  • Ability to add custom webpack loaders, plugins, etc.
  • Add-ons to support items like TypeScript, Less, EJS, etc.
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
Pratap Dessai
  • 139
  • 2
  • 3
  • 1
    That project is currently [having issues](https://github.com/electron-userland/electron-webpack/issues/428), so I wouldn't recommend – Tyler Dane Jul 23 '21 at 14:42