PostCSS is a tool for transforming styles with JS plugins. These plugins can lint your CSS, support variables and mixins, transpile future CSS syntax, inline images, and more.
How does it work?
original.css -> Parser -> Plugins -> Stringifier -> output.css
Comparison with SASS
PostCSS can provide all the features that SASS has. There is even a package which does exactly that – it’s called PreCSS.
A nice thing about PostCSS is, however, that it can very well coexist with SASS. It can run after a SASS compilation, applying its transforms to the resulting CSS. This makes it very easy to integrate it even into SASS-heavy projects.
New projects can use just the PostCSS with plugins providing SASS-like features, like nesting or mixins. It allows us to replace SASS features found in new web standards, such as variables, and leave those available exclusively in SASS, such as nesting. This is beneficial, because we can use as much CSS and as little other languages/syntaxes as possible, which would make it easier for developers not familiar with SASS.
Does it require a lot of setup?
If it doesn’t have anything like that – sorry, you have to get one to use PostCSS.
Example config (using webpack):
// install postcss with autoprefixer
npm install --save-dev postcss autoprefixer
// add webpack loader
// define plugins (in webpack's module.exports)
Keeping our stack tight
Because it’s very easy to add new plugins, it’s also difficult to enforce a single stack through many projects. If stacks differ, new developers need to learn new syntaxes and features, which may prove troublesome in an environment with a high turnover. This is both the greatest strength and the greatest drawback of PostCSS if looking from a company’s perspective.
- Allows for the usage of the most current web standards
- Enables modular, more maintainable CSS
- Allows picking a setup catered to one’s needs
- Easy to write own plugins
- Easy to add to existing Webpack/Gulp/whatever setup
- Can work alongside SASS
- Faster compilation than SASS
- One more tool
- Doesn’t support Rails Asset Pipeline
- Requires a task runner/module bundler to work
- Harder to maintain across different projects
With great power comes great responsibility. PostCSS provides a set of extremely useful features that would make every CSS codebase more maintainable, and it can work alongside SASS or even replace it. Nonetheless, a company wanting to adopt it needs solid agreements or a centralized configuration to make the setup consistent across a bunch of different projects to ensure effortless onboarding of new developers.