【笔记】postcss部分插件小记
Apr 16, 2019笔记工具csspostcss部分插件小记
现有postcss插件列表:
1.autoprefixer自动补充前缀
- 功能:补全css属性前缀,如transform -> -webkit-transform
1.1 支持浏览器配置
支持浏览器列表:browsers,文档
我的移动端配置:1
2
3'last 2 versions',
'android 4',
'ios 8'
PC端配置:1
['ie 6-8', 'Firefox > 20', 'cover 99.5%']
1.2 webpack配置
安装node依赖包postcss-loader,autoprefixer1
npm i -D postcss-loader autoprefixer
webpack.config.js配置如下1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25// webpack.config.js
module.exports = {
// css
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: [
require('autoprefixer')({
browsers: [
'last 2 versions',
'android 4',
'ios 8'
]
})
]
}
}
]
},
}
1.3 vue-cli3配置
vue-cli默认安装autoprefixer,配置项目根目录package.json文件中的browserslist字段。其他配置可见vue-cli3配置文档
如1
2
3
4
5"browserslist": [
"last 2 versions",
"android 4",
"ios 8"
]
1.4 create-react-app配置
同上vue-cli3配置,package.json文件中的browserslist字段
2.postcss-plugin-px2rem px转rem
- 功能:取消用编辑器将px转rem
2.1 配置参数
1 | { |
2.2 安装依赖
安装node依赖包postcss-loader,postcss-plugin-px2rem1
npm i -D postcss-loader postcss-plugin-px2rem
2.3 webpack配置
webpack.config.js配置如下1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22// webpack.config.js
module.exports = {
// css
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: [
require('postcss-plugin-px2rem')({
rootValue: 75,
unitPrecision: 4
})
]
}
}
]
},
}
2.4 vue-cli3配置
修改vue.config.js配置文件,如1
2
3
4
5
6
7
8
9
10
11
12
13
14module.exports = {
css: {
loaderOptions: {
postcss: {
plugins: [
require('postcss-plugin-px2rem')({
rootValue: 75,
unitPrecision: 4
}),
]
}
}
},
};
2.5 create-react-app配置
找到config目录下webpack.config.js(或webpack.config.dev.js),找到postcss-loader配置位置(搜索postcss-loader),添加px2rem配置插件,如1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27//...
{
// Options for PostCSS as we reference these options twice
// Adds vendor prefixing based on your specified browser support in
// package.json
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebook/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
}),
require('postcss-plugin-px2rem')({
rootValue: 75,
unitPrecision: 4
})
],
sourceMap: isEnvProduction && shouldUseSourceMap,
},
}
//...
3.postcss-px-to-viewport px转vw
- 功能:取消用编辑器将px转vw
3.1 配置参数
1 | { |
3.2 安装依赖
1 | npm i -D postcss-px-to-viewport |
webpack/vue/react配置方式同上px2rem的配置方式(2.3~2.5),在此不做介绍
Author
My name is Micheal Wayne and this is my blog.
I am a front-end software engineer.
Contact: michealwayne@163.com