Plugins, plugins…we can't escape them; by now, you will have seen that they are an essential part of developing within PostCSS. Its modular nature is built entirely around plugins, and is how we can streamline our processing through selective use of these plugins.
In this chapter, we'll cover the anatomy of a PostCSS plugin and take a look at some pre-built examples, before embarking on the construction of a simple plugin that we can then test and submit for inclusion in the PostCSS system.
We will cover a number of topics throughout this chapter, which will include the following:
Let's make a start…!
A question, how many times have you worked with plugins that by themselves don't actually achieve anything?
I'll bet the answer won't be a high figure—and no, I'm not including those plugins that claim to perform an operation, yet don't seem to work for some reason! The power of PostCSS lies not in the core system, but the plugins that we use to manipulate our CSS style sheets.
At the time of writing, there are more than 200 plugins available for use within PostCSS—these range from extending PostCSS (such as postcss-nested or postcss-mixins), to manipulating colors (such as postcss-color-hcl or postcss-rgba-hex), to plugins that cater for future CSS syntax (such as adding @extend support).
The full list is available at https://github.com/postcss/postcss/blob/master/docs/plugins.md, or via the searchable catalogue hosted at http://www.postcss.parts.
To date, we've used a fair number of plugins throughout our examples—we've configured them for use, but there is still an element of black box about them, where we don't always know how the insides work. It's time to change that. As a first step, let's briefly meet the toolset that helps make plugins possible, the PostCSS API.
Any plugin created for PostCSS will have been constructed using the API. The key to this API will be the Node and Container methods, which can be used to manipulate content once the postcss object has been initialized in the plugin. We will explore these in more detail throughout this chapter, but before doing so, it makes sense to explore the architecture of a PostCSS plugin first, so let's have a look and see what makes one tick.