MDB 5 Webpack Integration

Introduction

This guide will walk you through steps necessary to include and bundle MDB package in your project using Webpack.


Webpack starter

If you want to use the starter prepared by us, use MDB CLI to create a new project.

Note: If you don't have MDB CLI installed yet, you can do it with NPM: npm install -g mdb-cli. Now log in with your MDB account, type: mdb login. If you don't have account yet you can create one using mdb register command.

        
            
          mdb frontend init mdb5-free-standard-webpack
        
        
    
        
            
          mdb frontend init mdb5-essential-standard-webpack
        
        
    
        
            
          mdb frontend init mdb5-advanced-standard-webpack
        
        
    

Or download the repository.

MDB How to use Bootstrap 5 with Webpack - free starter Starter 1-CLICK INIT VIA MDB GO

Note: How to use Bootstrap 5 with Webpack - free starter starter on Github repo uses MDB Standard free installed via NPM. If you want to use a Pro version or a different installation method go to the import MDB section and check if it requires configuration changes. For example, changing the import paths of MDB files.

Basic features
Dev Server
        
            
      npm install
      
        
    

If you decided to use a prepared starter, you can skip this tutorial. The starter is fully configured and ready to use.


Setup

Before you start make sure you have Node.js installed.

Create a project folder and setup npm

We’ll create the my-project folder and initialize npm with the -y argument to avoid it asking us all the interactive questions.

        
            
      mkdir my-project && cd my-project
      npm init -y
      
        
    
Install Webpack

Next we need to install our Webpack development dependencies: webpack for the core of Webpack, webpack-cli so we can run Webpack commands from the terminal, and webpack-dev-server so we can run a local development server. We use --save-dev to signal that these dependencies are only for development use and not for production.

        
            
      npm i --save-dev webpack webpack-cli webpack-dev-server
      
        
    
Install additional dependencies

In addition to Webpack and Bootstrap, we need a few more dependencies to properly import and bundle MDB's CSS and JS with Webpack. These include Sass, some loaders, and Autoprefixer.

        
            
      npm i --save-dev autoprefixer css-loader postcss-loader sass sass-loader style-loader
      
        
    

Now that we have all the necessary dependencies installed, we can get to work creating the project files and importing MDB.


Project Structure

We’ve already created the my-project folder and initialized npm. Now we’ll also create our src and dist folders to round out the project structure. Run the following from my-project, or manually create the folder and file structure shown below.

        
            
        mkdir {dist,src,src/js,src/scss,src/assets,src/assets/mdb}
        touch dist/index.html src/js/main.js src/scss/styles.scss webpack.config.js
      
        
    

When you’re done, your complete project should look like this:

        
          my-project/
          ├── dist/
          │   └── index.html
          ├── src/
          │   ├── assets/
          │   │   └── mdb/
          │   ├── js/
          │   │   └── main.js
          │   └── scss/
          │       └── styles.scss
          ├── package-lock.json
          ├── package.json
          └── webpack.config.js
        
      

At this point, everything is in the right place, but Webpack won’t work because we haven’t filled in our webpack.config.js yet.


Configure Webpack

With dependencies installed and our project folder ready for us to start coding, we can now configure Webpack and run our project locally.

Open webpack.config.js in your editor

Since it’s blank, we’ll need to add some boilerplate config to it so we can start our server. This part of the config tells Webpack were to look for our project’s JavaScript, where to output the compiled code to (dist), and how the development server should behave (pulling from the dist folder with hot reload).

        
            
      const path = require('path')

      module.exports = {
        entry: './src/js/main.js',
        output: {
          filename: 'main.js',
          path: path.resolve(__dirname, 'dist')
        },
        devServer: {
          static: path.resolve(__dirname, 'dist'),
          port: 8080,
          hot: true
        }
      }
      
        
    
Next we fill in our dist/index.html

