Cutting edge websites for the 19th Century

Scaffolding an Angular + Bootstrap App

March 16th, 2017

Snohomish River, WA, cyanotype print

Given the complexity of modern JavaScript web applications, it makes sense to use a scaffolding tool to set up. Fountainjs is a good way to go if you want to have your app built and served with Gulp, Sass and Webpack. I went to Fountain’s Download page, and selected:

framework: Angular 1 (version 1.6 actually)

module and build: Webpack and NPM

JavaScript preprocessing: none

CSS preprocessing: Sass

template: Hello World

So, that’s a good start. Do npm install, and gulp serve, and you’ll be at Hello World.

Now suppose you want to use Bootstrap with your project? This is not so simple. First off if you just use the Get Bootstrap stuff, the scripts will be dependent on jQuery. I’m not the sort of dev who hates jQuery, and while its possible to use it on top of Angular, its not Best Practice. Angular has its own jqlite for DOM manipulation. If you use jQuery and Bootstrap, believe me, you’ll get errors. BTW, in this post I’m using Bootstrap 3. I have not used Bootstrap 4 yet.

The best way to add Bootstrap is to use Angular UI Bootstrap, which makes a nice set of Angular directives for Bootstrap’s javascript components. UI Bootstrap has no jQuery dependency. You do need to load angular-animate and angular-touch. And of course you need to load the Bootstrap CSS.

You can see that ui.bootstrap is added as a Module level dependency. All the directives that are nicely documented on angular-ui.github.io should be available in your app. All you need is the CSS and you’re good, right? Well that’s no so simple actually. I guess you could put a link to Bootstrap’s CSS in the document head, and you’d be fine, as long as you like the default Bootstrap look and feel. I prefer more control over things, and I don’t want to write elaborate CSS overrides.

What I need is to bring Boostrap’s Sass into the webpack build. And it turns out that there is another node module to do just that: bootstrap-loader. Go ahead and install that also, as a Dev Dependency. And require it in your index.js, as above.

The nice thing about bootstrap-loader is that you can configure it however you want (the bad part is that you have to). Do this by putting a .bootstraprc file (like this one) in your project root. This is written in yaml.

Now comes the hopefully fun part—configuration. Note that if you try to build with the default config, it won’t work. The first thing to do is turn off all javascript in bootstrap-loader. We only want the JS that comes with Angular UI Bootstrap, not Bootstrap’s own jQuery-dependent JS. So at the bottom of the bootstraprc, you’ll see scripts section. Just put in scripts: false, and then delete all the script component lines.

You will probably still be getting build errors. And check both dev and production builds. This is how I set my style loaders in bootstraprc:

I had to disable the Sass sourcemap to prevent webpack from adding a half-meg blob to my production CSS file.

You will probably be getting other errors if you do a production build (gulp serve:dist). It will break on the fonts because Fountain’s scaffolding does not account for font urls in Sass. The solution is to add the correct loaders to the webpack config.

First install url-loader: npm install url-loader --save-dev

Then add these two loaders to the loaders object in the /conf/webpack-dist.js file:

Hopefully your app is now building correctly and you have access to the Bootstrap CSS and component library. Webpack config is something of a black art, and it might take some time to tweak out errors.

You might be wonder when do you get to the fun part? How about this:preBootstrapCustomizations: './src/scss/bootstrap-config/_pre-customizations.scss'

You link this to a file changing any of Bootstrap’s Sass variables. Want to give your site a default text size of 17px and default width of 1200px? Easy: