mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
e477373487
8390a4a3aAdd Void Linux installation instructionsd6099aae8Update link to PostCSS plugins25dad4693netlify: Hugo 0.139.42b1fa118cFix typo3ef1eb505Update hosting-on-aws-amplifyc0f6d35d6Fix typoaa54d4301Correct directory name98aa26bdbImprove instructions for hosting with AWS Amplifya07638a80Add new-in badges6ad018055netlify: Hugo 0.139.31050835d6Update title of hugo.Store pageebbd2e851Clarify the shortcode Ordinal methodb7716ed95Revise code block render hook for Mermaid diagramsf1da9b6eanetlify: Hugo 0.139.2d8ac9f428Downgrade the Go toolchain in go.mod to a slightly older Go version254b3c4f2netlify: Hugo 0.139.103e666038Add hugo.Store, site.Store and Shortcode.Store157e8983bUpdate Anchorize.md59fa9f214Document the PageRef menu entry methodbda544ccedocs(transform.Unmarshal): match lang attribute to title language in examples1985886bdAdjust front matter of shared Markdown snippetsda5bd70d1Fix typo431b65d6bUpdate themeb63ef69f5Update style guidanced50ed3422Remove old new-in badges12bfb9933Update docs.yaml0b936cacdnetlify: Hugo 0.139.0ab7668b4ddartsass: Add silenceDeprecations option154df9bfcMerge commit '838bd312b1a287bb33962ad478dbc54737654f35'efa80477cdocs: Regen CLI docsad99e4a7adocs: Regenerate CLI docs git-subtree-dir: docs git-subtree-split:8390a4a3ac
3.9 KiB
3.9 KiB
title, description, categories, keywords, action, toc
| title | description | categories | keywords | action | toc | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| css.PostCSS | Processes the given resource with PostCSS using any PostCSS plugin. |
|
true |
{{< new-in 0.128.0 >}}
{{ 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:
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.jsor another supported file name. For example:
module.exports = {
plugins: [
require('autoprefixer')
]
};
{{% note %}} {{% include "functions/resources/_common/postcss-windows-warning.md" %}} {{% /note %}}
- Step 4
- Place your CSS file within the
assets/cssdirectory. - Step 5
- Process the resource with PostCSS:
{{ 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 isfalse. Iftrue, disables inline sourcemaps. - inlineImports
- (
bool) Default isfalse. 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 isfalse. Before Hugo 0.99.0 wheninlineImportswas 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 setskipInlineImportsNotFoundto true.
{{ $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.
{{ $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:
const autoprefixer = require('autoprefixer');
const purgecss = require('@fullhuman/postcss-purgecss');
module.exports = {
plugins: [
autoprefixer,
process.env.HUGO_ENVIRONMENT !== 'development' ? purgecss : null
]
}