In a postcss.config.js listing postcss-import, postcss-preset-env and cssnano, why does plugin order matter, and which order do you use?
answer
- the array is the run order
- one tree before anything else
- non-standard sugar before standard lowering
- Autoprefixer is already inside the preset
- the minifier goes last
basics
~20 sPlugins run in the order listed, each on the tree the previous ones left. Put postcss-import first so later plugins see one tree, non-standard plugins before postcss-preset-env (which already runs Autoprefixer), and cssnano last so it compresses everything the others emitted.
solid answer
~40 sPostCSS runs plugins in the order of the `plugins` array, and each one works on the tree its predecessors produced, so order decides what each plugin can see. postcss-import goes first: it inlines `@import`ed files, so every later plugin works on one tree instead of only the entry file. Plugins for non-standard syntax, including an in-house rewrite, go next, because postcss-preset-env only understands standard CSS. postcss-preset-env follows; it already contains Autoprefixer, so a separate `autoprefixer` entry should be removed. The minifier, cssnano, goes last so the fallbacks and prefixes added upstream are compressed too. cssnano's `default` preset makes only safe transforms; the `advanced` preset shortens identifiers and reduces `z-index` values, which is safe only if nothing outside the file depends on them.
code
javascript · 11 lines// postcss.config.js for the exhibition-guide site
module.exports = {
plugins: [
require('postcss-import')(), // 1. inline @import: one tree from here on
require('./postcss/gutter-units')(), // 2. non-standard in-house rewrite
require('postcss-preset-env')({ // 3. lower standard CSS, then Autoprefixer
stage: 2
}),
require('cssnano')() // 4. minify last (default preset)
]
}go deeper
Remember the shape of a working config: postcss-import first, cssnano last, and no separate Autoprefixer, because postcss-preset-env already includes it.
Explain why each position matters: what a plugin cannot see if it runs too early, and what a plugin added after the minifier leaves uncompressed.
Show how PostCSS 8 really runs the list (Once in order, one shared walk for node listeners, OnceExit after it) and choose a cssnano preset by naming the assumptions the advanced preset makes.
Own the pipeline as a contract: one config, one target list shared by prefixer and minifier, warnings that fail the build, and a review rule for every new plugin's position.
## Where the plugin list lives PostCSS reads its plugins from a **config file**: usually a `postcss.config.js` in the project root, or a `postcss` section in `package.json`, or the PostCSS options inside a build tool's own configuration. The important part is the `plugins` list, because **its order is the run order**. Each plugin sees the tree exactly as the plugins before it left it, and nothing that runs later is visible to it. ## The order that works 1. **postcss-import first.** It inlines every `@import`ed file into the tree. Its README says it should probably be the first plugin, so that "other plugins will work on the AST as if there were only a single file". Put it later and the plugins before it only ever see the entry file. It also imports each file only once per scope, and requires `@import` statements to precede everything else except `@charset` and `@layer`. 2. **Asset and non-standard plugins next.** postcss-url belongs after postcss-import so it can fix `url()` paths in the inlined files. Plugins for syntax that is not a CSS standard (Sass-style nesting with postcss-nested, or the site's own plugin that rewrites the legacy `--gutter-units` declaration) go before postcss-preset-env, whose README says it only handles standard features and asks for such plugins to be placed first. 3. **postcss-preset-env.** It lowers standard features for the Browserslist targets, then runs **Autoprefixer** after its polyfills, with the same targets. A separate `autoprefixer` entry is redundant work and a second set of options that can drift, so remove it. 4. **The minifier last.** cssnano compresses whatever it receives. Anything added after it (a fallback, a prefix, an inlined file) reaches the output uncompressed. 5. **Optionally, a fail-on-warning plugin at the very end**, such as `postcss-fail-on-warn`, to turn any plugin warning into a failed build. ## How PostCSS 8 actually executes the list "Runs in order" is accurate, with one nuance worth knowing at senior level: | Plugin shape | When it runs | |---|---| | a `Once` listener, or an old-style function plugin | one after another, in list order, before any tree walk | | node listeners (`Rule`, `Declaration`, `AtRule`, ...) | all plugins share **one** walk; on each node, listeners fire in list order | | `OnceExit` | after the walk, in list order | | a plugin pack such as postcss-preset-env | its inner plugins are spliced into the list at its position | Autoprefixer, for instance, does its work in `OnceExit`, after every node listener in the pipeline has finished. So the list order sets the order within each phase, and a plugin's choice of listeners decides which phase its work happens in. ## Choosing a cssnano preset - **`default`**: transforms that make no assumptions about your CSS beyond the file itself: removing comments and duplicates, compressing colours and values, merging rules and longhands, normalising whitespace and URLs. Bundled with cssnano, so it needs no configuration. - **`advanced`**: adds transforms that are "safe only when your site meets the requirements". It **reduces custom identifiers** such as keyframe names, **reduces `z-index` values**, and discards keyframes, fonts and counter styles it believes are unused. Each assumes this file is the only place those names and numbers are used. It also loads Autoprefixer with `add: false`, so it only removes prefixes. Some cssnano transforms also consult Browserslist, so the minifier and the prefixer should read the same targets. ## Common order mistakes - **Autoprefixer listed twice**, directly and inside postcss-preset-env. - **cssnano before postcss-preset-env**: the fallbacks and prefixes the preset adds are never minified. - **postcss-import in the middle**: rules from imported files skip the plugins listed before it. - **Sass-style nesting placed after postcss-preset-env**: the preset meets syntax it does not handle. The exhibition-guide site's config in the example follows this order: import, the in-house plugin, the preset, the minifier. ## Reviewing a config you inherit When a pipeline's output looks wrong, read the config top to bottom and ask three questions of each entry: 1. **What does this plugin need to see?** postcss-import needs the raw `@import` statements, a minifier needs the final CSS, and Autoprefixer needs the unprefixed standard properties. 2. **What does it add that a later plugin must process?** Inlined files must be lowered and prefixed, and fallbacks must be minified. 3. **Is it already inside another entry?** Plugin packs flatten into the list, so a pack can quietly contain a plugin that is also listed on its own. The answers almost always lead to the same layout: gather, rewrite non-standard syntax, lower and prefix the standard syntax, and compress.
- Why does a separate `autoprefixer` entry not belong next to postcss-preset-env?postcss-preset-env runs Autoprefixer itself, passing it the same Browserslist environment or `browsers` list, and its README says to remove a standalone copy. Two entries prefix the same tree twice and carry two option sets that can drift. Tune the inner one with the preset's `autoprefixer` option, or turn it off with `autoprefixer: false`.
- When is cssnano's advanced preset risky?It assumes the file is the whole story: it shortens custom identifiers such as keyframe names, reduces `z-index` values and drops keyframes, fonts and counter styles it thinks are unused. If JavaScript sets an animation name, or another stylesheet relies on a `z-index` or a keyframe defined here, the output breaks. Stay on `default` unless you can guarantee those requirements.
A packing line: parts are gathered first, then shaped, then boxed. Anything bolted on after the boxing station ships loose. A plugin added after the minifier ships uncompressed in the same way.
- PostCSS sorts plugins itself, so the order in the config is cosmetic.
- Keep autoprefixer listed beside postcss-preset-env to make sure prefixes are added.
- Run cssnano first so the other plugins have less CSS to process.
- postcss-import can sit anywhere because imports resolve before plugins run.
- cssnano's advanced preset is a safe drop-in for a smaller file.