]> git.ipfire.org Git - thirdparty/bootstrap.git/blame - site/content/docs/5.3/getting-started/vite.md
Update Vite, Parcel and Webpack guides (#38305)
[thirdparty/bootstrap.git] / site / content / docs / 5.3 / getting-started / vite.md
CommitLineData
5b0bf3f4
JD
1---
2layout: docs
39c51aa3 3title: Bootstrap and Vite
5b0bf3f4
JD
4description: The official guide for how to include and bundle Bootstrap's CSS and JavaScript in your project using Vite.
5group: getting-started
6toc: true
501faa2c 7thumbnail: guides/bootstrap-vite@2x.png
5b0bf3f4
JD
8---
9
10<img class="mb-4 img-fluid rounded-3" srcset="/docs/{{< param docs_version >}}/assets/img/guides/bootstrap-vite.png, /docs/{{< param docs_version >}}/assets/img/guides/bootstrap-vite@2x.png 2x" src="/docs/{{< param docs_version >}}/assets/img/guides/bootstrap-vite.png" width="2000" height="1000" alt="">
11
12{{< callout >}}
13**Want to skip to the end?** Download the source code and working demo for this guide from the [twbs/examples repository](https://github.com/twbs/examples/tree/main/vite). You can also [open the example in StackBlitz](https://stackblitz.com/github/twbs/examples/tree/main/vite?file=index.html) for live editing.
14{{< /callout >}}
15
16## Setup
17
18We'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.
19
201. **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.
21
22 ```sh
23 mkdir my-project && cd my-project
24 npm init -y
25 ```
26
272. **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.
28
29 ```sh
30 npm i --save-dev vite
31 ```
32
333. **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.
34
35 ```sh
36 npm i --save bootstrap @popperjs/core
37 ```
7166e953 38
5b0bf3f4
JD
394. **Install additional dependency.** In addition to Vite and Bootstrap, we need another dependency (Sass) to properly import and bundle Bootstrap's CSS.
40
41 ```sh
42 npm i --save-dev sass
43 ```
44
45Now that we have all the necessary dependencies installed and setup, we can get to work creating the project files and importing Bootstrap.
46
47## Project structure
48
49We'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.
50
51```sh
52mkdir {src,src/js,src/scss}
53touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
54```
55
56When you're done, your complete project should look like this:
57
58```text
59my-project/
60├── src/
61│ ├── js/
62│ │ └── main.js
63│ └── scss/
64│ | └── styles.scss
65| └── index.html
66├── package-lock.json
67├── package.json
68└── vite.config.js
69```
70
71At this point, everything is in the right place, but Vite won't work because we haven't filled in our `vite.config.js` yet.
72
73## Configure Vite
74
75With dependencies installed and our project folder ready for us to start coding, we can now configure Vite and run our project locally.
76
9af1232c 771. **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).
5b0bf3f4
JD
78
79 <!-- eslint-skip -->
80 ```js
81 const path = require('path')
82
83 export default {
84 root: path.resolve(__dirname, 'src'),
99867eb9
JD
85 build: {
86 outDir: '../dist'
87 },
5b0bf3f4
JD
88 server: {
89 port: 8080,
90 hot: true
91 }
92 }
93 ```
94
952. **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.
96
97 ```html
98 <!doctype html>
99 <html lang="en">
100 <head>
101 <meta charset="utf-8">
102 <meta name="viewport" content="width=device-width, initial-scale=1">
103 <title>Bootstrap w/ Vite</title>
99867eb9 104 <script type="module" src="./js/main.js"></script>
5b0bf3f4
JD
105 </head>
106 <body>
107 <div class="container py-4 px-3 mx-auto">
108 <h1>Hello, Bootstrap and Vite!</h1>
109 <button class="btn btn-primary">Primary button</button>
110 </div>
5b0bf3f4
JD
111 </body>
112 </html>
113 ```
114
115 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.
116
1173. **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.
118
119 ```json
120 {
121 // ...
122 "scripts": {
123 "start": "vite",
124 "test": "echo \"Error: no test specified\" && exit 1"
125 },
126 // ...
127 }
128 ```
129
1304. **And finally, we can start Vite.** From the `my-project` folder in your terminal, run that newly added npm script:
131
132 ```sh
133 npm start
134 ```
135
136 <img class="img-fluid" src="/docs/{{< param docs_version >}}/assets/img/guides/vite-dev-server.png" alt="Vite dev server running">
137
138In the next and final section to this guide, we’ll import all of Bootstrap’s CSS and JavaScript.
139
140## Import Bootstrap
141
99867eb9 1421. **Import Bootstrap's CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap's source Sass.
5b0bf3f4
JD
143
144 ```scss
145 // Import all of Bootstrap's CSS
99867eb9 146 @import "bootstrap/scss/bootstrap";
5b0bf3f4
JD
147 ```
148
149 *You can also import our stylesheets individually if you want. [Read our Sass import docs]({{< docsref "/customize/sass#importing" >}}) for details.*
150
99867eb9 1512. **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.
5b0bf3f4
JD
152
153 <!-- eslint-skip -->
154 ```js
155 // Import our custom CSS
156 import '../scss/styles.scss'
157
158 // Import all of Bootstrap's JS
159 import * as bootstrap from 'bootstrap'
160 ```
161
162 You can also import JavaScript plugins individually as needed to keep bundle sizes down:
163
164 <!-- eslint-skip -->
165 ```js
166 import Alert from 'bootstrap/js/dist/alert';
167
168 // or, specify which plugins you need:
169 import { Tooltip, Toast, Popover } from 'bootstrap';
170 ```
171
172 *[Read our JavaScript docs]({{< docsref "/getting-started/javascript/" >}}) for more information on how to use Bootstrap's plugins.*
173
99867eb9 1743. **And you're done! 🎉** With Bootstrap's source Sass and JS fully loaded, your local development server should now look like this.
5b0bf3f4
JD
175
176 <img class="img-fluid" src="/docs/{{< param docs_version >}}/assets/img/guides/vite-dev-server-bootstrap.png" alt="Vite dev server running with Bootstrap">
177
eae51cdf 178 Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete Vite example project](https://github.com/twbs/examples/tree/main/vite) 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.
5b0bf3f4
JD
179
180{{< markdown >}}
181{{< partial "guide-footer.md" >}}
182{{< /markdown >}}