mirror of
https://github.com/gohugoio/hugo.git
synced 2026-09-03 04:02:38 +00:00
a024bc7d76
d1a251933theme: Optimize for printingd906488d5theme: Add border to highlighted code blocks within list items9630c38cbtheme: Display short command on commands list7f81dd997content: Regen commands markupa84008e99content: Cleanup primary list page0874b03e9content: Publish QR code examples to the images/qr directoryef1ce3498Restore static/img489a68458content: Restore KeyCDN screen captures7d6e6184btheme: Move search button in navbar to the right on smaller screensb03b96082theme: Misc style adjustmentsecad97821theme: Misc fixescf5e07b0fcontent: Restore example data and imagesc7265041fcontent: Reformat css.TailwindCSS setup steps3226e668ftheme: Make the search button more compact on mobile1f6614ee8content: Update css.TailwindCSS example49a2e7d71theme: Improve search output7e6b81ffamisc: Create issue template826740223theme: Misc search improvements75f32b2f6theme: Remove the search alertfd3de4ac2config: Enable link render hook warnings769e387cdAdjust logic to mark current section for Algoliaba73ce646Adjust markup to make it easier to match in Algolia5accec5f1Misc adjustmentseb5842566Some content adjustmentsbdf97b7b4Add new themeb19d68ee5Remove old themea04e96e55Rewrite the css.TailwindCSS page1c46f1864Update deprecation notesaedcb444cFix typoc3290b876Changes related to release of v0.144.0a0012fccenetlify: Hugo 0.143.19e4d73e87Include sections in quick-reference page collection4591058f5Miscellaneous edits612b8528fglossary: More edits1f0c54e60Fit typo6f14084c1Move glossary to quick reference section2d94905beImprove branch bundle admonition2ea56bef3Fix typos found with codespell71c5fe951Close shortcodee95d06592Document the responseHeaders option for resources.GetRemoteda7c12aaaDeprecate gist shortcodec2d3e2c25netlify: Bump to 0.143.0367d3a7abImprove new-in shortcode1a7413a16Clean up shortcode documentationd847892aaImprove glossary6a7fd42ffUpdate pages describing Store methods and functionsfa7643d1bRevert "Improve link render hook performance"fe4c86ec4Reformat MathJax examplecea44922aUpdate MathJax example to use safe mode0b6d0292cUpdate KaTeX example to use latest versionfd4508645Improve link render hook performance23aeb5bd0Update purgecss examplef7ef83e56Refer each Hugo Pipe page to the corresponding function81b91ef4eRemove related pages from pages for deprecated functions and methods53c7627d5Remove body from deprecated functions and methodsd9cf034c8Consolidate css.ToCSS informationb7ed108f9Improve js.Build example and presentation of options86a4a5088Improve babel example and description of optionsd1874c5f5Consistently use "edition" when referring to standard, extended, etc.92f03a350Revise description of privacy settings152a92d80Update Last.md82a2365bdAdd print-only QR code exampled22cadc25Update mathematics.mdb8160af03Delete page self-reference in page/type.mdce24fe4e0Update param.md51cb92180Improve description of output format template selection78bcf358eUpdate Tailwind CSS npm package version to 4.02ca9da4afInclude winget command to uninstall git-subtree-dir: docs git-subtree-split:d1a2519330
127 lines
3.6 KiB
Markdown
127 lines
3.6 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].
|
|
|
|
[postcss plugins]: https://postcss.org/docs/postcss-plugins
|
|
|
|
Step 1
|
|
: Install [Node.js].
|
|
|
|
[node.js]: https://nodejs.org/en/download
|
|
|
|
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.
|
|
|
|
{{< code file=postcss.config.js >}}
|
|
module.exports = {
|
|
plugins: [
|
|
require('autoprefixer')
|
|
]
|
|
};
|
|
{{< /code >}}
|
|
|
|
{{% 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');
|
|
module.exports = {
|
|
plugins: [
|
|
process.env.HUGO_ENVIRONMENT !== 'development' ? autoprefixer : null
|
|
]
|
|
}
|
|
```
|