The latest and greatest version of Bootstrap (version 4) sees a few changes from version 3.x. Adding it to Aurelia is a breeze if you follow these simple steps.
Please note: these instructions are for installing Bootstrap 4 into an Aurelia CLI application that is using RequireJS. Other topics will cover Bootstrap using Webpack.
Install Bootstrap
The CLI does ship with an au install
command, however, we’ll need to modify our aurelia.json
file to make some additional changes for supporting Bootstrap that we’ll just install it the old fashioned way via Npm.
npm install bootstrap --save
Bootstrap also has a requirement of jQuery and Popper.js, but we only need to install jQuery as Popper.js ships with Bootstrap itself:
npm install jquery --save
Configure aurelia.json
All Aurelia CLI applications have the concept of a project configuration file inside of aurelia_project/aurelia.json
which tells the bundler where our dependencies are and what they are. Without configuring our application, it won’t know what to load when we import it.
Secondly, we then need to add the Bootstrap dependency itself into the dependencies
section beneath the prepend
section we were just working within.
"jquery",
{
"name": "bootstrap",
"path": "../node_modules/bootstrap/dist",
"main": "js/bootstrap.bundle.min",
"deps": [
"jquery"
],
"exports": "$",
"resources": [
"css/bootstrap.css"
]
}
Bootstrap is now ready to be used within our application. Open up app.html
and add the following CSS import to include Bootstrap’s CSS. Your app.html
in a default CLI install should look like my example, we’ve just added in a Bootstrap CSS import using <require>
.
<template>
<require from="bootstrap/css/bootstrap.css"></require>
<h1>${message}</h1>
</template>
Lastly, open up app.js
and add the following import at the top:
import 'bootstrap';