精简的 Sass 设置
在资源管道中使用 Sass 时,请确保通过只 @use 你需要的 partials 来优化 Bootstrap。你最大的优化可能来自 bootstrap.scss 中的 Layout & Components 部分。
@forward "banner";
// Configuration variables and maps, such as $utilities. Forwarding config makes
// them configurable from this entrypoint with `@use ... with (...)`. Emits no CSS.
@forward "config";
@forward "colors";
// Theme color maps ($theme-colors, $theme-bgs, etc.). Forwarding makes them
// configurable from this entrypoint with `@use ... with (...)`. Emits no CSS.
@forward "theme";
// Global CSS variables, layer definitions, and configuration
@forward "root";
// Subdir imports
@forward "content";
@forward "layout";
@forward "forms";
@forward "buttons";
// Components
@forward "accordion";
@forward "alert";
@forward "avatar";
@forward "badge";
@forward "breadcrumb";
@forward "chip";
@forward "card";
@forward "carousel";
@forward "dialog";
@forward "menu";
@forward "list-group";
@forward "nav";
@forward "nav-overflow";
@forward "navbar";
@forward "drawer";
@forward "pagination";
@forward "placeholder";
@forward "popover";
@forward "progress";
@forward "spinner";
@forward "stepper";
@forward "toasts";
@forward "tooltip";
@forward "transitions";
// Helpers
@forward "helpers";
// Utilities
@forward "utilities/api";
如果你不使用某个组件,请注释掉或完全删除其 @forward 条目。例如,如果你不使用轮播,请移除该行以节省编译后 CSS 的文件大小。请记住,Sass partials 之间存在一些依赖关系,这可能使省略某个文件变得更加困难。
最干净的做法是维护你自己的入口文件,只转发你需要的 Bootstrap 部分,而不是导入整个 bootstrap。将上面的导入栈复制到你自己的样式表中,然后注释掉你不使用的组件:
// custom-bootstrap.scss
@forward "bootstrap/scss/colors";
@forward "bootstrap/scss/root";
// Layout & content
@forward "bootstrap/scss/content";
@forward "bootstrap/scss/layout";
@forward "bootstrap/scss/forms";
@forward "bootstrap/scss/buttons";
// Components — keep only what you need
@forward "bootstrap/scss/alert";
@forward "bootstrap/scss/badge";
@forward "bootstrap/scss/card";
// @forward "bootstrap/scss/accordion"; // unused
// @forward "bootstrap/scss/carousel"; // unused
// @forward "bootstrap/scss/dialog"; // unused
// Helpers & utilities
@forward "bootstrap/scss/helpers";
@forward "bootstrap/scss/utilities/api";
然后编译你的 custom-bootstrap.scss 而不是 Bootstrap 默认的 bootstrap.scss。只需记住保留 colors 和 root(以及剩余组件所依赖的任何 partials),因为框架的其余部分构建在它们定义的变量和 tokens 之上。
精简的 JavaScript
Bootstrap 的 JavaScript 在我们的主要 dist 文件(bootstrap.js 和 bootstrap.min.js)中包含每个组件,甚至在我们的 bundle 文件(bootstrap.bundle.js 和 bootstrap.bundle.min.js)中包含我们的主要依赖(Floating UI 和 Vanilla Calendar Pro)。当你通过 Sass 进行自定义时,请务必移除相关的 JavaScript。
例如,假设你使用自己的 JavaScript 打包器,如 Webpack、Parcel 或 Vite,你只需导入计划使用的 JavaScript。在下面的示例中,我们展示了如何仅包含我们的对话框 JavaScript:
// Import just what we need
// import 'bootstrap/js/dist/alert'
// import 'bootstrap/js/dist/button'
// import 'bootstrap/js/dist/carousel'
// import 'bootstrap/js/dist/collapse'
import 'bootstrap/js/dist/dialog'
// import 'bootstrap/js/dist/menu'
// import 'bootstrap/js/dist/drawer'
// import 'bootstrap/js/dist/popover'
// import 'bootstrap/js/dist/scrollspy'
// import 'bootstrap/js/dist/tab'
// import 'bootstrap/js/dist/toast'
// import 'bootstrap/js/dist/tooltip'
以这种方式导入文件会因其副作用而运行它,从而注册该组件的 data 属性 API——因此 data-bs-toggle 之类无需任何额外代码即可继续工作。这样,你就不会为按钮、轮播和工具提示等组件包含任何你不打算使用的 JavaScript。如果你导入菜单、工具提示或弹出框,请务必在 package.json 文件中列出 Floating UI 依赖。如果你使用日期选择器,请务必同时列出 Vanilla Calendar Pro 依赖。
请注意! bootstrap/js/dist 中的文件使用默认导出。要使用它们,请执行以下操作:
import Dialog from 'bootstrap/js/dist/dialog'
const dialog = new Dialog(document.getElementById('myDialog'))
Autoprefixer .browserslistrc
Bootstrap 依赖 Autoprefixer 自动为某些 CSS 属性添加浏览器前缀。前缀由我们的 .browserslistrc 文件决定,该文件位于 Bootstrap 仓库根目录以及 npm 包根目录(node_modules/bootstrap/.browserslistrc)。自定义此浏览器列表并重新编译 Sass 将自动从编译后的 CSS 中移除一些 CSS,如果存在该浏览器或版本特有的供应商前缀。
未使用的 CSS
PurgeCSS 会分析你的标记并剔除任何它找不到在用的 CSS 选择器,这可以显著缩小 Bootstrap 编译后的 CSS。下面的设置假设项目由静态 HTML 页面以及 Bootstrap 的 JavaScript 组成。
首先,将 PurgeCSS 安装为开发依赖:
npm install --save-dev purgecss
然后在项目根目录添加 purgecss.config.js:
// purgecss.config.js
export default {
// Files to scan for the class names actually in use
content: ['./**/*.html', './**/*.js'],
// Your compiled Bootstrap CSS (plus any of your own)
css: ['./css/bootstrap.css'],
output: './css/bootstrap.purged.css',
// Bootstrap 6 uses ":" in class names (e.g. `md:d-none`, `hover:opacity-50`),
// so the extractor must keep colons and slashes.
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || [],
safelist: {
// Classes added at runtime by Bootstrap's JS that never appear in your HTML
standard: ['show', 'active', 'hiding', 'dialog-open'],
// Keep component families that are generated or toggled dynamically
greedy: [/^carousel/, /^menu/, /^drawer/, /^dialog/, /^tooltip/, /^popover/, /^data-vc/]
}
}
在构建过程中运行它,让 PurgeCSS 使用该配置:
npx purgecss --config purgecss.config.js
然后提供 bootstrap.purged.css 而不是完整文件。
清理后请彻底测试。 Bootstrap 使用 JavaScript 切换许多类(.show、.active、.hiding 等),并动态生成其他类(轮播、菜单、对话框)。PurgeCSS 在其扫描的文件中找不到的任何内容都将被移除,因此请使用 safelist 选项保护运行时类,并验证每个交互组件仍可正常工作。
如需更深入的演练,社区撰写了一些有用的指南:
压缩和 gzip
只要有可能,请务必压缩你提供给访客的所有代码。如果你使用 Bootstrap dist 文件,请尽量使用压缩版本(由 .min.css 和 .min.js 扩展名表示)。如果你使用自己的构建系统从源代码构建 Bootstrap,请务必为 HTML、CSS 和 JS 实现你自己的压缩器。
非阻塞文件
虽然压缩和使用压缩算法可能看起来足够了,但让你的文件成为非阻塞文件也是使你的站点经过良好优化并足够快的重要一步。
如果你在 Google Chrome 中使用 Lighthouse 插件,你可能遇到过 FCP。首次内容绘制(First Contentful Paint)指标衡量从页面开始加载到页面内容的任何部分渲染在屏幕上的时间。
你可以通过延迟非关键的 JavaScript 或 CSS 来改善 FCP。这是什么意思?简而言之,不需要在页面首次绘制时出现的 JavaScript 或样式表应标记为 async 或 defer 属性。
这确保不太重要的资源稍后加载,不会阻塞首次绘制。另一方面,关键资源可以作为内联脚本或样式包含。
如果你想了解更多相关信息,已经有很多关于此的优秀文章:
- https://developer.chrome.com/docs/lighthouse/performance/render-blocking-resources/
- https://web.dev/articles/defer-non-critical-css
始终使用 HTTPS
你的网站在生产环境中应仅通过 HTTPS 连接可用。HTTPS 提高了所有站点的安全性、隐私性和可用性,而且不存在所谓的非敏感 Web 流量。将网站配置为仅通过 HTTPS 提供服务的步骤因你的架构和 Web 托管提供商而异,因此超出了这些文档的范围。
通过 HTTPS 提供的站点还应通过 HTTPS 连接访问所有样式表、脚本和其他资源。否则,你会向用户发送混合活动内容,导致潜在的漏洞,即站点可能因更改依赖项而受到威胁。这可能导致安全问题并向用户显示浏览器警告。无论你是从 CDN 获取 Bootstrap 还是自行提供,请确保仅通过 HTTPS 连接访问它。