fcc3ed651Remove some expired new-ina9c5981f5Fix cascade example82bb250faAdd some lines about permalinks tokens in front matter328fe564eRemove some outdated new-infb140b153Hide showcase menu entry42d9d1c79Update image formats from which EXIF data can be extracted09ad56b6enetlify: Hugo 0.130.01d503f846Merge branch 'tempv0.130.0'e2458074dmath: Add trigonometric functions and some angle helper functions392afc8f9Disable the showcase section for now0300750f2Improve example of image render hook60a9306afImprove description of the .Site.RegularPages method8d759175dFix typos55daa4554Update XxHash.md397c81cb7Add namespace for hash functions70fe8d2f0netlify: Bump Hugo 0.129.05a9771affMerge branch 'tempv0.129.0'f9146575bFix typoe6e1fea49Fix typo in Hugo docs | functions | partial732d10ec4source: Expose GitInfo Body34c97e639netlify: Hugo 0.128.23270587e9Fix typo727c5396enetlify: Hugo 0.128.180b6ae99cUpdate GitHub Pages workflow file example027134102Update GitHub Pages workflow file example2600a8a2eMiscellaneous edits3fdd5819bUpdate Build.md7764005c3Improve example of render hook directory structure5e3941d82Fix typos748bf065fRestructure templates sectionfafbf6566Update Defer.md012162e0dDocument changes to template functions in v0.128.00990ce35bquick-reference: Update emojis6677a30efUpdate Goldmark configuration documentation4449d530dDocument new pagination config0af8be439Update Defer.md56348196dnetlify: Hugo 0.128.0d67b6d82eUpdate content/en/functions/templates/Defer.md23d996b3dUpdate content/en/functions/templates/Defer.md7f7fb2f27Document templates.Defer5ada1e9d5Fix docs merge (remove shortcode)d27ee6156Merge branch 'tempv0.128.0'5d7317c84Fix typo7c18ee546Update theme83bfea63bUpdate themeb274b3238Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722'ff34a035adeploy: Add stripIndexHtml target optiond9e964bdbmarkup/goldmark: Add the Hugo Goldmark Extras "delete" extensionac5bd16d2deps: Upgrade github.com/alecthomas/chroma v2.13.0 => v2.14.025377171bconfig: Remove extraneous BuildConfig setting0d2044f6ddocs: Regen docshelpera2548dac9markup/goldmark: Support extras extension9d0c86ee8commands: Add gen chromastyles --lineNumbersTableStyle flag git-subtree-dir: docs git-subtree-split:fcc3ed651a
5.4 KiB
title, description, categories, keywords, action, toc
| title | description | categories | keywords | action | toc | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| resources.PostProcess | Processes the given resource after the build. |
|
true |
{{ with resources.Get "css/main.css" }}
{{ if hugo.IsDevelopment }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{ else }}
{{ with . | postCSS | minify | fingerprint | resources.PostProcess }}
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
{{ end }}
{{ end }}
{{ end }}
Marking a resource with resources.PostProcess postpones transformations until the build has finished.
Call resources.PostProcess when one or more of the steps in the transformation chain depends on the result of the build.
A prime use case for this is purging unused CSS rules using the PurgeCSS plugin for the PostCSS Node.js package.
CSS Purging
{{% note %}}
There are several ways to set up CSS purging with PostCSS in Hugo. If you have a simple project, you should consider going the simpler route and drop the use of resources.PostProcess and just extract keywords from the templates. See the Tailwind documentation for examples.
{{% /note %}}
- Step 1
- Install Node.js.
- Step 2
- Install the required Node.js packages in the root of your project:
npm i -D postcss postcss-cli autoprefixer @fullhuman/postcss-purgecss
- Step 3
- Create a PostCSS configuration file in the root of your project. You must name this file
postcss.config.jsor another supported file name. For example:
const autoprefixer = require('autoprefixer');
const purgecss = require('@fullhuman/postcss-purgecss')({
content: ['./hugo_stats.json'],
defaultExtractor: content => {
const els = JSON.parse(content).htmlElements;
return [
...(els.tags || []),
...(els.classes || []),
...(els.ids || []),
];
},
// https://purgecss.com/safelisting.html
safelist: []
});
module.exports = {
plugins: [
autoprefixer,
process.env.HUGO_ENVIRONMENT !== 'development' ? purgecss : null
]
};
{{% note %}} {{% include "functions/resources/_common/postcss-windows-warning.md" %}} {{% /note %}}
- Step 4
- Enable creation of the
hugo_stats.jsonfile when building the site. If you are only using this for the production build, consider placing it below config/production.
{{< code-toggle file=hugo >}} [build.buildStats] enable = true {{< /code-toggle >}}
See the configure build documentation for details and options.
- Step 5
- Place your CSS file within the
assets/cssdirectory. - Step 6
- If the current environment is not
development, process the resource with PostCSS:
{{ with resources.Get "css/main.css" }}
{{ if hugo.IsDevelopment }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{ else }}
{{ with . | postCSS | minify | fingerprint | resources.PostProcess }}
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
{{ end }}
{{ end }}
{{ end }}
Environment variables
Hugo passes these environment variables to PostCSS, which allows you to do something like:
process.env.HUGO_ENVIRONMENT === 'production' ? [autoprefixer] : []
- PWD
- The absolute path to the project working directory.
- HUGO_ENVIRONMENT
- The current Hugo environment, set with the
--environmentcommand line flag. Default isproductionforhugoanddevelopmentforhugo server. - HUGO_PUBLISHDIR
- The absolute path to the publish directory (the
publicdirectory). Note that the value will always point to a directory on disk even when runninghugo serverin memory mode. If you write to this folder from PostCSS when running the server, you could run the server with one of these flags:
hugo server --renderToDisk
hugo server --renderStaticToDisk
Also, Hugo will add environment variables for all files mounted below assets/_jsconfig. A default mount will be set up with files in the project root matching this regexp: (babel|postcss|tailwind)\.config\.js.
These will get environment variables named on the form HUGO_FILE_:filename: where :filename: is all upper case with periods replaced with underscore. This allows you to do something like:
let tailwindConfig = process.env.HUGO_FILE_TAILWIND_CONFIG_JS || './tailwind.config.js';
Limitations
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".
The resources.PostProcess function only works within templates that produce HTML files.
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:
{{ $css := resources.Get "css/main.css" }}
{{ $css = $css | css.PostCSS | minify | fingerprint | resources.PostProcess }}
{{ $css.RelPermalink | strings.ToUpper }}