In my theme, I used to check in Bootstrap files manually under /assets/paige/bootstrap
. The “dist” CSS and JS files, individual JS files, and SCSS files were all in that directory. By default, my theme would minimize and fingerprint /assets/paige/bootstrap/bootstrap.css
.
There was a site parameter for customizing the primary Bootstrap color. According to this Stack Overflow answer, you just have to set the SCSS variable $primary
before importing bootstrap.scss
:
$primary: purple;
@import "bootstrap";
I had a /assets/paige/bootstrap/custom.scss
file that did just this, except it was parameterized with a site config value:
$primary: {{ .Param "paige.color" | default "#0d6efd" }};
@import "bootstrap";
This worked great, even with libsass. So, if paige.color
was unset, then /assets/paige/bootstrap/bootstrap.css
was used; if it was set, then /assets/paige/bootstrap/custom.scss
was used.
Then I noticed that the size of my repo was getting too large. I wanted to keep Bootstrap out of the repo, but have it available for building. I switched to declaring a Bootstrap dependency in package.json
:
{
"comments": {
"dependencies": {
"bootstrap": "project",
"bootstrap-icons": "project",
"katex": "project"
},
"devDependencies": {}
},
"dependencies": {
"bootstrap": "^5.3.0-alpha1",
"bootstrap-icons": "^1.10.3",
"katex": "^0.16.4"
},
"devDependencies": {}
}
I created a package.hugo.json
:
{
"dependencies": {
"bootstrap": "^5.3.0-alpha1",
"bootstrap-icons": "^1.10.3",
"katex": "^0.16.4"
}
}
I mounted Bootstrap files into /assets/paige/bootstrap
in config.yaml
:
module:
hugoVersion:
min: "0.101.0"
extended: true
mounts:
# Default mounts
- source: "archetypes"
target: "archetypes"
- source: "assets"
target: "assets"
- source: "content"
target: "content"
- source: "data"
target: "data"
- source: "i18n"
target: "i18n"
- source: "layouts"
target: "layouts"
- source: "static"
target: "static"
# Bootstrap
- source: "node_modules/bootstrap/dist/css"
target: "assets/paige/bootstrap"
includeFiles: ["/bootstrap.css"]
- source: "node_modules/bootstrap/dist/js"
target: "assets/paige/bootstrap"
includeFiles: ["/bootstrap.bundle.js"]
- source: "node_modules/bootstrap/js/dist"
target: "static/paige/bootstrap"
- source: "node_modules/bootstrap/scss"
target: "static/paige/bootstrap"
# Bootstrap Icons
- source: "node_modules/bootstrap-icons/font/fonts"
target: "static/paige/bootstrap-icons/fonts"
- source: "node_modules/bootstrap-icons/font"
target: "assets/paige/bootstrap-icons"
includeFiles: ["/bootstrap-icons.css"]
# Katex
- source: "node_modules/katex/contrib/auto-render"
target: "assets/paige/katex"
includeFiles: ["/auto-render.js"]
- source: "node_modules/katex/dist/fonts"
target: "static/paige/katex/fonts"
- source: "node_modules/katex/dist"
target: "assets/paige/katex"
includeFiles: ["/katex.css", "/katex.js"]
This all seemed to work. The Bootstrap CSS was loading and rendering as before. Great!
Fast forward to today: I noticed that if I set paige.color
, nothing happens. Using binary search, I determined that it was the commit that made the above changes (mounting, etc.) that broke it.
At first, it was a “TOCSS” error about not finding “bootstrap”, if I remember correctly. Through trial and error, I figured it might be an issue with libsass, and I should switch to dartsass. I did that, and the first error went away, and the site built without errors.
However, the color is unchanged.
I checked the HTML, and the custom file is indeed being transpiled:
<link crossorigin="anonymous" href="/paige/bootstrap/custom.min.a0c9268bba8796b8f41a578eded55c047b6b2e9d7cd1b2d5b845bc474a2f669b.css" integrity="sha256-oMkmi7qHlrj0GleO3tVcBHtrLp180bLVuEW8R0ovZps=" referrerpolicy="no-referrer" rel="stylesheet">
However, the primary color appears to be unchanged (set to #ff0000
):
--bs-primary: #0d6efd;
Even if you hard-code it in custom.scss
:
$primary: #ff0000;
@import "bootstrap";
Adding a little test to make sure the variable is working:
$primary: #ff0000;
.wjf { color: $primary; }
@import "bootstrap";
has the expected result:
.wjf {
color: red
}
/*!* Bootstrap v5.3.0-alpha1 (https://getbootstrap.com/)
* Copyright 2011-2022 The Bootstrap Authors
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)*/
:root, [data-bs-theme=light] {
except --bs-primary
is still unaffected:
--bs-primary: #0d6efd;
So I’m left wondering: How has mounting Bootstrap resulted in Bootstrap variable overrides no longer working? And how can I get it working again?
Thanks for reading.
Edit: Hugo info:
$ hugo env
hugo v0.111.1+extended darwin/amd64 BuildDate=unknown
GOOS="darwin"
GOARCH="amd64"
GOVERSION="go1.20.1"
github.com/sass/libsass="3.6.5"
github.com/webmproject/libwebp="v1.2.4"
github.com/sass/dart-sass-embedded/protocol="1.2.0"
github.com/sass/dart-sass-embedded/compiler="1.58.3"
github.com/sass/dart-sass-embedded/implementation="1.58.3"
Links: