|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +title: Webpack |
| 4 | +description: Learn how to install Bootstrap using webpack 2 |
| 5 | +group: getting-started |
| 6 | +--- |
| 7 | + |
| 8 | +Use [webpack v2.x](https://webpack.js.org/) to bundle Bootstrap into your project. |
| 9 | + |
| 10 | + |
| 11 | +## Contents |
| 12 | + |
| 13 | +* Will be replaced with the ToC, excluding the "Contents" header |
| 14 | +{:toc} |
| 15 | + |
| 16 | +## Installing Bootstrap |
| 17 | + |
| 18 | +[Install bootstrap](/getting-started/download/#npm) as a node module using npm. |
| 19 | + |
| 20 | +## Importing JavaScript |
| 21 | + |
| 22 | +Import [Bootstrap's JavaScript](/getting-started/javascript/) by adding this line to your app's entry point (usally `index.js` or `app.js`): |
| 23 | + |
| 24 | +{% highlight js %} |
| 25 | +import 'bootstrap'; |
| 26 | +{% endhighlight %} |
| 27 | + |
| 28 | +Alternatively, you may **import plugins individually** as needed: |
| 29 | + |
| 30 | +{% highlight js %} |
| 31 | +import 'bootstrap/js/dist/util'; |
| 32 | +import 'bootstrap/js/dist/dropdown'; |
| 33 | +... |
| 34 | +{% endhighlight %} |
| 35 | + |
| 36 | +Bootstrap is dependent on jQuery and Tether, so npm will install them for you if needed. But they must be explicitly provided by webpack. Add the following code to the `plugins` section in your webpack config file: |
| 37 | + |
| 38 | +{% highlight js %} |
| 39 | + plugins: [ |
| 40 | + ... |
| 41 | + new webpack.ProvidePlugin({ |
| 42 | + $: 'jquery', |
| 43 | + jQuery: 'jquery', |
| 44 | + 'window.jQuery': 'jquery', |
| 45 | + Tether: 'tether', |
| 46 | + // In case you imported plugins individually, you must also require them here: |
| 47 | + Util: "exports-loader?Util!bootstrap/js/dist/util", |
| 48 | + Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown", |
| 49 | + ... |
| 50 | + }) |
| 51 | + ... |
| 52 | + ] |
| 53 | +{% endhighlight %} |
| 54 | + |
| 55 | +{% callout warning %} |
| 56 | +Notice that if you chose to **import plugins individually**, you must also install [exports-loader](https://github.com/webpack-contrib/exports-loader) |
| 57 | +{% endcallout %} |
| 58 | + |
| 59 | +## Importing Styles |
| 60 | + |
| 61 | +### Importing Precompiled SASS |
| 62 | + |
| 63 | +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. |
| 64 | + |
| 65 | +First, create your own `_custom.scss` and use it to override the [built-in custom variables](/getting-started/options/). Then, use your main sass file to import your custom variables, followed by Bootstrap: |
| 66 | +{% highlight scss %} |
| 67 | +@import "custom"; |
| 68 | +@import "~bootstrap/scss/bootstrap"; |
| 69 | +{% endhighlight %} |
| 70 | + |
| 71 | +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) and [postcss-flexbugs-fixes](https://github.com/luisrudge/postcss-flexbugs-fixes). With minimal setup, your webpack config should include this rule or similar: |
| 72 | + |
| 73 | +{% highlight js %} |
| 74 | + ... |
| 75 | + { |
| 76 | + test: /\.(scss)$/, |
| 77 | + use: [{ |
| 78 | + loader: 'style-loader', // inject CSS to page |
| 79 | + }, { |
| 80 | + loader: 'css-loader', // translates CSS into CommonJS modules |
| 81 | + }, { |
| 82 | + loader: 'postcss-loader', // Run post css actions |
| 83 | + options: { |
| 84 | + plugins: function () { // post css plugins, can be exported to postcss.config.js |
| 85 | + return [ |
| 86 | + require('precss'), |
| 87 | + require('autoprefixer') |
| 88 | + ]; |
| 89 | + } |
| 90 | + } |
| 91 | + }, { |
| 92 | + loader: 'sass-loader' // compiles SASS to CSS |
| 93 | + }] |
| 94 | + }, |
| 95 | + ... |
| 96 | +{% endhighlight %} |
| 97 | + |
| 98 | +### Importing Compiled CSS |
| 99 | + |
| 100 | +Alternatively, you may use Bootstrap's ready-to-use css by simply adding this line to your project's entry point: |
| 101 | + |
| 102 | +{% highlight js %} |
| 103 | +import 'bootstrap/dist/css/bootstrap.min.css'; |
| 104 | +{% endhighlight %} |
| 105 | + |
| 106 | +In this case you may use your existing rule for `css` without any special modifications to webpack config. |
0 commit comments