cb18a5183 Fix broken link 07a0198bf Config: Place Google Analytics tag ID under the services key 4bf0c719f Fix typo 50d8ad1af Fix muiltilingual menu definition instructions 1a32519a9 Fix typos 6f34ca8e0 Explain usage of front matter to target a template 5bd977257 Improve goldmark config docs 447632938 Remove Docker notes from installation instructions 84741d173 Update reference to hugo.work 0338d7c71 Fix menu template f5d2f5ed4 Fix typos in content/en/functions/fmt a3a40ff99 Add return type to functions 85ac3e779 Remove outdated feature image d47d889e4 Fix signatures 7551ba28f Document safe.JSStr function e77993be0 Document keyVals function 4dba20db3 Update theme babf91544 Update echoparam 8c8203efa Adjust related functions 4cb1b30fc Fix example ba95eca64 Improve showcase prose 5d3dcf366 Add Overmind Studios showcase 8d634ac70 Change code blocks from indented to fenced cfab978e6 Add missing code fences 407dd5c47 Limit related pages for functions to other functions 9fa67d981 Fix .Site.LastChange doc 393aa16d0 netlify: Hugo 0.119.0 f864af97a docs: Even more about images.Process 9d772d5f0 docs: More about images.Process bc655f869 docs: Regen docshelper 41c3536d1 Merge commit '9aec42c5452b3eb224888c50ba1c3f3b68a447e9' 918ed53f4 Add images.Process filter 573645883 Add $image.Process a1151b0fd Add images.Opacity filter git-subtree-dir: docs git-subtree-split: cb18a5183fc62f301ffde50b8c39f03e4b897aec
4.3 KiB
Executable file
title | description | categories | keywords | menu | toc | weight | signatures | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PostCSS | Process CSS files with PostCSS, using any of the available plugins. |
|
|
true | 40 |
|
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 CSS rules:
npm install 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 one of the other supported file names. For example:
{{< code file="postcss.config.js" >}} module.exports = { plugins: [ require('autoprefixer') ] }; {{< /code >}}
{{% note %}} If you are a Windows user, and the path to your project contains a space, you must place the PostCSS configuration within the package.json file. See this example and issue #7333. {{% /note %}}
- Step 4
- Place your CSS file within the
assets
directory. - Step 5
- Capture the CSS file as a resource and pipe it through
resources.PostCSS
(aliaspostCSS
):
{{< code file="layouts/partials/css.html" >}} {{ with resources.Get "css/main.css" | postCSS }}
{{ end }} {{< /code >}}If starting with a Sass file within the assets
directory:
{{< code file="layouts/partials/css.html" >}} {{ with resources.Get "sass/main.scss" | toCSS | postCSS }}
{{ end }} {{< /code >}}Options
The resources.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 {{< new-in "0.99.0" >}}
- (
bool
) Default isfalse
. Before Hugo 0.99.0 wheninlineImports
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 setskipInlineImportsNotFound
to true.
{{< code file="layouts/partials/css.html" >}} {{ $opts := dict "config" "config-directory" "noMap" true }} {{ with resources.Get "css/main.css" | postCSS $opts }}
{{ end }} {{< /code >}}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.
{{< code file="layouts/partials/css.html" >}} {{ $opts := dict "use" "autoprefixer postcss-color-alpha" }} {{ with resources.Get "css/main.css" | postCSS $opts }}
{{ end }} {{< /code >}}Check Hugo 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:
{{< code file="postcss.config.js" >}} module.exports = { plugins: [ require('autoprefixer'), ...process.env.HUGO_ENVIRONMENT === 'production' ? [purgecss] : [] ] } {{< /code >}}