Open xlearns opened 2 years ago
module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, }
autoprefixer 会获取我们在 Postcss 中配置的需要支持的浏览器版本。我们的 css 文件里的内容已经被 Postcss 转换成 AST ,相应的就可以拿到属性名和属性值。在 autoprefixer 里面有很多属性的 hook ,当 AST 中有hooks 中的属性时,就会进到该属性的处理中,就会进行上面代码展示的进行属性名和属性值的转换,比如为该属性加上浏览器前缀。(应对不同浏览器厂商所支持的不同,只能进行很多的特判来进行兼容,可见 css 历史包袱有多重)。
class DisplayFlex extends Value { constructor (name, prefixes) { super(name, prefixes) if (name === 'display-flex') { this.name = 'flex' } } ... prefixed (prefix) { let spec, value ;[spec, prefix] = flexSpec(prefix) // spec 为年份版本 prefix为浏览器前缀 if (spec === 2009) { if (this.name === 'flex') { value = 'box' } else { value = 'inline-box' } } else if (spec === 2012) { if (this.name === 'flex') { value = 'flexbox' } else { value = 'inline-flexbox' } } else if (spec === 'final') { value = this.name } return prefix + value } ... }
Postcss 是一个使用js插件来转换样式的工具,Postcss 的插件会检查你的css。【Postcss 是一个插件工具】
j基于Postcss得插件
Postcss原理
autoprefixer 原理
autoprefixer 会获取我们在 Postcss 中配置的需要支持的浏览器版本。我们的 css 文件里的内容已经被 Postcss 转换成 AST ,相应的就可以拿到属性名和属性值。在 autoprefixer 里面有很多属性的 hook ,当 AST 中有hooks 中的属性时,就会进到该属性的处理中,就会进行上面代码展示的进行属性名和属性值的转换,比如为该属性加上浏览器前缀。(应对不同浏览器厂商所支持的不同,只能进行很多的特判来进行兼容,可见 css 历史包袱有多重)。