This is the HTML page Webpack will load in the browser to utilize the bundled CSS and JS we’ll add to it in later steps. Before we can do that, we have to give it something to render and include the output JS from the previous step.

        
            
      <!doctype html>
      <html lang="en">
        <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <title>MDB w/ Webpack</title>
          <!-- Font Awesome -->
          <link
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
          rel="stylesheet"
          />
          <!-- Google Fonts -->
          <link
          href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
          rel="stylesheet"
          />
        </head>
        <body>
          <div class="container py-4 px-3 mx-auto">
            <h1>Hello, MDB and Webpack!</h1>
            <button class="btn btn-primary" data-mdb-ripple-init>Primary button</button>
          </div>
          <script src="./main.js"></script>
        </body>
      </html>
      
        
    

We’re including a little bit of styling here with the div class="container" and <button> so that we see when CSS from MDB package is loaded by Webpack.

Now we need an npm script to run Webpack

Open package.json and add the start script shown below (you should already have the test script). We’ll use this script to start our local Webpack dev server.

        
            
      {
        // ...
        "scripts": {
          "start": "webpack serve --mode development",
          "test": "echo \"Error: no test specified\" && exit 1"
        },
        // ...
      }
      
        
    

Importing MDB into Webpack requires the loaders we installed in the first section. We’ve installed them with npm, but now Webpack needs to be configured to use them.

Set up the loaders in webpack.config.js

Your configuration file is now complete and should match the snippet below. The only new part here is the module section.

        
            
      const path = require('path')

      module.exports = {
        entry: './src/js/main.js',
        output: {
          filename: 'main.js',
          path: path.resolve(__dirname, 'dist')
        },
        devServer: {
          static: path.resolve(__dirname, 'dist'),
          port: 8080,
          hot: true
        },
        module: {
          rules: [
            {
              test: /\.(scss)$/,
              use: [
                {
                  loader: 'style-loader'
                },
                {
                  loader: 'css-loader'
                },
                {
                  loader: 'postcss-loader',
                  options: {
                    postcssOptions: {
                      plugins: () => [
                        require('autoprefixer')
                      ]
                    }
                  }
                },
                {
                  loader: 'sass-loader'
                }
              ]
            }
          ]
        }
      }
      
        
    

Here’s a recap of why we need all these loaders. style-loader injects the CSS into a <style> element in the <head> of the HTML page, css-loader helps with using @import and url(), postcss-loader is required for Autoprefixer, and sass-loader allows us to use Sass.

And finally, we can start Webpack

From the my-project folder in your terminal, run that newly added npm script:

        
            
        npm start
      
        
    

In the next section to this guide, we’ll import all of MDB's CSS and JavaScript.


Import MDB

Here you need to decide which method of import you want to choose

CDN import is available only for free version of MDB.

To install the MDB UI KIT in your project easily type the following command in the terminal. If you have PRO package remember to swap the access token before starting the installation.

Token generation

If you don't have access token yet please follow the tutorial.

Install MDB via NPM
        
            
              npm i git+https://oauth2:ACCESS_TOKEN@git.mdbootstrap.com/mdb/standard/mdb-ui-kit-pro-essential
            
        
    
        
            
            npm i git+https://oauth2:ACCESS_TOKEN@git.mdbootstrap.com/mdb/standard/mdb-ui-kit-pro-advanced
          
        
    
        
            
              npm i mdb-ui-kit
            
        
    
Importing JavaScript modules
        
            
            import '../scss/styles.scss';

            import { Ripple, initMDB } from 'mdb-ui-kit/js/mdb.es.min.js'; // Import needed modules

            window.Ripple = Ripple;
            
            initMDB({ Ripple }) // Initialize imported modules to enable data-attribute init
          
        
    
        
            
            import '../scss/styles.scss';
            import * as mdb from 'mdb-ui-kit/js/mdb.umd.min.js';
            window.mdb = mdb;
          
        
    
Importing CSS file
        
            
              @import 'mdb-ui-kit/css/mdb.min.css';
            
        
    
        
            
              @import 'mdb-ui-kit/css/mdb.min.css';
            
        
    
        
            
              @import 'mdb-ui-kit/css/mdb.min.css';
            
        
    
Add MDB via MDB CLI

Create a new project with our MDB starter. Run the command below and select the MDB5 Standard.

        
            
              cd my-project/src/assets
              mdb frontend init
            
        
    

Note: The project will be created in a folder with a different name than mdb. Rename the folder to mdb, or note that you must include a different folder name in the paths of the imported MDB files.

