webpack.md 2.94 KB
Newer Older
1
2
3
---
layout: docs
title: Webpack
4
description: Learn how to include Bootstrap in your project using Webpack.
5
group: getting-started
6
toc: true
7
8
9
10
---

## Installing Bootstrap

11
[Install bootstrap]({{ site.baseurl }}/docs/{{ site.docs_version }}/getting-started/download/#npm) as a Node.js module using npm.
12
13
14

## Importing JavaScript

15
Import [Bootstrap's JavaScript]({{ site.baseurl }}/docs/{{ site.docs_version }}/getting-started/javascript/) by adding this line to your app's entry point (usually `index.js` or `app.js`):
16
17
18
19
20
21
22
23

{% highlight js %}
import 'bootstrap';
{% endhighlight %}

Alternatively, you may **import plugins individually** as needed:

{% highlight js %}
24
import 'bootstrap/js/dist/alert';
25
26
27
...
{% endhighlight %}

XhmikosR's avatar
XhmikosR committed
28
29
Bootstrap dependends on [Popper](https://popper.js.org/), which is speicified in the `peerDependencies` property.
This means that you will have to make sure to add both of them to your `package.json` using `npm install popper.js`.
30
31
32

## Importing Styles

33
### Importing Precompiled Sass
34

35
To enjoy the full potential of Bootstrap and customize it to your needs, use the source files as a part of your project's bundling process.
36

XhmikosR's avatar
XhmikosR committed
37
First, create your own `_custom.scss` and use it to override the [built-in custom variables]({{ site.baseurl }}/docs/{{ site.docs_version }}/getting-started/theming/). Then, use your main Sass file to import your custom variables, followed by Bootstrap:
38

39
40
41
42
43
{% highlight scss %}
@import "custom";
@import "~bootstrap/scss/bootstrap";
{% endhighlight %}

44
For Bootstrap to compile, make sure you install and use the required loaders: [sass-loader](https://github.com/webpack-contrib/sass-loader), [postcss-loader](https://github.com/postcss/postcss-loader) with [Autoprefixer](https://github.com/postcss/autoprefixer#webpack). With minimal setup, your webpack config should include this rule or similar:
45

46
{% highlight js %}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
...
{
  test: /\.(scss)$/,
  use: [{
    loader: 'style-loader', // inject CSS to page
  }, {
    loader: 'css-loader', // translates CSS into CommonJS modules
  }, {
    loader: 'postcss-loader', // Run postcss actions
    options: {
      plugins: function () { // postcss plugins, can be exported to postcss.config.js
        return [
          require('autoprefixer')
        ];
61
      }
62
63
64
65
66
67
    }
  }, {
    loader: 'sass-loader' // compiles Sass to CSS
  }]
},
...
68
69
70
71
{% endhighlight %}

### Importing Compiled CSS

Robert Martin's avatar
Robert Martin committed
72
Alternatively, you may use Bootstrap's ready-to-use CSS by simply adding this line to your project's entry point:
73
74
75
76
77

{% highlight js %}
import 'bootstrap/dist/css/bootstrap.min.css';
{% endhighlight %}

Robert Martin's avatar
Robert Martin committed
78
In this case you may use your existing rule for `css` without any special modifications to webpack config, except you don't need `sass-loader` just [style-loader](https://github.com/webpack-contrib/style-loader) and [css-loader](https://github.com/webpack-contrib/css-loader).
79
80

{% highlight js %}
81
82
83
84
85
86
87
88
89
90
...
module: {
  rules: [
    {
      test: /\.css$/,
      use: ['style-loader', 'css-loader']
    }
  ]
}
...
91
{% endhighlight %}