Debian Codegraph @

docs/content/en/functions/css/Sass.md

raw
1 ---
2 title: css.Sass
3 description: Transpiles Sass to CSS.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [toCSS]
9 returnType: resource.Resource
10 signatures: ['css.Sass [OPTIONS] RESOURCE']
11 aliases: [/functions/resources/tocss/]
12 ---
13
14 Transpile Sass to CSS using the LibSass transpiler included in Hugo's extended and extended/deploy editions, or [install Dart Sass](#dart-sass) to use the latest features of the Sass language.
15
16 <!-- TODO
17 Remove the admonition below somewhere after v0.168.0, 15 minor releases
18 after deprecation.
19 -->
20
21 > [!WARNING]
22 > The embedded LibSass transpiler was deprecated in [v0.153.0][] and will be removed in a future release. Use the Dart Sass transpiler instead by setting the `transpiler` option to `dartsass` as shown in the examples below.
23
24 Sass has two forms of syntax: [SCSS][] and [indented][]. Hugo supports both.
25
26 ## Options
27
28 The `css.Sass` function accepts an options map.
29
30 `enableSourceMap`
31 : (`bool`) Whether to generate a source map. Default is `false`.
32
33 ```go-html-template
34 {{ $opts := dict
35 "transpiler" "dartsass"
36 "enableSourceMap" true
37 }}
38 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
39 ```
40
41 `includePaths`
42 : (`slice`) A slice of paths, relative to the project root, that the transpiler will use when resolving `@use` and `@import` statements.
43
44 ```go-html-template
45 {{ $opts := dict
46 "transpiler" "dartsass"
47 "includePaths" (slice "node_modules/bootstrap/scss")
48 }}
49 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
50 ```
51
52 `outputStyle`
53 : (`string`) The output style of the resulting CSS. With LibSass, one of `nested` (default), `expanded`, `compact`, or `compressed`. With Dart Sass, either `expanded` (default) or `compressed`.
54
55 ```go-html-template
56 {{ $opts := dict
57 "transpiler" "dartsass"
58 "outputStyle" "compressed"
59 }}
60 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
61 ```
62
63 `precision`
64 : (`int`) The precision of floating point math. Applicable to LibSass. Default is `8`.
65
66 ```go-html-template
67 {{ $opts := dict
68 "transpiler" "dartsass"
69 "precision" 10
70 }}
71 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
72 ```
73
74 `silenceDeprecations`
75 : {{< new-in 0.139.0 />}}
76 : (`slice`) A slice of deprecation IDs to silence. IDs are enclosed in brackets within Dart Sass warning messages (e.g., `import` in `WARN Dart Sass: DEPRECATED [import]`). Applicable to Dart Sass.
77
78 ```go-html-template
79 {{ $opts := dict
80 "transpiler" "dartsass"
81 "silenceDeprecations" (slice "import")
82 }}
83 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
84 ```
85
86 `silenceDependencyDeprecations`
87 : {{< new-in 0.146.0 />}}
88 : (`bool`) Whether to silence deprecation warnings from dependencies, where a dependency is considered any file transitively imported through a load path. This does not apply to `@warn` or `@debug` rules. Default is `false`.
89
90 ```go-html-template
91 {{ $opts := dict
92 "transpiler" "dartsass"
93 "silenceDependencyDeprecations" true
94 }}
95 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
96 ```
97
98 `sourceMapIncludeSources`
99 : (`bool`) Whether to embed sources in the generated source map. Applicable to Dart Sass. Default is `false`.
100
101 ```go-html-template
102 {{ $opts := dict
103 "transpiler" "dartsass"
104 "enableSourceMap" true "sourceMapIncludeSources" true
105 }}
106 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
107 ```
108
109 `targetPath`
110 : (`string`) The publish path for the transformed resource, relative to the [`publishDir`][]. If unset, the target path defaults to the asset's original path with a `.css` extension.
111
112 ```go-html-template
113 {{ $opts := dict
114 "transpiler" "dartsass"
115 "targetPath" "css/bundle.css"
116 }}
117 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
118 ```
119
120 `transpiler`
121 : (`string`) The transpiler to use, either `libsass` or `dartsass`. Hugo's extended and extended/deploy editions include the LibSass transpiler. To use the Dart Sass transpiler, see the [installation instructions](#dart-sass). Default is `libsass`.
122
123 ```go-html-template
124 {{ $opts := dict "transpiler" "dartsass" }}
125 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
126 ```
127
128 `vars`
129 : (`map`) A map of key-value pairs used to generate Sass variables. The `css.Sass` function injects these variables into the stylesheet when it encounters the `hugo:vars` internal identifier within a `@use` or `@import` statement.
130
131 ```go-html-template
132 {{ $vars := dict
133 "font-family" "\"Times New Roman\", Times, serif"
134 "font-size" "24px"
135 "primary-color" "blue"
136 }}
137 {{ $opts := dict
138 "transpiler" "dartsass"
139 "vars" $vars
140 }}
141 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
142 ```
143
144 In the example above, using the identifier in your stylesheet allows you to access the values as Sass variables in the `hugo:vars` namespace:
145
146 ```scss
147 @use 'hugo:vars' as v;
148
149 .element {
150 color: v.$primary-color;
151 font-family: v.$font-family;
152 font-size: v.$font-size;
153 }
154 ```
155
156 The above produces output equivalent to:
157
158 ```css
159 .element {
160 color: blue;
161 font-family: "Times New Roman", Times, serif;
162 font-size: 24px;
163 }
164 ```
165
166 {{< new-in 0.161.0 />}}
167
168 The map may optionally contain nested maps. Each nested map is exposed as a separate `hugo:vars/<name>` namespace, where `<name>` is the key of the nested map (lowercased). Top-level scalar values and nested maps are independent. A top-level `@use 'hugo:vars'` only includes scalar values, while `@use 'hugo:vars/<name>'` only includes the scalars from the named nested map.
169
170 ```go-html-template
171 {{ $vars := dict
172 "font-family" "\"Times New Roman\", Times, serif"
173 "font-size" "24px"
174 "primary-color" "blue"
175 "mobile" (dict
176 "font-size" "12px"
177 "primary-color" "red"
178 )
179 }}
180 {{ $opts := dict
181 "transpiler" "dartsass"
182 "vars" $vars
183 }}
184 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
185 ```
186
187 In the stylesheet, reference each nested namespace with a separate `@use` statement. Assign an alias to access the variables from that namespace:
188
189 ```scss
190 @use 'hugo:vars' as v;
191 @use 'hugo:vars/mobile' as mobile;
192
193 body {
194 color: v.$primary-color;
195 font-family: v.$font-family;
196 font-size: v.$font-size;
197 }
198
199 @media (max-width: 650px) {
200 body {
201 color: mobile.$primary-color;
202 font-size: mobile.$font-size;
203 }
204 }
205 ```
206
207 The above produces output equivalent to:
208
209 ```css
210 body {
211 color: blue;
212 font-family: "Times New Roman", Times, serif;
213 font-size: 24px;
214 }
215
216 @media (max-width: 650px) {
217 body {
218 color: red;
219 font-size: 12px;
220 }
221 }
222 ```
223
224 The `vars` option is useful for setting Sass variables within your project configuration.
225
226 {{< code-toggle file=hugo >}}
227 [params.theme.style]
228 font-family = '"Times New Roman", Times, serif'
229 font-size = '24px'
230 primary-color = 'blue'
231
232 [params.theme.style.mobile]
233 font-size = '12px'
234 primary-color = 'red'
235 {{< /code-toggle >}}
236
237 ```go-html-template
238 {{ $opts := dict
239 "transpiler" "dartsass"
240 "vars" site.Params.theme.style }}
241 {{ $r := resources.Get "sass/main.scss" | css.Sass $opts }}
242 ```
243
244 When passing a `vars` map to the `css.Sass` function, Hugo detects common typed CSS values such as `24px` or `#FF0000` using regular expression matching. If necessary, you can bypass automatic type inference by using the [`css.Quoted`][] or [`css.Unquoted`][] function to explicitly indicate a value's type.
245
246 ## Example
247
248 ```go-html-template {copy=true}
249 {{ with resources.Get "sass/main.scss" }}
250 {{ $opts := dict
251 "enableSourceMap" hugo.IsDevelopment
252 "outputStyle" (cond hugo.IsDevelopment "expanded" "compressed")
253 "targetPath" "css/main.css"
254 "transpiler" "dartsass"
255 "vars" site.Params.styles
256 "includePaths" (slice "node_modules/bootstrap/scss")
257 }}
258 {{ with . | css.Sass $opts }}
259 {{ if hugo.IsDevelopment }}
260 <link rel="stylesheet" href="{{ .RelPermalink }}">
261 {{ else }}
262 {{ with . | fingerprint }}
263 <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
264 {{ end }}
265 {{ end }}
266 {{ end }}
267 {{ end }}
268 ```
269
270 ## Dart Sass
271
272 <!-- TODO
273 Revise the paragraphs below somewhere after v0.168.0, 15 minor releases
274 after the deprecation of the LibSass transpiler.
275 -->
276
277 Hugo's extended and extended/deploy editions include [LibSass][] to transpile Sass to CSS. In 2020, the Sass team deprecated LibSass in favor of [Dart Sass][].
278
279 Use the latest features of the Sass language by installing Dart Sass in your development and production environments.
280
281 ### Installation overview
282
283 Dart Sass is compatible with Hugo v0.114.0 and later.
284
285 If you have been using Embedded Dart Sass[^1] with Hugo v0.113.0 and earlier, uninstall Embedded Dart Sass, then install Dart Sass. If you have installed both, Hugo will use Dart Sass.
286
287 If you install Hugo as a [Snap package][] there is no need to install Dart Sass. The Hugo Snap package includes Dart Sass.
288
289 ### Installing in a development environment
290
291 When you install Dart Sass somewhere in your PATH, Hugo will find it.
292
293 OS | Package manager | Site | Installation
294 :-------|:----------------|:-------------------|:-----------------------------
295 Linux | Homebrew | [brew.sh][] | `brew install sass/sass/sass`
296 Linux | Snap | [snapcraft.io][] | `sudo snap install dart-sass`
297 macOS | Homebrew | [brew.sh][] | `brew install sass/sass/sass`
298 Windows | Chocolatey | [chocolatey.org][] | `choco install sass`
299 Windows | Scoop | [scoop.sh][] | `scoop install sass`
300
301 You may also install [prebuilt binaries][] for Linux, macOS, and Windows. You must install the prebuilt binary outside of your project directory and ensure its path is included in your system's PATH environment variable.
302
303 Run `hugo env` to list the active transpilers.
304
305 > [!NOTE]
306 > If you build Hugo from source and run `mage test -v`, the test will fail if you install Dart Sass as a Snap package. This is due to the Snap package's strict confinement model.
307
308 ### Installing in a production environment
309
310 To use Dart Sass with Hugo on a [CI/CD](g) platform, you typically must modify your build workflow to install Dart Sass before the Hugo site build begins. This is because these platforms don't have Dart Sass pre-installed, and Hugo needs it to process your Sass files.
311
312 There's one key exception where you can skip this step: you have committed your `resources` directory to your repository. This is only possible if:
313
314 - You have not changed Hugo's default asset cache location.
315 - You have not set [`useResourceCacheWhen`][] to never in your project configuration.
316
317 By committing the `resources` directory, you're providing the pre-built CSS files directly to your CI/CD platform, so it doesn't need to run the Sass compilation itself.
318
319 For examples of how to install Dart Sass in a production environment, see these hosting guides:
320
321 - [Cloudflare][]
322 - [GitHub Pages][]
323 - [GitLab Pages][]
324 - [Netlify][]
325 - [Render][]
326 - [SourceHut][]
327 - [Vercel][]
328
329 [^1]: In 2023, the Sass team deprecated Embedded Dart Sass in favor of Dart Sass.
330
331 [Cloudflare]: /host-and-deploy/host-on-cloudflare/
332 [Dart Sass]: https://sass-lang.com/dart-sass/
333 [GitHub Pages]: /host-and-deploy/host-on-github-pages/
334 [GitLab Pages]: /host-and-deploy/host-on-gitlab-pages/
335 [LibSass]: https://sass-lang.com/libsass
336 [Netlify]: /host-and-deploy/host-on-netlify/
337 [Render]: /host-and-deploy/host-on-render/
338 [SCSS]: https://sass-lang.com/documentation/syntax#scss
339 [Snap package]: https://snapcraft.io/hugo
340 [SourceHut]: /host-and-deploy/host-on-sourcehut-pages/
341 [Vercel]: /host-and-deploy/host-on-vercel/
342 [`css.Quoted`]: /functions/css/quoted/
343 [`css.Unquoted`]: /functions/css/unquoted/
344 [`publishDir`]: /configuration/all/#publishdir
345 [`useResourceCacheWhen`]: /configuration/build/#useresourcecachewhen
346 [brew.sh]: https://brew.sh/
347 [chocolatey.org]: https://community.chocolatey.org/packages/sass
348 [indented]: https://sass-lang.com/documentation/syntax#the-indented-syntax
349 [prebuilt binaries]: https://github.com/sass/dart-sass/releases/latest
350 [scoop.sh]: https://scoop.sh/#/apps?q=sass
351 [snapcraft.io]: https://snapcraft.io/dart-sass
352 [v0.153.0]: https://github.com/gohugoio/hugo/releases/tag/v0.153.0