Understanding webpack
Webpack is a module bundler that takes all your assets and turns them into easily distributable packages. Once you understand it, it can drastically simplify your workflow, but getting started with it can be painful.
What I found difficult in starting to use it, was that I didn’t have a good model of what was going on. It seemed like magic. This made it hard to fix errors when they occurred and scared me away from trying to tailor the configurations I was using.
So I wrote myself an idiots guide to configuring Webpack. It helped me, maybe it can help you.
“Idiots guide to Webpack configuration”
Webpack bundles modules for web applications. Pretty much anything can be treated as a module. Images, CSS, JS, HTML, preprocessor files (.pug / .styl / .sass etc)… anything. It takes in all your modules (or files), figures out their dependencies and outputs some nice static assets, structured the way you want.
Node modules setup
The modules we need to npm install
for this are:
- babel-core
- babel-loader
- babel-preset-latest
- css-loader
- extract-text-webpack-plugin
- html-webpack-plugin
- pug
- pug-loader
- stylus
- stylus-loader
- webpack
Project setup
We will use a basic example project structure to help make this example easy to understand. This theoretical project contains ES2015 javascript code - split into some modules. It has styles written with a CSS preprocessor (Stylus) and a html template written with an HTML preprocessor (Pug).
| Source files
|
|
| Desired output
|
|
| Example contents of JS files
|
|
|
|
|
|
Configuration file
Webpack uses a javascript file to configure it’s operations. By default it looks for a file named webpack.config.js, but you can call it anything and pass it as the --config
option. There are four key parts of this configuration file that will get your Webpack build going.
Entry
The starting point of your application. This option tells Webpack where to start. It will follow dependencies it finds in this file. It will then do the same for each dependency it finds.
|
|
Output
The output option tells Webpack how to output your bundled assets.
|
|
Loaders
Webpack treats every file as a module, but it can only understand javascript. Loaders transform files into modules as they’re processed. Loaders identify which files they should handle with the test property and they process and transform the files so they can be added to your bundle.
|
|
Plugins
Loaders only run transforms on a per-file basis, once you have compiled your files into a bundle you can use plugins to further customise and perform actions on your outputted bundle.
To use a plugin, you require() it and add it to the plugins array. Most plugins have options that can be customised when they are used. You initiate the plugin by calling it with new
. This is because you can use the same plugin multiple times with different configurations.
|
|
Run webpack
If you have named your config with the default name, like in the above example you only need to run webpack
from the command line in the same directory as your Webpack config. If you named it something else, say ‘config.js’, you can pass the configuration file to Webpack on the CLI like so: webpack --config config.js
Thats it. Webpack will process the files and output the finished product into the directory structure you want, ready to deploy to a webserver.
Not covered here
There’s a plethora of options that are not going to be in this. Not because they aren’t useful but because they can be safely ignored in the interest of understanding. Once you have wrapped your brain around the general model of whats occurring, I find the rest is all fairly simple.