| 1 |
--- |
| 2 |
title: css.PostCSS |
| 3 |
description: Process CSS resources using PostCSS. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [postCSS] |
| 9 |
returnType: resource.Resource |
| 10 |
signatures: ['css.PostCSS [OPTIONS] RESOURCE'] |
| 11 |
aliases: [/functions/resources/postcss/] |
| 12 |
--- |
| 13 |
|
| 14 |
The `css.PostCSS` function transforms CSS using [PostCSS][] and any of its [plugins][]. |
| 15 |
|
| 16 |
## Setup |
| 17 |
|
| 18 |
Step 1 |
| 19 |
: Install [Node.js][]. |
| 20 |
|
| 21 |
Step 2 |
| 22 |
: Install the required Node packages in the root of your project. For example, to install PostCSS, its command-line interface, and the plugin to automatically add vendor prefixes to your CSS: |
| 23 |
|
| 24 |
```sh |
| 25 |
npm install --save-dev postcss postcss-cli autoprefixer |
| 26 |
``` |
| 27 |
|
| 28 |
Step 3 |
| 29 |
: Create a PostCSS configuration file in the root of your project. The current Hugo [environment](g) name is available in the Node context. For example, in this configuration, running `hugo server` disables vendor prefixes but enables an inline sourcemap. Conversely, when building for production, it applies vendor prefixes and disables the sourcemap: |
| 30 |
|
| 31 |
```js {file="postcss.config.mjs" copy=true} |
| 32 |
import autoprefixer from 'autoprefixer'; |
| 33 |
|
| 34 |
const isDev = process.env.HUGO_ENVIRONMENT === 'development'; |
| 35 |
|
| 36 |
export default { |
| 37 |
plugins: [ |
| 38 |
!isDev ? autoprefixer : null |
| 39 |
], |
| 40 |
map: isDev ? { inline: true } : false |
| 41 |
}; |
| 42 |
``` |
| 43 |
|
| 44 |
Step 4 |
| 45 |
: Place your CSS file within the `assets/css` directory. |
| 46 |
|
| 47 |
Step 5 |
| 48 |
: Create a partial template to process the CSS: |
| 49 |
|
| 50 |
```go-html-template {file="layouts/_partials/css.html" copy=true} |
| 51 |
{{ with resources.Get "css/main.css" }} |
| 52 |
{{ $opts := dict |
| 53 |
"inlineImports" true |
| 54 |
}} |
| 55 |
{{ with . | css.PostCSS $opts }} |
| 56 |
{{ if hugo.IsDevelopment }} |
| 57 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 58 |
{{ else }} |
| 59 |
{{ with . | minify | fingerprint }} |
| 60 |
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"> |
| 61 |
{{ end }} |
| 62 |
{{ end }} |
| 63 |
{{ end }} |
| 64 |
{{ end }} |
| 65 |
``` |
| 66 |
|
| 67 |
Step 6 |
| 68 |
: Call the partial template from your base template: |
| 69 |
|
| 70 |
```go-html-template {file="layouts/baseof.html" copy=true} |
| 71 |
<head> |
| 72 |
{{ partial "css.html" . }} |
| 73 |
</head> |
| 74 |
``` |
| 75 |
|
| 76 |
## Options |
| 77 |
|
| 78 |
The `css.PostCSS` function accepts an options map. |
| 79 |
|
| 80 |
`config` |
| 81 |
: (`string`) The path to the directory that contains the PostCSS configuration file. By default, Hugo searches the root of the project directory and any modules for `postcss.config.js`, `postcss.config.mjs`, and `postcss.config.cjs` in that order. Use this option only if your configuration file is located in a custom subdirectory. |
| 82 |
|
| 83 |
`inlineImports` |
| 84 |
: (`bool`) Whether to enable inlining of import statements. It does so recursively, but will only import a file once. Hugo looks for imports relative to the module mount and respects theme overrides. Default is `false`. |
| 85 |
|
| 86 |
Note that Hugo's internal import routine does not adhere to the CSS specification; you can place `@import` statements anywhere in the file. However, external URL imports and imports with media queries are ignored during the inlining process. |
| 87 |
|
| 88 |
The following snippet illustrates an external URL import that Hugo will ignore: |
| 89 |
|
| 90 |
```css |
| 91 |
@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap'); |
| 92 |
``` |
| 93 |
|
| 94 |
`skipInlineImportsNotFound` |
| 95 |
: (`bool`) Whether to allow the build process to continue despite unresolved import statements, preserving the original import declarations. Set this option to `true` if you want to retain standard CSS imports unparsed. Default is `false`. |
| 96 |
|
| 97 |
To avoid using a PostCSS configuration file, you can specify a minimal configuration with these options: |
| 98 |
|
| 99 |
`noMap` |
| 100 |
: (`bool`) Whether to disable the default inline source maps. Default is `false`. |
| 101 |
|
| 102 |
`parser` |
| 103 |
: (`string`) A custom PostCSS parser. |
| 104 |
|
| 105 |
`stringifier` |
| 106 |
: (`string`) A custom PostCSS stringifier. |
| 107 |
|
| 108 |
`syntax` |
| 109 |
: (`string`) Custom PostCSS syntax. |
| 110 |
|
| 111 |
`use` |
| 112 |
: (`string`) A space-delimited list of PostCSS plugins to use. |
| 113 |
|
| 114 |
For example, to pass your plugins and disable source maps directly through the options map instead of a configuration file: |
| 115 |
|
| 116 |
```go-html-template {file="layouts/_partials/css.html" copy=true} |
| 117 |
{{ with resources.Get "css/main.css" }} |
| 118 |
{{ $opts := dict |
| 119 |
"noMap" true |
| 120 |
"use" "autoprefixer postcss-color-alpha" |
| 121 |
}} |
| 122 |
{{ with . | css.PostCSS $opts }} |
| 123 |
{{ if hugo.IsDevelopment }} |
| 124 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 125 |
{{ else }} |
| 126 |
{{ with . | fingerprint }} |
| 127 |
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"> |
| 128 |
{{ end }} |
| 129 |
{{ end }} |
| 130 |
{{ end }} |
| 131 |
{{ end }} |
| 132 |
``` |
| 133 |
|
| 134 |
[Node.js]: https://nodejs.org/en |
| 135 |
[PostCSS]: https://postcss.org/ |
| 136 |
[plugins]: https://postcss.org/docs/postcss-plugins |