Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup SCSS with Hugo

Tags:

sass

hugo

I'm pretty new with Hugo and am struggling a bit with the documentation as it feels pretty fragmented with incomplete examples.

I've created a new site hugo new site site-name along with a new theme hugo new theme theme-name.

In the documentation page for SASS/SCSS there's the following example:

{{ $sass := resources.Get "sass/main.scss" }}
{{ $style := $sass | resources.ToCSS }}

Not sure where that's supposed to go, how the whole pipeline works. Also, this seems to be specifically looking for files under an assets folder even though the theme is created with a static/css folder. Most examples I find on the web are all old setups using gulp to compile before the support being added to hugo (from my understanding)

like image 591
LostBalloon Avatar asked Jan 05 '19 23:01

LostBalloon


People also ask

What is LibSass?

LibSass is an implementation of Sass in C/C++, designed to be easy to integrate into many different languages. However, as time wore on it ended up lagging behind Dart Sass in features and CSS compatibility. LibSass is now deprecated—new projects should use Dart Sass instead.

What is DART sass?

Dart Sass is the primary implementation of Sass, which means it gets new features before any other implementation. It's fast, easy to install, and it compiles to pure JavaScript which makes it easy to integrate into modern web development workflows. Find out more or help out with its development on GitHub.


2 Answers

You can use hugo's extended (like https://github.com/gohugoio/hugo/releases/download/v0.53/hugo_extended_0.53_Windows-64bit.zip) version which automatically compiles SCSS to CSS for you. You can then customize all the setup. If you don't want to/aren't using the extended version, then ofc you will have to go old school with a watcher like ruby SASS or Gulp, etc.

Also please refer: https://gohugo.io/news/0.43-relnotes/ , see Notes. "Hugo is now released with two binary version: One with and one without SCSS/SASS support. At the time of writing, this is only available in the binaries on the GitHub release page. Brew, Snap builds etc. will come. But note that you only need the extended version if you want to edit SCSS. For your CI server, or if you don’t use SCSS, you will most likely want the non-extended version."

I personally use the extended version; that too with Gitlab CI without any issues. I always write/edit SCSS; run hugo and it does the rest. You also have to make sure your theme supports/plays well with it. Theme I use (supports SCSS): https://github.com/luizdepra/hugo-coder/

like image 159
Aaditya Maheshwari Avatar answered Oct 19 '22 04:10

Aaditya Maheshwari


Not sure where that's supposed to go, how the whole pipeline works

That code is supposed to go inside HTML specifically where you normally add CSS. The $styles variable in the code will contain the location of the processed CSS file along with other details if any.


Here are the steps to set up your SCSS pipeline in Hugo,

  1. Create your scss somewhere within your assets directory. The default asset directory is /assets. Ex: /assets/sass/main.scss
  2. Go to your base HTML layout or any other partial section where you'll import your CSS files normally and add the code in the pipeline documentation there. The URL next to resources.Get is relative to the assets directory defined in your configuration file. In my case it is like the following,
{{ $sass := resources.Get "sass/main.scss" }}
{{ $style := $sass | resources.ToCSS | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $style.Permalink }}" integrity="{{ $style.Data.Integrity }}">
like image 27
Kolappan N Avatar answered Oct 19 '22 05:10

Kolappan N