Why We Use PostCSS at Netguru
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.
The new projects can use just the PostCSS with plugins providing SASS-like features, such as nesting or mixins. It allows us to replace SASS features found in new web standards, e.g. 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
As 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 the environment with a high turnover. This is both the greatest strength and drawback of PostCSS, 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 one's own plugins,
- easy to add to the 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 willing to adopt it needs solid agreements or a centralised configuration to make the setup consistent across a bunch of different projects and ensure effortless onboarding of new developers.
Links / Literature: