网页
综合
更新时间 2026-11-14 02:24:30
Bootstrap and Parcel · Bootstrap v6.0
自动蜘蛛
getbootstrap.com
正文约 4707 字
The official guide for how to include and bundle Bootstrap’s CSS and JavaScript in your project using Parcel.
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 but not run it because Parcel isn’t currently supported there.
Using an AI coding agent? Point it at our bootstrap-parcel skill to automate this setup. See AI for all available skills.
Parcel is a web application bundler designed to simplify the development process with a zero-configuration setup out of the box. It offers features found in more advanced bundlers while focusing on ease of use, making it ideal for developers seeking a quick start.
We’re building a Parcel 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.
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.
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.
Install Bootstrap. Now we can install Bootstrap. We’ll also install Floating UI and Vanilla Calendar Pro since some of our components depend on them. If you don’t plan on using menus, popovers, or tooltips, you can omit Floating UI. If you don’t plan on using the datepicker, you can omit Vanilla Calendar Pro.
Now that we have all the necessary dependencies installed, we can get to work creating the project files and importing Bootstrap.
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.
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.
With dependencies installed and our project folder ready for us to start coding, we can now configure Parcel and run our project locally. Parcel itself requires no configuration file by design, but we do need an npm script and an HTML file to start our server.
Fill in the src/index.html file. Parcel needs a page to render, so we use our index.html page to set up some basic HTML, including our CSS and JavaScript files.
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 Parcel.
Parcel will automatically detect we’re using Sass and install the Sass Parcel plugin to support it. However, if you wish, you can also manually run npm i --save-dev @parcel/transformer-sass.
Add the Parcel npm scripts. Open the package.json and add the following start script to the scripts object. We’ll use this script to start our Parcel development server and render the HTML file we created after it’s compiled into the dist directory.
And finally, we can start Parcel. From the my-project folder in your terminal, run that newly added npm script:
In the next and final section to this guide, we’ll import all of Bootstrap’s CSS and JavaScript.
Importing Bootstrap into Parcel requires two imports, one into our styles.scss and one into our main.js.
Import Bootstrap’s CSS. Add the following to src/scss/styles.scss to import all of Bootstrap’s source Sass.
You can also import our stylesheets individually if you want. Read our Sass import docs for details.
Import Bootstrap’s JS. Add the following to src/js/main.js to import all of Bootstrap’s JS. Floating UI and Vanilla Calendar Pro will be imported automatically through Bootstrap.
You can also import JavaScript plugins individually as needed to keep bundle sizes down:
Read our JavaScript docs for more information on how to use Bootstrap’s plugins.
And you’re done! 🎉 With Bootstrap’s source Sass and JS fully loaded, your local development server should now look like this:
Now you can start adding any Bootstrap components you want to use. Be sure to check out the complete Parcel 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.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。