Custom Building Bootstrap and jQuery With Webpack
When using bootstrap its always been simple to slim down the css by removing some ‘@imports’ in your less or scss files. Getting rid of that extra bloat in js can be done in the same way thanks to webpack. If you’re not familiar with webpack it packs js modules into common js assets. Each component of bootstrap and jQuery are split into modules, using webpack loaders we can import only the modules we are using.
The two loader we will need is amd-define-factory-patcher-loader for jQuery and rollup-loader. If you haven’t don’t already have the babel-loader we will need that too for bootstrap. A sample module config now looks like this.
When bootstrap imports jQuery it uses the full version but doesn’t use it all. To get around this we can use aliases to resolve ‘jquery’ to our built version, and ‘jQuery’ to the node_modules version, notice the uppercase “Q” to define the two versions
Our directory structure is now
In ‘bootstrap.js’ and ‘jQuery.js’ it now looks a lot more like our scss imports file where we can just comment put the modules we are not using. Just the same as I always do with the “jubotron” css component.
Finally in our ‘index.js’ we can require out custom build of bootstrap and intern will only require the js modules we are actually using.