Key Points
Many developers have adopted LESS, Stylus or the most popular Sass at present. Ideally, you should also post-process your CSS using Autoprefixer to add vendor prefixes when needed without using frameworks like mixin or Compass. Autoprefixer is a plugin for PostCSS; it is a tool for converting CSS into an object model that can be used to manipulate styles. There are many other available plugins that implement preprocessor-like features such as variables, mixins, and nesting. Ben Frain's recent article "Breaking Up with Sass: Not You, It's Me" puts forward an interesting proposition. Given that you can use PostCSS to create the CSS preprocessor you want, do you really need Sass, LESS or Stylus? A basic Sass-like parser can be created during your build process. In this example, I'll use a Gulp.js task file, but the same concept applies to Grunt or any other build system. First, we use npm to install PostCSS and the required plugins, for example:
npm install gulp-postcss postcss-mixins postcss-simple-vars postcss-nested autoprefixer-core --save-dev
Then create an array of CSS processing functions in gulpfile.js, for example:
var gulp = require('gulp'), postcss = require('gulp-postcss'), processors = [ require('postcss-mixins'), require('postcss-simple-vars'), require('postcss-nested'), require('autoprefixer-core')({ browsers: ['last 2 versions', '> 2%'] }) ];
and write a CSS processing task, for example:
// 编译CSS gulp.task('css', function() { return gulp.src('source/css/styles.css') .pipe(postcss(processors)) .pipe(gulp.dest('dest/styles/')); });
You can also use the PostCSS API to create your own processing functions. For example, we can apply a sans-serif fallback for all font-family declarations:
processors = [ require('postcss-mixins'), require('postcss-simple-vars'), require('postcss-nested'), function(css) { // sans-serif 后备 css.eachDecl('font-family', function(decl) { decl.value = decl.value + ', sans-serif'; }); }, require('autoprefixer-core')({ browsers: ['last 2 versions', '> 2%'] }) ];
If the file /source/css/styles.css contains the following code:
$colorfore: #333; $colorback: #fff; @define-mixin reset { padding: 0; margin: 0; } main { font-family: Arial; @mixin reset; color: $colorfore; background-color: $colorback; article { color: $colorback; background-color: $colorfore; } }
Running gulp css will create this CSS in /dest/css/styles.css:
main { font-family: Arial, sans-serif; padding: 0; margin: 0; color: #333; background-color: #fff; } main article { color: #fff; background-color: #333; }
Advantages
PostCSS allows you to get rid of the restrictions and choices imposed by preprocessor authors. This method provides several benefits:
Disadvantages
So everything is OK? Unfortunately, PostCSS is not the perfect solution:
Should you give up on your preprocessor?
If you start a small, relatively simple standalone project on a single team, a custom PostCSS processor can be an attractive option. I also recommend PostCSS for any real postprocessing tasks such as vendor prefix, packaging media queries into a single declaration, reducing calc() equations, backing up for old browser applications, supporting CSS4 selectors , shrink, etc. There is no benefit in doing these tasks yourself. However, Sass has reached critical mass. No preprocessor syntax is perfect, but it will be understood by most developers on your team. Any subtle difference is unlikely to provide significant benefits or appeal to everyone. That is, PostCSS and similar Rework frameworks have great potential. If a modular CSS plug-in system can replicate - or even mix - the syntax and functionality of Sass, LESS and Stylus we want, we will have a preprocessor that can beat all other preprocessors utensil. You can bet that someone is developing this project now...
Did you successfully use PostCSS as a preprocessor for your project? Does it attract you away from Sass? Will you migrate from LESS? Will you give up on Stylus?FAQs about PostCSS
What is the main difference between PostCSS and other CSS preprocessors?
How to install and use PostCSS?
. To use PostCSS, you need to install the plugin you want to use and then create a configuration file that specifies the plugin you want to use. Then, you can run PostCSS on your CSS file using the command npm install -g postcss-cli
. postcss input.css -o output.css
Can I use PostCSS with other CSS preprocessors?
PostCSS provides many benefits. It is highly customizable, allowing you to select only the plugins you want. This can lead to a smaller, faster build process. PostCSS also enables you to use future CSS features today, and it automatically adds vendor prefixes to your CSS, saving you time and ensuring your style works properly in different browsers.
While PostCSS is powerful and flexible, its learning curve may be steeper than other CSS preprocessors. Because it relies on plugins, you need to spend time researching and choosing the right plugin for your project. Also, because it is a newer tool, it may not be as widely supported or adopted as other preprocessors.
PostCSS handles mixin through the postcss-mixins plugin. This plugin allows you to define and use mixins in CSS, similar to what you do in Sass or Less. You can define a mixin in the PostCSS configuration file and use it in your CSS using the @mixin
keyword.
Yes, you can integrate PostCSS into your existing projects. You just need to install PostCSS and the plugin you want to use, and then set up a PostCSS configuration file. You can then run PostCSS on the existing CSS file.
Autoprefixer and cssnext are actually PostCSS plugins. This means they are tools that run on top of PostCSS, leveraging its plug-in architecture. Autoprefixer adds vendor prefix to your CSS, while cssnext allows you to use future CSS features today.
Yes, PostCSS is suitable for projects of any size. Because it is customizable, you can select only the plugins you want, enabling a smaller, faster build process. This makes it a good choice for large projects where performance is an issue.
The above is the detailed content of How to Build Your Own CSS Preprocessor With PostCSS. For more information, please follow other related articles on the PHP Chinese website!