bootstrap-npm
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBootstrap 6 with npm
基于 npm 的 Bootstrap 6 项目搭建
Set up a Bootstrap 6 project that compiles Sass to CSS using npm command-line tools, with no JavaScript bundler. Bootstrap's pre-built JS bundle is loaded directly via a tag. Requires Node.js and terminal familiarity.
<script>本教程将使用 npm 命令行工具搭建一个将 Sass 编译为 CSS 的 Bootstrap 6 项目,无需 JavaScript 打包工具。Bootstrap 预构建的 JS 包通过 标签直接加载。需要具备 Node.js 和终端使用基础。
<script>Workflow
工作流程
- Step 1: Set up npm and install dependencies
- Step 2: Create the project structure
- Step 3: Configure npm scripts and config files
- Step 4: Import Bootstrap's Sass
- Step 5: Verify
- 步骤 1:配置 npm 并安装依赖
- 步骤 2:创建项目结构
- 步骤 3:配置 npm 脚本与配置文件
- 步骤 4:引入 Bootstrap 的 Sass
- 步骤 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 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 the tools to compile and post-process CSS:sh
npm i --save-dev autoprefixer postcss postcss-cli sass -
Install local dev tooling (watch, run scripts, serve, lint):sh
npm i --save-dev nodemon npm-run-all sirv-cli stylelint stylelint-config-twbs-bootstrap
-
创建项目并初始化 npm:sh
mkdir my-project && cd my-project npm init -y -
安装 Bootstrap 及其可选的 peer 依赖(如果不使用菜单/弹出框/工具提示,可省略;如果不使用日期选择器,可省略
@floating-ui/dom):vanilla-calendar-proshnpm i --save bootstrap@6.0.0-alpha.1 @floating-ui/dom vanilla-calendar-pro -
安装用于 CSS 编译和后处理的工具:sh
npm i --save-dev autoprefixer postcss postcss-cli sass -
安装本地开发工具(文件监听、脚本运行、静态服务、代码检查):sh
npm i --save-dev nodemon npm-run-all sirv-cli stylelint stylelint-config-twbs-bootstrap
Step 2: Create the project structure
步骤 2:创建项目结构
sh
mkdir {css,scss}
touch index.html scss/styles.scss postcss.config.js stylelint.config.mjsTarget layout:
text
my-project/
├── css/
├── scss/
│ └── styles.scss
├── stylelint.config.mjs
├── index.html
├── package-lock.json
├── package.json
└── postcss.config.jssh
mkdir {css,scss}
touch index.html scss/styles.scss postcss.config.js stylelint.config.mjs目标目录结构:
text
my-project/
├── css/
├── scss/
│ └── styles.scss
├── stylelint.config.mjs
├── index.html
├── package-lock.json
├── package.json
└── postcss.config.jsStep 3: Configure npm scripts and config files
步骤 3:配置 npm 脚本与配置文件
-
— run Autoprefixer after Sass:
postcss.config.jsjsconst autoprefixer = require('autoprefixer') module.exports = { plugins: [ autoprefixer ] } -
— link the compiled CSS and load Bootstrap's pre-built JS bundle (no bundler here, so use
index.html):disthtml<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap w/ npm</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <div class="container py-4 px-3 mx-auto"> <h1>Hello, Bootstrap and npm!</h1> <button class="btn-solid theme-primary">Primary button</button> </div> <script type="module" src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script> </body> </html> -
— add scripts. Note
package.jsonso Sass resolves--load-path=node_modules:bootstrapjson{ "scripts": { "css-compile": "sass --load-path=node_modules --style expanded --source-map --embed-sources scss:css", "css-prefix": "postcss --config postcss.config.js --replace \"css/*.css\" \"!css/*.min.css\"", "build": "npm-run-all --sequential css-compile css-prefix", "watch": "nodemon --watch scss/ --ext scss --exec \"npm run build\"", "serve": "sirv --port 8080 --dev .", "start": "npm-run-all build --parallel watch serve", "lint": "stylelint \"scss/**/*.scss\"" } } -
:
stylelint.config.mjsjs/** @type {import('stylelint').Config} */ export default { extends: 'stylelint-config-twbs-bootstrap' } -
Start the dev server:sh
npm start
-
— 在 Sass 编译后运行 Autoprefixer:
postcss.config.jsjsconst autoprefixer = require('autoprefixer') module.exports = { plugins: [ autoprefixer ] } -
— 引入编译后的 CSS,并加载 Bootstrap 预构建的 JS 包(此处无打包工具,因此使用
index.html目录下的文件):disthtml<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap w/ npm</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <div class="container py-4 px-3 mx-auto"> <h1>Hello, Bootstrap and npm!</h1> <button class="btn-solid theme-primary">Primary button</button> </div> <script type="module" src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script> </body> </html> -
— 添加脚本。注意此处的
package.json参数用于让 Sass 能够解析--load-path=node_modules包:bootstrapjson{ "scripts": { "css-compile": "sass --load-path=node_modules --style expanded --source-map --embed-sources scss:css", "css-prefix": "postcss --config postcss.config.js --replace \"css/*.css\" \"!css/*.min.css\"", "build": "npm-run-all --sequential css-compile css-prefix", "watch": "nodemon --watch scss/ --ext scss --exec \"npm run build\"", "serve": "sirv --port 8080 --dev .", "start": "npm-run-all build --parallel watch serve", "lint": "stylelint \"scss/**/*.scss\"" } } -
:
stylelint.config.mjsjs/** @type {import('stylelint').Config} */ export default { extends: 'stylelint-config-twbs-bootstrap' } -
启动开发服务器:sh
npm start
Step 4: Import Bootstrap's Sass
步骤 4:引入 Bootstrap 的 Sass
Add to using the modern rule (never ):
scss/styles.scss@use@importscss
// Import all of Bootstrap's CSS
@use "bootstrap/scss/bootstrap";Customize tokens with — include only the keys you want to change:
with ()scss
@use "bootstrap/scss/bootstrap" with (
$root-tokens: (
--border-radius: .25rem,
--spacer: 1.5rem,
)
);Override CSS custom properties through the token maps ( and each component's map), following Customize › Sass. Token names are written unprefixed in Sass (e.g. , ); Bootstrap's dist/CDN CSS runs PostCSS to add the prefix, so compiling the source yourself keeps them unprefixed.
$root-tokens$*-tokens--border-radius--spacer--bs-使用现代的 规则将以下内容添加到 中(请勿使用 ):
@usescss/styles.scss@importscss
// Import all of Bootstrap's CSS
@use "bootstrap/scss/bootstrap";使用 自定义 token —— 仅包含你需要修改的键即可:
with ()scss
@use "bootstrap/scss/bootstrap" with (
$root-tokens: (
--border-radius: .25rem,
--spacer: 1.5rem,
)
);你可以通过 token 映射( 以及各组件的 映射)覆盖 CSS 自定义属性,具体可参考 自定义 › Sass。在 Sass 中,token 名称不带前缀(例如 、);Bootstrap 的 dist/CDN 版本 CSS 会通过 PostCSS 添加 前缀,因此自行编译源码时 token 不会带此前缀。
$root-tokens$*-tokens--border-radius--spacer--bs-Step 5: Verify
步骤 5:验证
- Run and open
npm start— thehttp://localhost:8080and.containerbutton should be styled once the CSS compiles..btn-solid.theme-primary - Confirm is generated and updates when you edit
css/styles.css(watch is running).scss/styles.scss - Confirm no console errors from the JS bundle.
- Run to check Sass against Bootstrap's stylelint config.
npm run lint - 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 sass-js project.
- 运行 并打开
npm start—— CSS 编译完成后,http://localhost:8080容器和.container按钮应该会显示对应样式。.btn-solid.theme-primary - 确认 已生成,且当你编辑
css/styles.css时文件会自动更新(监听功能已运行)。scss/styles.scss - 确认 JS 包没有在控制台抛出错误。
- 运行 ,使用 Bootstrap 的 stylelint 配置检查 Sass 代码。
npm run lint - Bootstrap 6 需要浏览器支持 和
oklch()特性。color-mix()
如需实现仅引入你用到的 Bootstrap 功能的优化构建,请参考 twbs/examples 中的 sass-js 项目。