| 1 |
--- |
| 2 |
title: resources.PostProcess |
| 3 |
description: Processes the given resource after the build. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: postpub.PostPublishedResource |
| 10 |
signatures: [resources.PostProcess RESOURCE] |
| 11 |
--- |
| 12 |
|
| 13 |
The `resources.PostProcess` function delays resource transformation steps until the build is complete, primarily for tasks like removing unused CSS rules. |
| 14 |
|
| 15 |
## Example |
| 16 |
|
| 17 |
In this example, after the build is complete, Hugo will: |
| 18 |
|
| 19 |
1. Purge unused CSS using the [PurgeCSS][] plugin for [PostCSS][] |
| 20 |
1. Add vendor prefixes to CSS rules using the [Autoprefixer][] plugin for PostCSS |
| 21 |
1. [Minify][] the CSS |
| 22 |
1. [Fingerprint][] the CSS |
| 23 |
|
| 24 |
Step 1 |
| 25 |
: Install [Node.js][]. |
| 26 |
|
| 27 |
Step 2 |
| 28 |
: Install the required Node packages in the root of your project: |
| 29 |
|
| 30 |
```sh {copy=true} |
| 31 |
npm i -D postcss postcss-cli autoprefixer @fullhuman/postcss-purgecss |
| 32 |
``` |
| 33 |
|
| 34 |
Step 3 |
| 35 |
: Enable creation of the `hugo_stats.json` file when building the site. If you are only using this for the production build, consider placing it below [`config/production`][]. |
| 36 |
|
| 37 |
{{< code-toggle file=hugo copy=true >}} |
| 38 |
[build.buildStats] |
| 39 |
enable = true |
| 40 |
{{< /code-toggle >}} |
| 41 |
|
| 42 |
See the [configure build][] documentation for details and options. |
| 43 |
|
| 44 |
Step 4 |
| 45 |
: Create a PostCSS configuration file in the root of your project. |
| 46 |
|
| 47 |
```js {file="postcss.config.mjs" copy=true} |
| 48 |
import autoprefixer from 'autoprefixer'; |
| 49 |
import purgeCSSPlugin from '@fullhuman/postcss-purgecss'; |
| 50 |
|
| 51 |
const purgecss = purgeCSSPlugin({ |
| 52 |
content: ['./hugo_stats.json'], |
| 53 |
defaultExtractor: content => { |
| 54 |
const els = JSON.parse(content).htmlElements; |
| 55 |
return [ |
| 56 |
...(els.tags || []), |
| 57 |
...(els.classes || []), |
| 58 |
...(els.ids || []), |
| 59 |
]; |
| 60 |
}, |
| 61 |
// https://purgecss.com/safelisting.html |
| 62 |
safelist: [] |
| 63 |
}); |
| 64 |
|
| 65 |
export default { |
| 66 |
plugins: [ |
| 67 |
process.env.HUGO_ENVIRONMENT !== 'development' ? purgecss : null, |
| 68 |
autoprefixer, |
| 69 |
] |
| 70 |
}; |
| 71 |
``` |
| 72 |
|
| 73 |
Step 5 |
| 74 |
: Place your CSS file within the `assets/css` directory. |
| 75 |
|
| 76 |
Step 6 |
| 77 |
: If the current environment is not `development`, process the resource with PostCSS: |
| 78 |
|
| 79 |
```go-html-template {copy=true} |
| 80 |
{{ with resources.Get "css/main.css" }} |
| 81 |
{{ if hugo.IsDevelopment }} |
| 82 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 83 |
{{ else }} |
| 84 |
{{ with . | postCSS | minify | fingerprint | resources.PostProcess }} |
| 85 |
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"> |
| 86 |
{{ end }} |
| 87 |
{{ end }} |
| 88 |
{{ end }} |
| 89 |
``` |
| 90 |
|
| 91 |
## Environment variables |
| 92 |
|
| 93 |
Hugo passes the environment variables below to PostCSS, allowing you to do something like: |
| 94 |
|
| 95 |
```js |
| 96 |
process.env.HUGO_ENVIRONMENT !== 'development' ? purgecss : null, |
| 97 |
``` |
| 98 |
|
| 99 |
`PWD` |
| 100 |
: The absolute path to the project working directory. |
| 101 |
|
| 102 |
`HUGO_ENVIRONMENT` |
| 103 |
: The current Hugo environment, set with the `--environment` command line flag. |
| 104 |
Default is `production` for `hugo build` and `development` for `hugo server`. |
| 105 |
|
| 106 |
`HUGO_PUBLISHDIR` |
| 107 |
: The absolute path to the publish directory, typically `public`. This value points to a directory on disk, even when rendering to memory with the `--renderToMemory` command line flag. |
| 108 |
|
| 109 |
`HUGO_FILE_X` |
| 110 |
: Hugo automatically mounts the following files from your project's root directory under `assets/_jsconfig`: |
| 111 |
|
| 112 |
- `babel.config.js`, `babel.config.mjs`, `babel.config.cjs` |
| 113 |
- `postcss.config.js`, `postcss.config.mjs`, `postcss.config.cjs` |
| 114 |
- `tailwind.config.js`, `tailwind.config.mjs`, `tailwind.config.cjs` |
| 115 |
|
| 116 |
For each file, Hugo creates a corresponding environment variable named `HUGO_FILE_:filename:`, where `:filename:` is the uppercase version of the filename with periods replaced by underscores. This allows you to access these files within your JavaScript, for example: |
| 117 |
|
| 118 |
```js |
| 119 |
let tailwindConfig = process.env.HUGO_FILE_TAILWIND_CONFIG_JS || './tailwind.config.js'; |
| 120 |
``` |
| 121 |
|
| 122 |
## Limitations |
| 123 |
|
| 124 |
Do not use `resources.PostProcess` when running Hugo's built-in development server. The examples above specifically prevent this by verifying that the current environment is not `development`. |
| 125 |
|
| 126 |
The `resources.PostProcess` function only works within templates that produce HTML files. |
| 127 |
|
| 128 |
You cannot manipulate the values returned from the resource's methods. For example, the `strings.ToUpper` function in this example will not work as expected: |
| 129 |
|
| 130 |
```go-html-template |
| 131 |
{{ $css := resources.Get "css/main.css" }} |
| 132 |
{{ $css = $css | css.PostCSS | minify | fingerprint | resources.PostProcess }} |
| 133 |
{{ $css.RelPermalink | strings.ToUpper }} |
| 134 |
``` |
| 135 |
|
| 136 |
[Autoprefixer]: https://github.com/postcss/autoprefixer |
| 137 |
[Fingerprint]: /functions/resources/fingerprint/ |
| 138 |
[Minify]: /functions/resources/minify/ |
| 139 |
[Node.js]: https://nodejs.org/en |
| 140 |
[PostCSS]: https://postcss.org/ |
| 141 |
[PurgeCSS]: https://github.com/FullHuman/purgecss |
| 142 |
[`config/production`]: /configuration/introduction/#configuration-directory |
| 143 |
[configure build]: /configuration/build/ |