js:Browserslist用特定语句查询浏览器列表的工具与Babel和Postcss配置使用

目录

Browserslist

Browserslist 是一个用特定语句查询浏览器列表的工具

文档

安装

bash 复制代码
pnpm install --save-dev browserslist

使用示例

bash 复制代码
$ npx browserslist "last 2 Chrome versions" 

chrome 119
chrome 118

Babel 和 Browserslist

文档

有如下代码,需要通过babel转译为兼容浏览器的代码

src/index.js

js 复制代码
const say = (msg) => {
  console.log(msg);
};
say("hello");

安装 babel依赖

bash 复制代码
pnpm install --save-dev @babel/core @babel/cli @babel/preset-env

在 package.json 中配置babel

json 复制代码
{
  "babel": {
    "presets": [
      "@babel/preset-env"
    ]
  }
}

运行

bash 复制代码
$ npx babel ./src/index.js --out-dir dist

输出

js 复制代码
"use strict";

var say = function say(msg) {
  console.log(msg);
};
say("hello");

在 package.json 中增加browserslist 配置

json 复制代码
{
    "babel": {
        "presets": [
          "@babel/preset-env"
        ]
    },
    "browserslist": [
        "chrome 119"
    ]
}

运行

bash 复制代码
$ npx babel ./src/index.js --out-dir dist

输出

js 复制代码
"use strict";

const say = msg => {
  console.log(msg);
};
say("hello");

比对两次输出,将浏览器兼容范围减小后,代码直接就是箭头函数

Postcss 和 Browserslist

使用postcss自动添加css的浏览器前缀

文档

安装

bash 复制代码
$ pnpm i -D postcss postcss-cli autoprefixer

在 package.json 中 配置 postcss

json 复制代码
{
  "postcss": {
    "map": false,
    "plugins": {
      "autoprefixer": {}
    }
  }
}

src/style.css

css 复制代码
.box {
  box-sizing: border-box;
}

不配置browserslist的情况下

运行命令

bash 复制代码
$ npx postcss ./src/style.css -o dist/style.css

生成

css 复制代码
.box {
  box-sizing: border-box;
}

package.json 配置browserslist

json 复制代码
{
    "browserslist": [
        "cover 99.5%"
    ],
}

运行命令

bash 复制代码
$ npx postcss ./src/style.css -o dist/style.css

输出

css 复制代码
.box {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

推荐配置

Browserslist 将依赖BROWSERSLIST_ENV 或者 NODE_ENV

json 复制代码
{
  "browserslist": {
    "production": [
      "> 1%",
      "last 2 version"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version"
    ]
  }
}
  • > 1% 全球市场占有率大于 1% 的浏览器
  • last 2 version 所有浏览器的最后 2 个版本
  • last 1 chrome version 查找 Chrome 浏览器的最后 1 个版本

完整配置 package.json

json 复制代码
{
  "type": "module",
  "dependencies": {},
  "devDependencies": {
    "browserslist": "^4.22.1",
    "@babel/cli": "^7.23.0",
    "@babel/core": "^7.23.2",
    "@babel/preset-env": "^7.23.2",
    "autoprefixer": "^10.4.16",
    "postcss": "^8.4.31",
    "postcss-cli": "^10.1.0",
    "postcss-preset-env": "^9.3.0"
  },
  "browserslist": {
    "production": [
      "> 1%",
      "last 2 version"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version"
    ]
  },
  "babel": {
    "presets": [
      "@babel/preset-env"
    ]
  },
  "postcss": {
    "map": false,
    "plugins": {
      "autoprefixer": {}
    }
  }
}

参考文章

相关推荐
无限的鲜花3 小时前
反射(原创推荐)
java·开发语言
yongche_shi3 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
一路向北he3 小时前
字节钢铁军团--“提供情境,而非控制”
java·开发语言·前端
kyriewen4 小时前
豆包和千问同时关了智能体,我用它们搭的 3 个自动化全废了——迁移方案整理
前端·javascript·ai编程
铁皮饭盒4 小时前
用 Bun.cron 定时 7 月 7 日,为啥? 看图1
javascript
AI行业学习4 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
大圣编程5 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
upgrador6 小时前
基础知识:C++ STL构造函数的左闭右开惯例及其实现原理
开发语言·c++
之歆6 小时前
Vue商品详情与放大镜组件
前端·javascript·vue.js
yoothey7 小时前
报废审批流规则引擎设计——责任链模式完整实现
linux·开发语言·bash