Bootstrap and Vite

The official guide for how to include and bundle Bootstrap’s CSS and JavaScript in your project using Vite.

Vite - 图1

Want to skip to the end? Download the source code and working demo for this guide from the twbs/examples repository. You can also open the example in StackBlitz for live editing.

Setup

We’re building a Vite project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we can really get started. This guide requires you to have Node.js installed and some familiarity with the terminal.

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

    1. mkdir my-project && cd my-project
    2. npm init -y
  2. 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.

    1. npm i --save-dev vite
  3. Install Bootstrap. Now we can install Bootstrap. We’ll also install Popper since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don’t plan on using those components, you can omit Popper here.

    1. npm i --save bootstrap @popperjs/core
  4. Install additional dependency. In addition to Vite and Bootstrap, we need another dependency (Sass) to properly import and bundle Bootstrap’s CSS.

    1. npm i --save-dev sass

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

Project structure

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

  1. mkdir {src,src/js,src/scss}
  2. 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:

  1. my-project/
  2. ├── src/
  3. ├── js/
  4. └── main.js
  5. └── scss/
  6. | └── styles.scss
  7. | └── index.html
  8. ├── package-lock.json
  9. ├── package.json
  10. └── vite.config.js

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.

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

    1. import { resolve } from 'path'
    2. export default {
    3. root: resolve(__dirname, 'src'),
    4. build: {
    5. outDir: '../dist'
    6. },
    7. server: {
    8. port: 8080
    9. }
    10. }
  2. 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.

    1. <!doctype html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="utf-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1">
    6. <title>Bootstrap w/ Vite</title>
    7. <script type="module" src="./js/main.js"></script>
    8. </head>
    9. <body>
    10. <div class="container py-4 px-3 mx-auto">
    11. <h1>Hello, Bootstrap and Vite!</h1>
    12. <button class="btn btn-primary">Primary button</button>
    13. </div>
    14. </body>
    15. </html>

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

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

    1. {
    2. // ...
    3. "scripts": {
    4. "start": "vite",
    5. "test": "echo \"Error: no test specified\" && exit 1"
    6. },
    7. // ...
    8. }
  4. And finally, we can start Vite. From the my-project folder in your terminal, run that newly added npm script:

    1. npm start

    Vite dev server running

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

Import Bootstrap

  1. Import Bootstrap’s CSS. Add the following to src/scss/styles.scss to import all of Bootstrap’s source Sass.

    1. // Import all of Bootstrap's CSS
    2. @import "bootstrap/scss/bootstrap";

    You can also import our stylesheets individually if you want. Read our Sass import docs for details.

  2. Next we load the CSS and import Bootstrap’s JavaScript. Add the following to src/js/main.js to load the CSS and import all of Bootstrap’s JS. Popper will be imported automatically through Bootstrap.

    1. // Import our custom CSS
    2. import '../scss/styles.scss'
    3. // Import all of Bootstrap's JS
    4. import * as bootstrap from 'bootstrap'

    You can also import JavaScript plugins individually as needed to keep bundle sizes down:

    1. import Alert from 'bootstrap/js/dist/alert';
    2. // or, specify which plugins you need:
    3. import { Tooltip, Toast, Popover } from 'bootstrap';

    Read our JavaScript docs for more information on how to use Bootstrap’s plugins.

  3. And you’re done! 🎉 With Bootstrap’s source Sass and JS fully loaded, your local development server should now look like this:

    Vite dev server running with Bootstrap

    Now you can start adding any Bootstrap components you want to use. Be sure to check out the complete Vite example project for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap’s CSS and JS that you need.


See something wrong or out of date here? Please open an issue on GitHub. Need help troubleshooting? Search or start a discussion on GitHub.