Introduction
This guide will walk you through steps necessary to include and bundle MDB package in your project using Parcel.
Parcel 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.
Or download the repository.
MDB How to use Bootstrap 5 with Parcel - free starter Starter 1-CLICK INIT VIA MDB GONote: How to use Bootstrap 5 with Parcel - 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
- Bundling via Parcel v2.7.0
- SASS Support via @parcel/transformer-sass v2.7.0
- Linting via @parcel/validator-eslint v2.7.0
- Unit Testing via Jest v29.0.1
- Code Formatting via Prettier v2.7.1
- Unused CSS removed via PurgeCSS v5.0.0
- Deploy via MDB CLI latest version
Unpack starter zip and install dependencies via npm.
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.
Install Parcel
Unlike our Webpack guide, there’s only a single build tool dependency here. Parcel will automatically install
language transformers (like Sass) as it detects them.
We use --save-dev
to signal that this dependency is only for development use and not for production.
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.
When you’re done, your complete project should look like this:
At this point, everything is in the right place, but Parcel needs an HTML page and npm script to start our server.
Configure Parcel
With dependencies installed and our project folder ready for us to start coding, we can now configure Parcel and run our project locally.
Next we fill in src/index.html
.
This is the HTML page Parcel will load in the browser to utilize the bundled CSS and JS we’ll add to it in later steps.
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 Parcel.
Now we need an npm script to run Parcel.
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 Parcel dev server.
And finally, we can start Parcel
From the my-project
folder in your terminal,
run that newly added npm script:
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
Importing JavaScript modules
Importing SCSS file
Add MDB via MDB CLI
Create a new project with our MDB starter. Run the command below and select the MDB5 Standard.
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.
Add the following to src/js/index.js
to import all of MDB's JS.
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.
In this case src/js/main.js
file will look like this:
And content of src/scss/styles.scss
should be:
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:
At the end of <body>
tag in your HTML file copy:
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.
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.