postcss及其插件autoprefixer、postcss-preset-env、stylelint的使用

简介: postcss及其插件autoprefixer、postcss-preset-env、stylelint的使用

image.png

测试环境

node -v
v16.14.0

使用 postcss

安装


pnpm i -D postcss postcss-cli
package.json
{
  "type": "module",
  "devDependencies": {
    "postcss": "^8.4.16",
    "postcss-cli": "^10.0.0"
  }
}

运行命令

npx postcss style.css -o dist.css

输入 style.css


.box {
  box-sizing: border-box;
}

输出 dist.css

.box {
  box-sizing: border-box;
}
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsImZpbGUiOiJkaXN0LmNzcyIsInNvdXJjZXNDb250ZW50IjpbIi5ib3gge1xuICBib3gtc2l6aW5nOiBib3JkZXItYm94O1xufVxuIl19 */

使用插件 autoprefixer

安装

pnpm i -D autoprefixer
npx postcss style.css -o dist.css -u autoprefixer

运行完命令发现,并没有什么改变,原因是css语法已经兼容了默认指定浏览器的版本,并不需要额外的处理


调试模式

npx autoprefixer --info

设置要兼容的浏览器版本

package.json
{
  "browserslist": [
    "cover 99.5%"
  ]
}

不输出sourcemaps

npx postcss style.css -o dist.css -u autoprefixer --no-map

输出,可以看到已经增加了浏览器前缀


.box {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

配置文件 postcss.config.js

postcss.config.js


import autoprefixer from "autoprefixer";
export default {
  map: false,
  plugins: [autoprefixer],
};

使用了配置文件后,可以简化命令行


npx postcss style.css -o dist.css

使用插件 postcss-preset-env

安装


pnpm i -D postcss-preset-env

配置文件 postcss.config.js


import autoprefixer from "autoprefixer";
import postcssPresetEnv from "postcss-preset-env";
export default {
  map: false,
  plugins: [
    autoprefixer,
    postcssPresetEnv({
      stage: 0,
    }),
  ],
};

关于stage:

image.png


image.png

输入 style.css


.box {
  box-sizing: border-box;
  &:hover{
    color: #ffffff;
  }
}

输出 dist.css


.box {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}
.box:hover{
    color: #ffffff;
  }

使用 stylelint

pnpm install --save-dev stylelint stylelint-config-standard

配置文件 stylelint.config.cjs


module.exports = {
  extends: ["stylelint-config-standard"],
};
$ npx stylelint style.css
style.css
 4:9   ✖  Expected single space before "{"  block-opening-brace-space-before
 5:12  ✖  Expected "#ffffff" to be "#fff"   color-hex-length
2 problems (2 errors, 0 warnings)

修复问题

.box {
  box-sizing: border-box;
  &:hover{
    /* color: #ffffff; */
    color: #fff;
  }
}
$ npx stylelint style.css
style.css
 4:9  ✖  Expected single space before "{"  block-opening-brace-space-before
1 problem (1 error, 0 warnings)

关闭冲突规则

pnpm i -D stylelint-config-prettier
stylelint.config.cjs
module.exports = {
  extends: ["stylelint-config-standard", "stylelint-config-prettier"],
};

使用插件 stylelint

postcss.config.js


import stylelint from "stylelint";
export default {
  plugins: [
    stylelint({
        fix: true
    }),
  ],
};

使用插件 postcss-pxtorem

package.json 增加脚本

{
  "scripts": {
    "build": "postcss style.css -o dist.css"
  }
}

安装

pnpm i -D postcss-pxtorem

配置 postcss.config.js


 
         
font-size: 15px;
// 输出
font-size: 0.9375rem;

完整配置

$ tree -I node_modules
.
├── package.json
├── pnpm-lock.yaml
├── postcss.config.js
├── src
│   └── style.css
└── stylelint.config.cjs

package.json


{
  "type": "module",
  "scripts": {
    "build": "postcss src/**/*.css --dir dist",
    "dev": "postcss src/**/*.css --dir dist --watch"
  },
  "devDependencies": {
    "autoprefixer": "^10.4.8",
    "postcss": "^8.4.16",
    "postcss-cli": "^10.0.0",
    "postcss-preset-env": "^7.7.2",
    "postcss-pxtorem": "^6.0.0",
    "stylelint": "^14.10.0",
    "stylelint-config-prettier": "^9.0.3",
    "stylelint-config-standard": "^27.0.0"
  },
  "browserslist": [
    "cover 99.5%"
  ]
}

postcss.config.js


import autoprefixer from "autoprefixer";
import stylelint from "stylelint";
import postcssPresetEnv from "postcss-preset-env";
import pxtorem from "postcss-pxtorem";
export default {
  // 不生成 sourcemaps
  map: false,
  plugins: [
    // 语法校验
    stylelint({
      fix: true, // 自动修复
    }),
    // 自动添加浏览器前缀
    autoprefixer,
    // 使用新语法
    postcssPresetEnv({
      stage: 0,
    }),
    // 单位转换:px->rem
    pxtorem,
  ],
};

stylelint.config.cjs


module.exports = {
  extends: ["stylelint-config-standard", "stylelint-config-prettier"],
};

相关文章
|
Linux API C语言
设备树知识小全(十):由设备树引发的BSP和驱动变更
设备树知识小全(十):由设备树引发的BSP和驱动变更
205 0
|
前端开发 JavaScript
vite中css最佳实践:使用postcss完善项目中的css配置
【8月更文挑战第3天】使用postcss完善项目中的css配置
2878 1
element-ui table排序sortable三种状态,怎么去掉默认状态
在 element-ui 中,也定义了 sort-orders 有三种状态: ascending、descending、null,这三种状态形成一个循环切换。
3223 0
|
11月前
|
JavaScript 前端开发 编译器
将 CommonJS 模块转换为 ES6 模块
【10月更文挑战第11天】 将 CommonJS 模块转换为 ES6 模块有三种主要方法:手动修改代码、使用工具(如 Babel)自动转换和逐步迁移。手动修改涉及导出和导入方式的转换,确保名称和结构一致;使用工具可自动化这一过程;逐步迁移适用于大型项目,先在新模块中使用 ES6 语法,再逐步替换旧模块。转换过程中需注意兼容性、代码逻辑调整和充分测试。
672 58
|
11月前
|
Web App开发 安全 Java
Debian 12.7 推出安全性和稳定性改进
【10月更文挑战第16天】
452 3
Debian 12.7 推出安全性和稳定性改进
成功解决:Failed to load resource: net::ERR_FILE_NOT_FOUND
这篇文章提供了解决"Failed to load resource: net::ERR_FILE_NOT_FOUND"错误的步骤,通过修改配置文件中的资源路径设置为相对路径"./"来成功运行打包后的项目。
成功解决:Failed to load resource: net::ERR_FILE_NOT_FOUND
|
缓存 JavaScript 前端开发
useMemo问题之在什么情况下使用useMemo和useCallback是不必要的
useMemo问题之在什么情况下使用useMemo和useCallback是不必要的
126 7
|
JSON C++ 数据格式
【VsCode】通过tasks.json中的problemMatcher属性的fileLocation子属性设定问题的输出内容
【VsCode】通过tasks.json中的problemMatcher属性的fileLocation子属性设定问题的输出内容
177 3
|
JavaScript 搜索推荐 前端开发
vue报错 ‘超出最大堆栈大小‘
vue报错 ‘超出最大堆栈大小‘
322 3
go反射获取变量类型、值、结构体成员、结构体方法
go反射获取变量类型、值、结构体成员、结构体方法
154 0