Add MDB via ZIP

Download the zip package, extract the downloaded package and copy included files into directory my-project/src/assets/mdb.

Compiled files

Add the following to src/scss/styles.scss to import all of MDB’s source Sass.

        
            
              @import '../assets/mdb/css/mdb.min.css'; // MDB package CSS library
            
        
    

Add the following to src/js/main.js to load the CSS and import all of MDB's JS.

        
            
              import '../scss/styles.scss';

              import { Ripple, initMDB } from '../assets/mdb/js/mdb.es.min.js'; // Import needed modules

              window.Ripple = Ripple;

              initMDB({ Ripple }) // Initialize imported modules to enable data-attribute init
            
        
    
        
            
            // Import our custom SCSS
            import '../scss/styles.scss';

            // Import all of MDB's JS
            import * as mdb from '../assets/mdb/js/mdb.umd.min.js';
            window.mdb = mdb;
            
        
    
Source files

If you prefer to import source files from MDB package or use custom import there are some additional dependency requirements depending on used package free/pro.

        
            
            npm i --save-dev @popperjs/core detect-autofill chart.js chartjs-plugin-datalabels deepmerge perfect-scrollbar
            
        
    
        
            
            npm i --save-dev @popperjs/core detect-autofill
            
        
    

In this case src/js/main.js file will look like this:

        
            
                  import '../scss/styles.scss';

                  import { Ripple, initMDB } from '../assets/mdb/src/js/mdb.pro.es.js'; // Import needed modules

                  window.Ripple = Ripple;

                  initMDB({ Ripple }) // Initialize imported modules to enable data-attribute init
                
        
    
        
            
                  // Import our custom SCSS
                  import '../scss/styles.scss';

                  // Import all of MDB's JS
                  import * as mdb from '../assets/mdb/src/js/mdb.pro.umd.js'; // lib
                  window.mdb = mdb;
                
        
    
        
            
                // Import our custom SCSS
                import '../scss/styles.scss';

                // Import all of MDB's JS
                import { Ripple, initMDB } from '../assets/mdb/src/js/mdb.free.es.js'; // Import needed modules

                window.Ripple = Ripple;

                initMDB({ Ripple }) // Initialize imported modules to enable data-attribute init
              
        
    
        
            
                // Import our custom SCSS
                import '../scss/styles.scss';

                // Import all of MDB's JS
                import * as mdb from '../assets/mdb/src/js/mdb.free.umd.js'; // lib
                window.mdb = mdb;
              
        
    

And content of src/scss/styles.scss should be:

        
            
          @import '../assets/mdb/src/scss/mdb.pro';
          
        
    
        
            
          @import '../assets/mdb/src/mdb/scss/mdb.pro';
          
        
    
        
            
          @import '../assets/mdb/src/scss/mdb.free';
          
        
    

Installation via CDN is one of the easiest methods of integrating MDB UI KIT with your project. Just copy the latest compiled JS script tag and CSS link tag from cdnjs to the application.

Don't forget to add also Font Awesome and Roboto font if you need. Here's an example code:

Into the <head> tag in your HTML file copy:

        
            
              <!-- Font Awesome -->
              <link
                href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
                rel="stylesheet"
              />
              <!-- Google Fonts -->
              <link
                href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
                rel="stylesheet"
              />
              <!-- MDB -->
              <link
                href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/7.0.0/mdb.min.css"
                rel="stylesheet"
              />
            
        
    

At the end of <body> tag in your HTML file copy:

        
            
            <!-- MDB -->
            <script
            type="text/javascript"
            src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/7.0.0/mdb.umd.min.js"
            ></script>
            
        
    
Now you're done

With MDB's source Sass and JS fully loaded, your local development server should work and you should see MDB styles applied to button in the example.


Create build

The starter that we have prepared for you is fully configured and we have added the build command to it. Just run the npm run build command. You will find the bundled files in the dist folder.

If you have decided to configure the project yourself, you must add the build command in the package.json file.

        
            
          "scripts": {
            ...
            "build": "webpack build --mode production",
            ...
          },
        
        
    

After that you can use npm run build command. You will find the bundled files in the folder you set as the output directory. By default, this is the dist folder.