mirror of
https://github.com/gohugoio/hugo.git
synced 2026-09-03 12:12:37 +00:00
a6e635ca7d
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
130 lines
3.9 KiB
Markdown
130 lines
3.9 KiB
Markdown
---
|
|
title: css.PostCSS
|
|
description: Processes the given resource with PostCSS using any PostCSS plugin.
|
|
categories: []
|
|
keywords: []
|
|
action:
|
|
aliases: [postCSS]
|
|
related:
|
|
- functions/css/Sass
|
|
- functions/css/TailwindCSS
|
|
returnType: resource.Resource
|
|
signatures: ['css.PostCSS [OPTIONS] RESOURCE']
|
|
toc: true
|
|
---
|
|
|
|
{{< new-in 0.128.0 >}}
|
|
|
|
```go-html-template
|
|
{{ with resources.Get "css/main.css" | postCSS }}
|
|
<link rel="stylesheet" href="{{ .RelPermalink }}">
|
|
{{ end }}
|
|
```
|
|
|
|
## Setup
|
|
|
|
Follow the steps below to transform CSS using any of the available [PostCSS plugins].
|
|
|
|
Step 1
|
|
: Install [Node.js].
|
|
|
|
Step 2
|
|
: Install the required Node.js packages in the root of your project. For example, to add vendor prefixes to your CSS rules:
|
|
|
|
```sh
|
|
npm i -D postcss postcss-cli autoprefixer
|
|
```
|
|
|
|
Step 3
|
|
: Create a PostCSS configuration file in the root of your project. You must name this file `postcss.config.js` or another [supported file name]. For example:
|
|
|
|
```js
|
|
module.exports = {
|
|
plugins: [
|
|
require('autoprefixer')
|
|
]
|
|
};
|
|
```
|
|
|
|
{{% note %}}
|
|
{{% include "functions/resources/_common/postcss-windows-warning.md" %}}
|
|
{{% /note %}}
|
|
|
|
Step 4
|
|
: Place your CSS file within the `assets/css` directory.
|
|
|
|
Step 5
|
|
: Process the resource with PostCSS:
|
|
|
|
```go-html-template
|
|
{{ with resources.Get "css/main.css" | postCSS }}
|
|
<link rel="stylesheet" href="{{ .RelPermalink }}">
|
|
{{ end }}
|
|
```
|
|
|
|
## Options
|
|
|
|
The `css.PostCSS` method takes an optional map of options.
|
|
|
|
config
|
|
: (`string`) The directory that contains the PostCSS configuration file. Default is the root of the project directory.
|
|
|
|
noMap
|
|
: (`bool`) Default is `false`. If `true`, disables inline sourcemaps.
|
|
|
|
inlineImports
|
|
: (`bool`) Default is `false`. Enable inlining of @import statements. It does so recursively, but will only import a file once. URL imports (e.g. `@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');`) and imports with media queries will be ignored. Note that this import routine does not care about the CSS spec, so you can have @import anywhere in the file. Hugo will look for imports relative to the module mount and will respect theme overrides.
|
|
|
|
skipInlineImportsNotFound
|
|
: (`bool`) Default is `false`. Before Hugo 0.99.0 when `inlineImports` was enabled and we failed to resolve an import, we logged it as a warning. We now fail the build. If you have regular CSS imports in your CSS that you want to preserve, you can either use imports with URL or media queries (Hugo does not try to resolve those) or set `skipInlineImportsNotFound` to true.
|
|
|
|
```go-html-template
|
|
{{ $opts := dict "config" "config-directory" "noMap" true }}
|
|
{{ with resources.Get "css/main.css" | postCSS $opts }}
|
|
<link rel="stylesheet" href="{{ .RelPermalink }}">
|
|
{{ end }}
|
|
```
|
|
|
|
## No configuration file
|
|
|
|
To avoid using a PostCSS configuration file, you can specify a minimal configuration using the options map.
|
|
|
|
use
|
|
: (`string`) A space-delimited list of PostCSS plugins to use.
|
|
|
|
parser
|
|
: (`string`) A custom PostCSS parser.
|
|
|
|
stringifier
|
|
: (`string`) A custom PostCSS stringifier.
|
|
|
|
syntax
|
|
: (`string`) Custom postcss syntax.
|
|
|
|
```go-html-template
|
|
{{ $opts := dict "use" "autoprefixer postcss-color-alpha" }}
|
|
{{ with resources.Get "css/main.css" | postCSS $opts }}
|
|
<link rel="stylesheet" href="{{ .RelPermalink }}">
|
|
{{ end }}
|
|
```
|
|
|
|
## Check environment
|
|
|
|
The current Hugo environment name (set by `--environment` or in configuration or OS environment) is available in the Node context, which allows constructs like this:
|
|
|
|
```js
|
|
const autoprefixer = require('autoprefixer');
|
|
const purgecss = require('@fullhuman/postcss-purgecss');
|
|
module.exports = {
|
|
plugins: [
|
|
autoprefixer,
|
|
process.env.HUGO_ENVIRONMENT !== 'development' ? purgecss : null
|
|
]
|
|
}
|
|
```
|
|
|
|
[node.js]: https://nodejs.org/en/download
|
|
[postcss plugins]: https://www.postcss.parts/
|
|
[supported file name]: https://github.com/postcss/postcss-load-config#usage
|
|
[transpile to CSS]: /functions/css/sass/
|