PostCSS 8 plugin migration: legacy plugins load but ordering and visitor behavior are unclear
0 reputation · 23 Sept 2020, 20:05 UTC
0 reputation · 23 Sept 2020, 20:05 UTC
We are moving a build pipeline from PostCSS 7 to PostCSS 8 (assume 8.4.x). PostCSS 8 replaced the postcss.plugin() wrapper with factory objects exposing a postcssPlugin name, and made postcss a peer dependency so plugins share one instance.
Our plugins array mixes older PostCSS 7-era packages with native PostCSS 8 plugins. The legacy ones still load through the compatibility layer, but it is unclear whether they participate in the same visitor-based event flow or run in a slower legacy mode that could interact badly with newer plugins. Separately, PostCSS does not resolve inter-plugin dependencies, so array order alone decides execution — and we have order-sensitive pairs (a property-rewriting transform plus autoprefixer) where the correct sequence is not obvious from either plugin's docs.
We also noticed that a misconfigured plugin can pass CSS through untransformed without failing the build, which makes it hard to tell whether the migration actually changed behavior or silently skipped transforms.
30775 reputation · 24 Sept 2020, 01:18 UTC
postcssPlugin string property (they may also expose process or defaults). Legacy PostCSS 7 plugins are plain functions (or functions wrapped by the compatibility layer) and lack that property.const plugin = require('some-postcss-plugin');
console.log(typeof plugin, plugin.postcssPlugin ? 'native' : 'legacy');
const postcss = require('postcss');
const input = 'a { color: red; }';
const output = await postcss([myPlugin]).process(input, { from: undefined });
if (input.trim() === output.css.trim()) {
console.warn('Plugin did not transform CSS');
}
Likely explanation: The compatibility layer treats legacy plugins as plain functions that receive the CSS once, while native plugins are instantiated as objects and participate in the visitor‑based event flow. This can cause subtle timing differences but does not change the fact that order is still dictated by the array.
Confirmed facts (from PostCSS 8 documentation and the research findings):
postcssPlugin; legacy plugins do not.plugin.postcssPlugin to see which are native.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.