前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊CSS postproccessors

聊聊CSS postproccessors

作者头像
IMWeb前端团队
发布2019-12-03 17:00:37
4240
发布2019-12-03 17:00:37
举报
文章被收录于专栏:IMWeb前端团队IMWeb前端团队

本文作者:IMWeb 杨文坚 原文出处:IMWeb社区 未经同意,禁止转载 阿里妈妈 @一丝 准备发布其CSSGrace,即CSS后处理插件,于是顺便聊聊CSS postprocessors。

从Rework说起

Rework是TJ大神开发的CSS预处理框架。但为什么会出现呢?TJ大神如此回答:

The simple answer is that Rework caters to a different audience, and provide you the flexibility to craft the preprocessor you want, not the choices the author(s) have force on you. Our goal with Rework contrasts the others, as it does not provide a higher level language within the CSS documents, Rework’s goal is to make those constructs unnecessary, being the most transparent CSS pre-processor out there.

简单地说,就是从之前的特定CSS预处理器,转而成为通用式CSS预处理框架,通过插件,可自定义扩展功能。

我用compass用得正爽,凭什么用你?

  • 工程师喜欢瞎折腾,满足其DIY乐趣
  • 现代前端,多端多屏、需要不同兼容场景下情况下,CSS预处理器需要深度定制,来看看我们没有深度定制的后果:
    1. 我们经常使用@include border-radius;,可你知道compass这个mixin有啥问题么?.btn-default { -webkit-border-radius: 2px } // 仅在 android 2.1, chrome 4, ios_saf 3.2, safari 4 或更早期版本适用 .btn-default { -moz-border-radius: 2px } // 仅在 firefox 3.6 以前版本适用 .btn-default { -ms-border-radius: 2px } // 根本不存在 -ms-border-radius .btn-default { -o-border-radius: 2px } // 这玩意早就淘汰了
代码语言:txt
复制
2. 我们也经常用`@include transition();`,但:.course-card .course-agency { -moz-transition: .3s } // 仅在 firefox 15 以前版本适用 .course-card .course-agency { -o-transition: .3s } // 仅在 opera 12 以前版本适用
  • 嵌套很强大,但某些时候也是灾难
    1. 多层嵌套,代码维护的灾难
代码语言:txt
复制
1. 多层嵌套导致的单页应用代码性能问题,所以Github的CSS规范明确指明Sass嵌套不允许多余三层(之前我们以为仅仅是维护性原因),有兴趣可以围观下 [GitHub's CSS Performance](https://speakerdeck.com/jonrohan/githubs-css-performance)

Autoprefixer革命

在我看来真正带来革命的不是postcss,恰恰是他的核心组件Autoprefixer。让我们看看他到底干了什么?1

Working with Autoprefixer is simple: just forget about vendor prefixes and write normal CSS according to the latest W3C specs. You don’t need a special language (like Sass) or remember, where you must use mixins. Just write normal CSS according to the latest W3C specs and Autoprefixer will produce the code for old browsers.

所以呢?如果我们写了:

代码语言:javascript
复制
a {
    display: flex;
}

则经过Autoprefixer,会变成:

代码语言:javascript
复制
a {
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -ms-flexbox;
    display: flex;
}

并且这些hack数据是从caniuse获取的,所以能根据你的需要设置你需要兼容的浏览器。Sounds good!这更像polyfill,我们只用按照标准写就好了,polyfill会帮忙处理兼容性,之后如果无需兼容,其会自动去除。

CSSGrace

Make it better!

CSSGrace在我看来主要由于AST的介入,其可能分析出以前preproccessors分析不出来的css错误问题,类似csslint的一些静态分析,以及一丝所说的CSS常见错误,例如:float: left/right 或者 position: absolute 后还写上 display: block,具体参见:http://www.zhihu.com/question/20979831

最后随想

个人感觉未来Web会Web Component化,无论是以W3C标准以HTML为核心的Web Component,还是类似React以Javascript为核心的Web Component,在纵向力度足够细的时候,css样式将趋近与足够简单。

在这种背景下,当处理好作用域的情况下(目前没什么好办法,可能只能将class name写长一点),未来嵌套场景将大大减少,从这一点来看,以前的Sass、LESS等如此强大的预处理器未必是必需品。

1 在我的记忆里Autoprefixer是rework的插件(因为当时Autoprefixer是rework的主打功能之一),所以我开挖掘机挖了下,发现的确如此,实际上Autoprefixer在1.0脱离了rework,独立出postcss,具体原因见下:

CSS parser was changed to PostCSS. It allows to add prefixes inside some new or custom at-rules and fix issue with comment in property name. Also it clean Autoprefixer code by moving some common CSS processor code to PostCSS.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-03-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 从Rework说起
  • 我用compass用得正爽,凭什么用你?
  • Autoprefixer革命
  • CSSGrace
  • 最后随想
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档