934d217332e633af97c9bc0e61dca3b329b90f4304a41c21b0b3198b6bedbf443d220e2e51433c60b17272324b060f9afabec1f3bf161c1244ea900116fe8f 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. # PostCSS [![Gitter][chat-img]][chat]
  2. <img align="right" width="95" height="95"
  3. alt="哲学家的石头 - PostCSS 的 logo"
  4. src="http://postcss.github.io/postcss/logo.svg">
  5. [chat-img]: https://img.shields.io/badge/Gitter-Join_the_PostCSS_chat-brightgreen.svg
  6. [chat]: https://gitter.im/postcss/postcss
  7. PostCSS 是一个允许使用 JS 插件转换样式的工具。
  8. 这些插件可以检查(lint)你的 CSS,支持 CSS Variables 和 Mixins,
  9. 编译尚未被浏览器广泛支持的先进的 CSS 语法,内联图片,以及其它很多优秀的功能。
  10. PostCSS 在工业界被广泛地应用,其中不乏很多有名的行业领导者,如:维基百科,Twitter,阿里巴巴,
  11. JetBrains。PostCSS 的 [Autoprefixer] 插件是最流行的 CSS 处理工具之一。
  12. PostCSS 接收一个 CSS 文件并提供了一个 API 来分析、修改它的规则(通过把 CSS 规则转换成一个[抽象语法树]的方式)。在这之后,这个 API 便可被许多[插件]利用来做有用的事情,比如寻错或自动添加 CSS vendor 前缀。
  13. **Twitter 账号:** [@postcss](https://twitter.com/postcss)<br>
  14. **支持 / 讨论:** [Gitter](https://gitter.im/postcss/postcss)<br>
  15. 如果需要 PostCSS 商业支持(如咨询,提升公司的前端文化,
  16. PostCSS 插件),请联系 [Evil Martians](https://evilmartians.com/?utm_source=postcss)
  17. 邮箱 <surrender@evilmartians.com>。
  18. [抽象语法树]: https://zh.wikipedia.org/wiki/%E6%8A%BD%E8%B1%A1%E8%AA%9E%E6%B3%95%E6%A8%B9
  19. [Autoprefixer]: https://github.com/postcss/autoprefixer
  20. [插件]: https://github.com/postcss/postcss/blob/master/README-cn.md#%E6%8F%92%E4%BB%B6
  21. <a href="https://evilmartians.com/?utm_source=postcss">
  22. <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
  23. alt="由 Evil Martians 赞助" width="236" height="54">
  24. </a>
  25. ## 插件
  26. 截止到目前,PostCSS 有 200 多个插件。你可以在 [插件列表] 或 [搜索目录] 找到它们。
  27. 下方的列表是我们最喜欢的插件 - 它们很好地演示了我们可以用 PostCSS 做些什么。
  28. 如果你有任何新的想法,[开发 PostCSS 插件] 非常简单易上手。
  29. [搜索目录]: http://postcss.parts
  30. [插件列表]: https://github.com/postcss/postcss/blob/master/docs/plugins.md
  31. ### 解决全局 CSS 的问题
  32. * [`postcss-use`] 允许你在 CSS 里明确地设置 PostCSS 插件,并且只在当前文件执行它们。
  33. * [`postcss-modules`] 和 [`react-css-modules`] 可以自动以组件为单位隔绝 CSS 选择器。
  34. * [`postcss-autoreset`] 是全局样式重置的又一个选择,它更适用于分离的组件。
  35. * [`postcss-initial`] 添加了 `all: initial` 的支持,重置了所有继承的样式。
  36. * [`cq-prolyfill`] 添加了容器查询的支持,允许添加响应于父元素宽度的样式.
  37. ### 提前使用先进的 CSS 特性
  38. * [`autoprefixer`] 添加了 vendor 浏览器前缀,它使用 Can I Use 上面的数据。
  39. * [`postcss-preset-env`] 允许你使用未来的 CSS 特性。
  40. ### 更佳的 CSS 可读性
  41. * [`precss`] 囊括了许多插件来支持类似 Sass 的特性,比如 CSS 变量,套嵌,mixins 等。
  42. * [`postcss-sorting`] 给规则的内容以及@规则排序。
  43. * [`postcss-utilities`] 囊括了最常用的简写方式和书写帮助。
  44. * [`short`] 添加并拓展了大量的缩写属性。
  45. ### 图片和字体
  46. * [`postcss-assets`] 可以插入图片尺寸和内联文件。
  47. * [`postcss-sprites`] 能生成雪碧图。
  48. * [`font-magician`] 生成所有在 CSS 里需要的 `@font-face` 规则。
  49. * [`postcss-inline-svg`] 允许你内联 SVG 并定制它的样式。
  50. * [`postcss-write-svg`] 允许你在 CSS 里写简单的 SVG。
  51. ### 提示器(Linters)
  52. * [`stylelint`] 是一个模块化的样式提示器。
  53. * [`stylefmt`] 是一个能根据 `stylelint` 规则自动优化 CSS 格式的工具。
  54. * [`doiuse`] 提示 CSS 的浏览器支持性,使用的数据来自于 Can I Use。
  55. * [`colorguard`] 帮助你保持一个始终如一的调色板。
  56. ### 其它
  57. * [`postcss-rtl`] 在单个 CSS 文件里组合了两个方向(左到右,右到左)的样式。
  58. * [`cssnano`] 是一个模块化的 CSS 压缩器。
  59. * [`lost`] 是一个功能强大的 `calc()` 栅格系统。
  60. * [`rtlcss`] 镜像翻转 CSS 样式,适用于 right-to-left 的应用场景。
  61. [`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
  62. [`postcss-preset-env`]: https://github.com/jonathantneal/postcss-preset-env
  63. [`react-css-modules`]: https://github.com/gajus/react-css-modules
  64. [`postcss-autoreset`]: https://github.com/maximkoretskiy/postcss-autoreset
  65. [`postcss-write-svg`]: https://github.com/jonathantneal/postcss-write-svg
  66. [`postcss-utilities`]: https://github.com/ismamz/postcss-utilities
  67. [`postcss-initial`]: https://github.com/maximkoretskiy/postcss-initial
  68. [`postcss-sprites`]: https://github.com/2createStudio/postcss-sprites
  69. [`postcss-modules`]: https://github.com/outpunk/postcss-modules
  70. [`postcss-sorting`]: https://github.com/hudochenkov/postcss-sorting
  71. [`postcss-assets`]: https://github.com/assetsjs/postcss-assets
  72. [开发 PostCSS 插件]: https://github.com/postcss/postcss/blob/master/docs/writing-a-plugin.md
  73. [`font-magician`]: https://github.com/jonathantneal/postcss-font-magician
  74. [`autoprefixer`]: https://github.com/postcss/autoprefixer
  75. [`cq-prolyfill`]: https://github.com/ausi/cq-prolyfill
  76. [`postcss-rtl`]: https://github.com/vkalinichev/postcss-rtl
  77. [`postcss-use`]: https://github.com/postcss/postcss-use
  78. [`css-modules`]: https://github.com/css-modules/css-modules
  79. [`colorguard`]: https://github.com/SlexAxton/css-colorguard
  80. [`stylelint`]: https://github.com/stylelint/stylelint
  81. [`stylefmt`]: https://github.com/morishitter/stylefmt
  82. [`cssnano`]: http://cssnano.co
  83. [`precss`]: https://github.com/jonathantneal/precss
  84. [`doiuse`]: https://github.com/anandthakker/doiuse
  85. [`rtlcss`]: https://github.com/MohammadYounes/rtlcss
  86. [`short`]: https://github.com/jonathantneal/postcss-short
  87. [`lost`]: https://github.com/peterramsing/lost
  88. ## 语法
  89. PostCSS 可以转化样式到任意语法,不仅仅是 CSS。
  90. 如果还没有支持你最喜欢的语法,你可以编写一个解释器以及(或者)一个 stringifier 来拓展 PostCSS。
  91. * [`sugarss`] 是一个以缩进为基础的语法,类似于 Sass 和 Stylus。
  92. * [`postcss-syntax`] 通过文件扩展名自动切换语法。
  93. * [`postcss-html`] 解析类 HTML 文件里`<style>`标签中的样式。
  94. * [`postcss-markdown`] 解析 Markdown 文件里代码块中的样式。
  95. * [`postcss-jsx`] 解析源文件里模板或对象字面量中的CSS。
  96. * [`postcss-styled`] 解析源文件里模板字面量中的CSS。
  97. * [`postcss-scss`] 允许你使用 SCSS *(但并没有将 SCSS 编译到 CSS)*。
  98. * [`postcss-sass`] 允许你使用 Sass *(但并没有将 Sass 编译到 CSS)*。
  99. * [`postcss-less`] 允许你使用 Less *(但并没有将 LESS 编译到 CSS)*。
  100. * [`postcss-less-engine`] 允许你使用 Less *(并且使用真正的 Less.js 把 LESS 编译到 CSS)*。
  101. * [`postcss-js`] 允许你在 JS 里编写样式,或者转换成 React 的内联样式/Radium/JSS。
  102. * [`postcss-safe-parser`] 查找并修复 CSS 语法错误。
  103. * [`midas`] 将 CSS 字符串转化成高亮的 HTML。
  104. [`postcss-less-engine`]: https://github.com/Crunch/postcss-less
  105. [`postcss-safe-parser`]: https://github.com/postcss/postcss-safe-parser
  106. [`postcss-syntax`]: https://github.com/gucong3000/postcss-syntax
  107. [`postcss-html`]: https://github.com/gucong3000/postcss-html
  108. [`postcss-markdown`]: https://github.com/gucong3000/postcss-markdown
  109. [`postcss-jsx`]: https://github.com/gucong3000/postcss-jsx
  110. [`postcss-styled`]: https://github.com/gucong3000/postcss-styled
  111. [`postcss-scss`]: https://github.com/postcss/postcss-scss
  112. [`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
  113. [`postcss-less`]: https://github.com/webschik/postcss-less
  114. [`postcss-js`]: https://github.com/postcss/postcss-js
  115. [`sugarss`]: https://github.com/postcss/sugarss
  116. [`midas`]: https://github.com/ben-eb/midas
  117. ## 文章
  118. * [一些你对 PostCSS 可能产生的误解](http://julian.io/some-things-you-may-think-about-postcss-and-you-might-be-wrong)
  119. * [PostCSS 究竟是什么,是做什么的](http://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss)
  120. * [PostCSS 指南](http://webdesign.tutsplus.com/series/postcss-deep-dive--cms-889)
  121. 你可以在 [awesome-postcss](https://github.com/jjaderg/awesome-postcss) 列表里找到更多优秀的文章和视频。
  122. ## 书籍
  123. * Alex Libby, Packt 的 [网页设计之精通 PostCSS](https://www.packtpub.com/web-development/mastering-postcss-web-design) (2016年6月)
  124. ## 使用方法
  125. 你可以通过简单的两步便开始使用 PostCSS:
  126. 1. 在你的构建工具中查找并添加 PostCSS 拓展。
  127. 2. [选择插件]并将它们添加到你的 PostCSS 处理队列中。
  128. [选择插件]: http://postcss.parts
  129. ### Webpack
  130. 在 `webpack.config.js` 里使用 [`postcss-loader`] :
  131. ```js
  132. module.exports = {
  133. module: {
  134. rules: [
  135. {
  136. test: /\.css$/,
  137. exclude: /node_modules/,
  138. use: [
  139. {
  140. loader: 'style-loader',
  141. },
  142. {
  143. loader: 'css-loader',
  144. options: {
  145. importLoaders: 1,
  146. }
  147. },
  148. {
  149. loader: 'postcss-loader'
  150. }
  151. ]
  152. }
  153. ]
  154. }
  155. }
  156. ```
  157. 然后创建 `postcss.config.js`:
  158. ```js
  159. module.exports = {
  160. plugins: [
  161. require('precss'),
  162. require('autoprefixer')
  163. ]
  164. }
  165. ```
  166. [`postcss-loader`]: https://github.com/postcss/postcss-loader
  167. ### Gulp
  168. 使用 [`gulp-postcss`] 和 [`gulp-sourcemaps`].
  169. ```js
  170. gulp.task('css', function () {
  171. var postcss = require('gulp-postcss');
  172. var sourcemaps = require('gulp-sourcemaps');
  173. return gulp.src('src/**/*.css')
  174. .pipe( sourcemaps.init() )
  175. .pipe( postcss([ require('precss'), require('autoprefixer') ]) )
  176. .pipe( sourcemaps.write('.') )
  177. .pipe( gulp.dest('build/') );
  178. });
  179. ```
  180. [`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
  181. [`gulp-postcss`]: https://github.com/postcss/gulp-postcss
  182. ### npm run / CLI
  183. 如果需要在你的命令行界面或 npm 脚本里使用 PostCSS,你可以使用 [`postcss-cli`]。
  184. ```sh
  185. postcss --use autoprefixer -c options.json -o main.css css/*.css
  186. ```
  187. [`postcss-cli`]: https://github.com/postcss/postcss-cli
  188. ### 浏览器
  189. 如果你想编译浏览器里的 CSS 字符串(例如像 CodePen 一样的在线编辑器),
  190. 只需使用 [Browserify] 或 [webpack]。它们会把 PostCSS 和插件文件打包进一个独立文件。
  191. 如果想要在 React 内联样式/JSS/Radium/其它 [CSS-in-JS] 里使用 PostCSS,
  192. 你可以用 [`postcss-js`] 然后转换样式对象。
  193. ```js
  194. var postcss = require('postcss-js');
  195. var prefixer = postcss.sync([ require('autoprefixer') ]);
  196. prefixer({ display: 'flex' }); //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
  197. ```
  198. [`postcss-js`]: https://github.com/postcss/postcss-js
  199. [Browserify]: http://browserify.org/
  200. [CSS-in-JS]: https://github.com/MicheleBertoli/css-in-js
  201. [webpack]: https://webpack.github.io/
  202. ### 运行器
  203. * **Grunt**: [`grunt-postcss`](https://github.com/nDmitry/grunt-postcss)
  204. * **HTML**: [`posthtml-postcss`](https://github.com/posthtml/posthtml-postcss)
  205. * **Stylus**: [`poststylus`](https://github.com/seaneking/poststylus)
  206. * **Rollup**: [`rollup-plugin-postcss`](https://github.com/egoist/rollup-plugin-postcss)
  207. * **Brunch**: [`postcss-brunch`](https://github.com/brunch/postcss-brunch)
  208. * **Broccoli**: [`broccoli-postcss`](https://github.com/jeffjewiss/broccoli-postcss)
  209. * **Meteor**: [`postcss`](https://atmospherejs.com/juliancwirko/postcss)
  210. * **ENB**: [`enb-postcss`](https://github.com/awinogradov/enb-postcss)
  211. * **Taskr**: [`taskr-postcss`](https://github.com/lukeed/taskr/tree/master/packages/postcss)
  212. * **Start**: [`start-postcss`](https://github.com/start-runner/postcss)
  213. * **Connect/Express**: [`postcss-middleware`](https://github.com/jedmao/postcss-middleware)
  214. ### JS API
  215. 对于其它的应用环境,你可以使用 JS API:
  216. ```js
  217. const fs = require('fs');
  218. const postcss = require('postcss');
  219. const precss = require('precss');
  220. const autoprefixer = require('autoprefixer');
  221. fs.readFile('src/app.css', (err, css) => {
  222. postcss([precss, autoprefixer])
  223. .process(css, { from: 'src/app.css', to: 'dest/app.css' })
  224. .then(result => {
  225. fs.writeFile('dest/app.css', result.css);
  226. if ( result.map ) fs.writeFile('dest/app.css.map', result.map);
  227. });
  228. });
  229. ```
  230. 阅读 [PostCSS API 文档] 获取更多有关 JS API 的信息.
  231. 所有的 PostCSS 运行器应当通过 [PostCSS 运行器指南]。
  232. [PostCSS 运行器指南]: https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md
  233. [PostCSS API 文档]: http://api.postcss.org/postcss.html
  234. ### 配置选项
  235. 绝大多数 PostCSS 运行器接受两个参数:
  236. * 一个包含所需插件的数组
  237. * 一个配置选项的对象
  238. 常见的选项:
  239. * `syntax`: 一个提供了语法解释器和 stringifier 的对象。
  240. * `parser`: 一个特殊的语法解释器(例如 [SCSS])。
  241. * `stringifier`: 一个特殊的语法 output 生成器(例如 [Midas])。
  242. * `map`: [source map 选项].
  243. * `from`: input 文件名称(大多数运行器自动设置了这个)。
  244. * `to`: output 文件名称(大多数运行器自动设置了这个)。
  245. [source map 选项]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md
  246. [Midas]: https://github.com/ben-eb/midas
  247. [SCSS]: https://github.com/postcss/postcss-scss
  248. ### Atom
  249. * [`language-postcss`] 添加了 PostCSS 和 [SugarSS] 代码高亮。
  250. * [`source-preview-postcss`] 在一个独立窗口里实时预览生成的 CSS。
  251. [SugarSS]: https://github.com/postcss/sugarss
  252. ### Sublime Text
  253. * [`Syntax-highlighting-for-PostCSS`] 添加了 PostCSS 代码高亮。
  254. [`Syntax-highlighting-for-PostCSS`]: https://github.com/hudochenkov/Syntax-highlighting-for-PostCSS
  255. [`source-preview-postcss`]: https://atom.io/packages/source-preview-postcss
  256. [`language-postcss`]: https://atom.io/packages/language-postcss
  257. ### Vim
  258. * [`postcss.vim`] 添加了 PostCSS 代码高亮。
  259. [`postcss.vim`]: https://github.com/stephenway/postcss.vim
  260. ### WebStorm
  261. 自 WebStorm 2016.3 开始,[提供了] 内建的 PostCSS 支持。
  262. [提供了]: https://blog.jetbrains.com/webstorm/2016/08/webstorm-2016-3-early-access-preview/