DEV Community

Cover image for Vite Integration
Keep Coding
Keep Coding

Posted on

Vite Integration

Introduction

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

If you need more extensive support and step-by-step guidance, check out our dedicated tutorial.


Vite 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.

FREE - mdb frontend init mdb5-free-standard-vite
ESSENTIAL - mdb frontend init mdb5-essential-standard-vite
ADVANCED - mdb frontend init mdb5-advanced-standard-vite

Or download the repository - MDB VITE STARTER

Note: Vite 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

TERMINAL
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.

TERMINAL
mkdir my-project && cd my-project
npm init -y

Install Vite

Unlike our Webpack guide, there’s only a single build tool dependency here. We use --save-dev to signal that this dependency is only for development use and not for production.

TERMINAL
npm i --save-dev vite

Install additional dependency

In addition to Vite, we need another dependency (Sass) to properly to properly import and bundle MDB's CSS.

TERMINAL
npm i --save-dev sass

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 folder to round out the project structure. Run the following from my-project, or manually create the folder and file structure shown below.

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

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

my-project/
├── src/
│   ├── assets/
│   │   └── mdb/
│   ├── js/
│   │   └── main.js
│   ├── scss/
│   │   └── styles.scss
│   └── index.html
├── package-lock.json
├── package.json
└── vite.config.js
Enter fullscreen mode Exit fullscreen mode

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


Configure Vite

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

Open vite.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 Vite were to look for our project’s JavaScript and how the development server should behave (pulling from the src folder with hot reload).

JAVASCRIPT

const path = require('path')

export default {
  root: path.resolve(__dirname, 'src'),
  server: {
    port: 8080,
    hot: true
  }
}
Enter fullscreen mode Exit fullscreen mode

Next we fill in src/index.html

This is the HTML page Vite will load in the browser to utilize the bundled CSS and JS we’ll add to it in later steps.

HTML

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>MDB w/ Vite</title>
  </head>
  <body>
    <div class="container py-4 px-3 mx-auto">
      <h1>Hello, MDB and Vite!</h1>
      <button class="btn btn-primary">Primary button</button>
    </div>
    <script type="module" src="./js/main.js"></script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

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 Vite.

Now we need an npm script to run Vite

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 Vite dev server.

JSON

{
  // ...
  "scripts": {
    "start": "vite",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  // ...
}
Enter fullscreen mode Exit fullscreen mode

And finally, we can start Vite

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 - Other MDB import methods can be found here

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

PRO ESSENTIAL - npm i git+https://oauth2:ACCESS_TOKEN@git.mdbootstrap.com/mdb/standard/mdb-ui-kit-pro-essential

PRO ADVANCED - npm i git+https://oauth2:ACCESS_TOKEN@git.mdbootstrap.com/mdb/standard/mdb-ui-kit-pro-advanced

FREE - npm i mdb-ui-kit

*Importing JS modules
*

import '../scss/styles.scss';
import * as mdb from 'mdb-ui-kit';
window.mdb = mdb;
Enter fullscreen mode Exit fullscreen mode

Importing SCSS file
PRO ESSENTIAL - @import 'mdb-ui-kit/mdb-ui-kit/src/scss/mdb.pro.scss';
PRO ADVANCED - @import 'mdb-ui-kit/mdb-ui-kit/src/mdb/scss/mdb.pro.scss';
FREE - @import 'mdb-ui-kit/mdb-ui-kit/src/scss/mdb.free.scss';

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": "vite build",
  ...
},
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)