I started a new client project today and I wanted to try using bootstrap-icons encapsulated directly in the project rather than via CDN.
Unfortunately, I have struggled trying to solve a “Hugo way” to lift the necessary files from node_modules into my Hugo pipeline. I have struggled with various incantations of [[modules.mounts]] and have not been able to figure it out yet.
The basic structure of the project:
I’ve created a very basic Hugo project and included bootstrap and bootstrap-icons via npm:
npm install --save bootstrap
npm install --save bootstrap-icons
I point to the bootstrap and include it in my Hugo pipeline via a couple of directives:
config.toml:
[module]
[[module.mounts]]
source = "assets"
target = "assets"
[[module.mounts]]
source = "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
target = "assets/js/bootstrap.bundle.min.js"
And in the html via the following:
head.html partial:
{{ $options := (dict "targetPath" "css/styles.css" "outputStyle" "compressed") }}
{{ $style := resources.Get "sass/main.scss" | toCSS $options | minify }}
<link rel="stylesheet" href="{{ $style.RelPermalink }}" media="screen">
(main.scss is in my assets/sass, and does the bootstrap scss inclusion from node_modules)
footer.html partial:
{{ $bootstrap := resources.Get "js/bootstrap.bundle.min.js" }}
{{ $js := slice $bootstrap | resources.Concat "js/bundle.js" | resources.Minify }}
<script src="{{$js.RelPermalink}}" defer></script>
This above all works flawlessly. I get a basic website with Bootstrap applied.
Now, I want to get the bootstrap-icons into the pipeline.
First, I need to update my assets/sass/main.scss to include the bootstrap-icons.scss:
@import "node_modules/bootstrap-icons/font/bootstrap-icons.scss";
A quick inspection of the bootstrap-icons.scss shows that it expects to be able to include the icon font from a local subdirectory:
$bootstrap-icons-font-src: url("./fonts/bootstrap-icons.woff2?524846017b983fc8ded9325d94ed40f3") format("woff2"),
url("./fonts/bootstrap-icons.woff?524846017b983fc8ded9325d94ed40f3") format("woff") !default;
And I thought I could solve this by doing the following mount:
[[module.mounts]]
source = "node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff"
target = "assets/sass/fonts/bootstrap-icons.woff"
[[module.mounts]]
source = "node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff2"
target = "assets/sass/fonts/bootstrap-icons.woff2"
And then … doing … something with resources.Get() … but I can’t find exactly what I am looking for in the docs.
So I am not quite there.
What I am trying to get happen is the have a copy of font files under node_modules land properly in the published destination (public/css/fonts) so that the compiled css file (public/css/styles.min.css) can properly @import the fonts.