Plugin vite scss

The npm package vite-plugin-sass-glob-import receives a total of 3,832 downloads a week. As such, we scored vite-plugin-sass-glob-import popularity level to be Small.

Based on project statistics from the GitHub repository for the npm package vite-plugin-sass-glob-import, we found that it has been starred 5 times, and that 0 other projects in the ecosystem are dependent on it.

Downloads are calculated as moving averages for a period of the last 12 months, excluding weekends and known missing data points.

We’re building a Vite project with Bootstrap from scratch, so there are some prerequisites and up front 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 setup npm. We’ll create the

    npm i --save bootstrap @popperjs/core
    
    1 folder and initialize npm with the
    npm i --save bootstrap @popperjs/core
    
    2 argument to avoid it asking us all the interactive questions.

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

  2. Install Vite. Unlike our Webpack guide, there’s only a single build tool dependency here. We use

    npm i --save bootstrap @popperjs/core
    
    3 to signal that this dependency is only for development use and not for production.

  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.

    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.

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

Project structure

We’ve already created the

npm i --save bootstrap @popperjs/core
1 folder and initialized npm. Now we’ll also create our
npm i --save bootstrap @popperjs/core
5 folder, stylesheet, and JavaScript file to round out the project structure. Run the following from
npm i --save bootstrap @popperjs/core
1, or manually create the folder and file structure shown below.

mkdir {src,src/js,src/scss}
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/
│   ├── js/
│   │   └── main.js
│   └── scss/
│   |   └── styles.scss
|   └── index.html
├── package-lock.json
├── package.json
└── vite.config.js

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

npm i --save bootstrap @popperjs/core
7 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

    npm i --save bootstrap @popperjs/core
    
    7 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
    npm i --save bootstrap @popperjs/core
    
    5 folder with hot reload).

    const path = require('path')
    
    export default {
      root: path.resolve(__dirname, 'src'),
      server: {
        port: 8080,
        hot: true
      }
    }
    

  2. Next we fill in

    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    0. 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.

    
    
      
        
        
        Bootstrap w/ Vite
      
      
        

    Hello, Bootstrap and Vite!

    Primary button

    We’re including a little bit of Bootstrap styling here with the

    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    1 and so that we see when Bootstrap’s CSS is loaded by Vite.

  3. Now we need an npm script to run Vite. Open

    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    2 and add the
    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    3 script shown below (you should already have the test script). We’ll use this script to start our local Vite dev server.

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

  4. And finally, we can start Vite. From the

    npm i --save bootstrap @popperjs/core
    
    1 folder in your terminal, run that newly added npm script:

    Plugin vite scss

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

Import Bootstrap

  1. Set up Bootstrap’s Sass import in

    npm i --save bootstrap @popperjs/core
    
    7. Your configuration file is now complete and should match the snippet below. The only new part here is the
    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    6 section—we use this to add an alias to our source files inside
    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    7 to keep imports as simple as possible.

    const path = require('path')
    
    export default {
      root: path.resolve(__dirname, 'src'),
      resolve: {
        alias: {
          '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
        }
      },
      server: {
        port: 8080,
        hot: true
      }
    }
    

  2. Now, let’s import Bootstrap’s CSS. Add the following to

    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    8 to import all of Bootstrap’s source Sass.

    // Import all of Bootstrap's CSS
    @import "~bootstrap/scss/bootstrap";
    

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

  3. Next we load the CSS and import Bootstrap’s JavaScript. Add the following to

    mkdir {src,src/js,src/scss}
    touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
    
    9 to load the CSS and import all of Bootstrap’s JS. Popper will be imported automatically through Bootstrap.

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

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

    npm i --save bootstrap @popperjs/core
    
    0

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

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

    Plugin vite scss

    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.

Does vite support sass?

Vite improves @import resolving for Sass and Less so that Vite aliases are also respected. In addition, relative url() references inside imported Sass/Less files that are in different directories from the root file are also automatically rebased to ensure correctness.

Should I use Vite in production?

Vite also offers a sensible production build configuration that handles library-based syntax like JSX, Vue, and TypeScript. By using Rollup under the hood, Vite ensures performance optimization techniques like tree-shaking, lazy-loading, and common chunk splitting are implemented for your production build.

Does Vite support code splitting?

Vite pre-bundles the app's dependencies — that does not change often, by using esbuild. esbuild is built with Golang making 10x-100x faster than JavaScript bundlers. And this redounds to the performance of Vite. Vite uses route-based code-splitting to determine what part of the code needs to be loaded.

How to use vite with TypeScript?

To use TypeScript in React (with Vite), install TypeScript and its dependencies into your application using the command line:.
npm install typescript @types/react @types/react-dom --save-dev..
touch tsconfig.json tsconfig.node.json..
mv src/main.jsx src/main.tsx. mv src/App.jsx src/App.tsx..