hugo/content/en/hugo-pipes/introduction.md
Bjørn Erik Pedersen cb39847dee Squashed 'docs/' changes from a26d0e610..7297c1172
7297c1172 Add note about caching for Hugo Pipes.
c91be3403 minor markdown, capitalization and spelling fixes (#1183)
fd4a103bf Fix several 404 errors (#1162)
69378bc20 Update related.md
28c24e95f Add note on setting baseURL
7b1502c99 minor typo fix (#1180)
33abeb4fe Update related.md
4887563f6 Update js.md
ee5f1de2e Hugo 0.74.3
986ea0c8e releaser: Add release notes to /docs for release of 0.74.3
3299b44bd Fix Asciidoctor args
bcb950347 resources/js: Add option for setting bundle format
3f8324918 resources/js: Add es5 build target

git-subtree-dir: docs
git-subtree-split: 7297c1172754078511ac1c10ca0dfd4cab629506
2020-08-14 18:31:01 +02:00

2.2 KiB
Executable file

title description date publishdate lastmod categories keywords menu weight sections_weight draft aliases
Hugo Pipes Introduction Hugo Pipes is Hugo's asset processing set of functions. 2018-07-14 2018-07-14 2018-07-14
asset management
docs
parent weight
pipes 20
01 01 false
/assets/

Asset directory

Asset files must be stored in the asset directory. This is /assets by default, but can be configured via the configuration file's assetDir key.

From file to resource

In order to process an asset with Hugo Pipes, it must be retrieved as a resource using resources.Get, which takes one argument: the filepath of the file relative to the asset directory.

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

Asset publishing

Assets will only be published (to /public) if .Permalink or .RelPermalink is used.

Go Pipes

For improved readability, the Hugo Pipes examples of this documentation will be written using Go Pipes:

{{ $style := resources.Get "sass/main.scss" | resources.ToCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $style.Permalink }}">

Method aliases

Each Hugo Pipes resources transformation method uses a camelCased alias (toCSS for resources.ToCSS). Non-transformation methods deprived of such aliases are resources.Get, resources.FromString, resources.ExecuteAsTemplate and resources.Concat.

The example above can therefore also be written as follows:

{{ $style := resources.Get "sass/main.scss" | toCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $style.Permalink }}">

Caching

Hugo Pipes invocations are cached based on the entire pipe chain.

An example of a pipe chain is:

{{ $mainJs := resources.Get "js/main.js" | js.Build "main.js" | minify | fingerprint }}

The pipe chain is only invoked the first time it is encountered in a site build, and results are otherwise loaded from cache. As such, Hugo Pipes can be used in templates which are executed thousands or millions of times without negatively impacting the build performance.