bootstrap-vite
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBootstrap 6 with Vite
Bootstrap 6 搭配 Vite 搭建教程
Set up a Bootstrap 6 project bundled with Vite. Requires Node.js and terminal familiarity.
使用 Vite 打包搭建 Bootstrap 6 项目。需要具备 Node.js 基础和终端使用经验。
Workflow
工作流程
- Step 1: Set up npm and install dependencies
- Step 2: Create the project structure
- Step 3: Configure Vite
- Step 4: Import Bootstrap's CSS and JS
- Step 5: Verify
- 步骤1:搭建npm环境并安装依赖
- 步骤2:创建项目结构
- 步骤3:配置Vite
- 步骤4:引入Bootstrap的CSS和JS
- 步骤5:验证
Step 1: Set up npm and install dependencies
步骤1:搭建npm环境并安装依赖
-
Create the project and initialize npm:sh
mkdir my-project && cd my-project npm init -y -
Install Vite:sh
npm i --save-dev vite -
Install Bootstrap and its optional peer dependencies (omitif not using menus/popovers/tooltips; omit
@floating-ui/domif not using the datepicker):vanilla-calendar-proshnpm i --save bootstrap@6.0.0-alpha.1 @floating-ui/dom vanilla-calendar-pro -
Install Sass to compile Bootstrap's source:sh
npm i --save-dev sass
-
创建项目并初始化npm:sh
mkdir my-project && cd my-project npm init -y -
安装Vite:sh
npm i --save-dev vite -
安装Bootstrap及其可选对等依赖(如果不使用菜单/弹出框/工具提示,可省略;如果不使用日期选择器,可省略
@floating-ui/dom):vanilla-calendar-proshnpm i --save bootstrap@6.0.0-alpha.1 @floating-ui/dom vanilla-calendar-pro -
安装Sass以编译Bootstrap源文件:sh
npm i --save-dev sass
Step 2: Create the project structure
步骤2:创建项目结构
sh
mkdir {src,src/js,src/scss}
touch src/index.html src/js/main.js src/scss/styles.scss vite.config.jsTarget layout:
text
my-project/
├── src/
│ ├── js/
│ │ └── main.js
│ ├── scss/
│ │ └── styles.scss
│ └── index.html
├── package-lock.json
├── package.json
└── vite.config.jssh
mkdir {src,src/js,src/scss}
touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js目标目录结构:
text
my-project/
├── src/
│ ├── js/
│ │ └── main.js
│ ├── scss/
│ │ └── styles.scss
│ └── index.html
├── package-lock.json
├── package.json
└── vite.config.jsStep 3: Configure Vite
步骤3:配置Vite
-
:
vite.config.jsjsimport path from 'path' export default { root: path.resolve(__dirname, 'src'), build: { outDir: '../dist' }, server: { port: 8080 } } -
— load the entry module:
src/index.htmlhtml<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap w/ Vite</title> <script type="module" src="./js/main.js"></script> </head> <body> <div class="container py-4 px-3 mx-auto"> <h1>Hello, Bootstrap and Vite!</h1> <button class="btn-solid theme-primary">Primary button</button> </div> </body> </html> -
— add the start script:
package.jsonjson{ "scripts": { "start": "vite", "test": "echo \"Error: no test specified\" && exit 1" } } -
Start Vite:sh
npm start
-
:
vite.config.jsjsimport path from 'path' export default { root: path.resolve(__dirname, 'src'), build: { outDir: '../dist' }, server: { port: 8080 } } -
— 加载入口模块:
src/index.htmlhtml<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap w/ Vite</title> <script type="module" src="./js/main.js"></script> </head> <body> <div class="container py-4 px-3 mx-auto"> <h1>Hello, Bootstrap and Vite!</h1> <button class="btn-solid theme-primary">Primary button</button> </div> </body> </html> -
— 添加启动脚本:
package.jsonjson{ "scripts": { "start": "vite", "test": "echo \"Error: no test specified\" && exit 1" } } -
启动Vite:sh
npm start
Step 4: Import Bootstrap's CSS and JS
步骤4:引入Bootstrap的CSS和JS
-
— import the source Sass with
src/scss/styles.scss(never@use):@importscss// Import all of Bootstrap's CSS @use "bootstrap/scss/bootstrap";To customize, override Bootstrap's CSS token maps (,$root-tokens) — see Customize › Sass. Token names are written unprefixed in Sass; Bootstrap's dist/CDN CSS adds the$*-tokensprefix via PostCSS, so compiling the source yourself keeps them unprefixed.--bs- -
— load the CSS and import Bootstrap's JS. Floating UI and Vanilla Calendar Pro are imported automatically through Bootstrap:
src/js/main.jsjs// Import our custom CSS, then all of Bootstrap’s JS import '../scss/styles.scss' import * as bootstrap from 'bootstrap'Import only the plugins you need to keep bundle sizes down:jsimport { Tooltip, Toast, Popover } from 'bootstrap'
-
— 使用
src/scss/styles.scss引入Sass源文件(切勿使用@use):@importscss// Import all of Bootstrap's CSS @use "bootstrap/scss/bootstrap";如需自定义,可覆盖Bootstrap的CSS令牌映射(、$root-tokens)——参见自定义 › Sass。Sass中的令牌名称无前缀;Bootstrap的dist/CDN版本CSS会通过PostCSS添加$*-tokens前缀,因此自行编译源文件时令牌不会带此前缀。--bs- -
— 加载CSS并引入Bootstrap的JS。Floating UI和Vanilla Calendar Pro会通过Bootstrap自动引入:
src/js/main.jsjs// Import our custom CSS, then all of Bootstrap’s JS import '../scss/styles.scss' import * as bootstrap from 'bootstrap'仅引入所需插件以减小打包体积:jsimport { Tooltip, Toast, Popover } from 'bootstrap'
Step 5: Verify
步骤5:验证
- Run and open
npm start— thehttp://localhost:8080and.containerbutton should be styled..btn-solid.theme-primary - Confirm the browser console is free of module-resolution errors (Bootstrap, Floating UI, Vanilla Calendar Pro).
- Trigger a JS component (tooltip/dialog) to confirm the JS bundle works.
- Bootstrap 6 requires a browser that supports and
oklch().color-mix()
For an optimized build that imports only the parts of Bootstrap you use, see the twbs/examples Vite project.
- 执行 并打开
npm start—http://localhost:8080容器和.container按钮应已应用样式。.btn-solid.theme-primary - 确认浏览器控制台无模块解析错误(涉及Bootstrap、Floating UI、Vanilla Calendar Pro)。
- 触发一个JS组件(工具提示/对话框)以确认JS打包文件正常运行。
- Bootstrap 6要求浏览器支持 和
oklch()。color-mix()
如需实现仅引入所用Bootstrap模块的优化构建,请参考twbs/examples Vite项目。