Debian Codegraph @

docs/content/en/functions/css/PostCSS.md

raw
1 ---
2 title: css.PostCSS
3 description: Process CSS resources using PostCSS.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [postCSS]
9 returnType: resource.Resource
10 signatures: ['css.PostCSS [OPTIONS] RESOURCE']
11 aliases: [/functions/resources/postcss/]
12 ---
13
14 The `css.PostCSS` function transforms CSS using [PostCSS][] and any of its [plugins][].
15
16 ## Setup
17
18 Step 1
19 : Install [Node.js][].
20
21 Step 2
22 : Install the required Node packages in the root of your project. For example, to install PostCSS, its command-line interface, and the plugin to automatically add vendor prefixes to your CSS:
23
24 ```sh
25 npm install --save-dev postcss postcss-cli autoprefixer
26 ```
27
28 Step 3
29 : Create a PostCSS configuration file in the root of your project. Hugo exposes several [environment variables](#environment-variables) to the PostCSS process, including the current Hugo [environment](g) name. For example, in this configuration, running `hugo server` disables vendor prefixes but enables an inline sourcemap. Conversely, when building for production, it applies vendor prefixes and disables the sourcemap:
30
31 ```js {file="postcss.config.mjs" copy=true}
32 import autoprefixer from 'autoprefixer';
33
34 const isDev = process.env.HUGO_ENVIRONMENT === 'development';
35
36 export default {
37 plugins: [
38 !isDev ? autoprefixer : null
39 ],
40 map: isDev ? { inline: true } : false
41 };
42 ```
43
44 Step 4
45 : Place your CSS file within the `assets/css` directory.
46
47 Step 5
48 : Create a _partial_ template to process the CSS:
49
50 ```go-html-template {file="layouts/_partials/css.html" copy=true}
51 {{ with resources.Get "css/main.css" }}
52 {{ $opts := dict
53 "inlineImports" true
54 }}
55 {{ with . | css.PostCSS $opts }}
56 {{ if hugo.IsDevelopment }}
57 <link rel="stylesheet" href="{{ .RelPermalink }}">
58 {{ else }}
59 {{ with . | minify | fingerprint }}
60 <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
61 {{ end }}
62 {{ end }}
63 {{ end }}
64 {{ end }}
65 ```
66
67 Step 6
68 : Call the _partial_ template from your _base_ template:
69
70 ```go-html-template {file="layouts/baseof.html" copy=true}
71 <head>
72 {{ partial "css.html" . }}
73 </head>
74 ```
75
76 ## Options
77
78 The `css.PostCSS` function accepts an options map.
79
80 The following options apply when using a PostCSS configuration file:
81
82 `config`
83 : (`string`) The path to the directory that contains the PostCSS configuration file. By default, Hugo searches the root of the project directory and any modules for `postcss.config.js`, `postcss.config.mjs`, and `postcss.config.cjs` in that order. Use this option only if your configuration file is located in a custom subdirectory.
84
85 `importContext`
86 : {{< new-in 0.165.0 />}}
87 : (`resource.ResourceGetter`) A [resource getter](g) to use when resolving `@import` statements. Hugo searches this context first, matching by the path as written in the statement, before falling back to the file system. Applicable when [`inlineImports`](#inlineimports) is `true`.
88
89 `inlineImports`
90 : (`bool`) Whether to enable inlining of import statements. It does so recursively, but will only import a file once. Hugo looks for imports relative to the module mount and respects theme overrides. Default is `false`.
91
92 Note that Hugo's internal import routine does not adhere to the CSS specification; you can place `@import` statements anywhere in the file. However, external URL imports and imports with media queries are ignored during the inlining process.
93
94 The following snippet illustrates an external URL import that Hugo will ignore:
95
96 ```css
97 @import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');
98 ```
99
100 `skipInlineImportsNotFound`
101 : (`bool`) Whether to allow the build process to continue despite unresolved import statements, preserving the original import declarations. Set this option to `true` if you want to retain standard CSS imports unparsed. Default is `false`.
102
103 The following options apply when configuring PostCSS without a configuration file:
104
105 `noMap`
106 : (`bool`) Whether to disable the default inline source maps. Default is `false`.
107
108 `parser`
109 : (`string`) A custom PostCSS parser.
110
111 `stringifier`
112 : (`string`) A custom PostCSS stringifier.
113
114 `syntax`
115 : (`string`) Custom PostCSS syntax.
116
117 `use`
118 : (`string`) A space-delimited list of PostCSS [plugins][] to use.
119
120 For example, to pass your plugins and disable source maps directly through the options map instead of a configuration file:
121
122 ```go-html-template {file="layouts/_partials/css.html" copy=true}
123 {{ with resources.Get "css/main.css" }}
124 {{ $opts := dict
125 "noMap" true
126 "use" "autoprefixer postcss-color-alpha"
127 }}
128 {{ with . | css.PostCSS $opts }}
129 {{ if hugo.IsDevelopment }}
130 <link rel="stylesheet" href="{{ .RelPermalink }}">
131 {{ else }}
132 {{ with . | fingerprint }}
133 <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
134 {{ end }}
135 {{ end }}
136 {{ end }}
137 {{ end }}
138 ```
139
140 ## Environment variables
141
142 Hugo passes the following environment variables to the PostCSS process, making them available within your PostCSS configuration file.
143
144 `PWD`
145 : The absolute path to the project working directory.
146
147 `HUGO_ENVIRONMENT`
148 : The current Hugo environment, set with the `--environment` command line flag.
149 Default is `production` for `hugo build` and `development` for `hugo server`.
150
151 `HUGO_PUBLISHDIR`
152 : The absolute path to the publish directory, typically `public`. This value points to a directory on disk, even when rendering to memory with the `--renderToMemory` command line flag.
153
154 `HUGO_FILE_FILENAME`
155 : Hugo automatically mounts the following files from your project's root directory under `assets/_jsconfig`:
156
157 - `babel.config.js`, `babel.config.mjs`, `babel.config.cjs`
158 - `postcss.config.js`, `postcss.config.mjs`, `postcss.config.cjs`
159 - `tailwind.config.js`, `tailwind.config.mjs`, `tailwind.config.cjs`
160
161 For each file, Hugo creates a corresponding environment variable named `HUGO_FILE_FILENAME`, where `FILENAME` is the uppercase version of the filename with periods replaced by underscores. This allows you to access these files within your JavaScript, for example:
162
163 ```js
164 let tailwindConfig = process.env.HUGO_FILE_TAILWIND_CONFIG_JS || './tailwind.config.js';
165 ```
166
167 [Node.js]: https://nodejs.org/en
168 [PostCSS]: https://postcss.org/
169 [plugins]: https://postcss.org/docs/postcss-plugins