Browserslist

博客介绍了不同浏览器支持特性的兼容性问题,指出可借助autoprefixer、babel等工具处理。同时重点介绍了Browserslist,它能在前端工具间共享目标浏览器和node版本配置,还说明了其原理、安装、配置文件及常用条件等内容。

不同浏览器支持特性的兼容性:

不同浏览器支持特性的兼容性:是指某些 CSS 特性、JS 语法等,有些浏览器支持,有些浏览器不支持。

目前解决这个兼容性问题非常简单,已经有工具可以自动帮助开发人员处理了。例如:autoprefixer 可以自动给 CSS 添加浏览器前缀,以兼容不同的浏览器;babel 可以对 JS 语法进行转换。

但是,并不需要给所有浏览器都做兼容性处理。因为某些浏览器可能市场占有率已经很低,几乎没有人再使用了,再针对它做兼容性处理,反而增加了项目的包体积。

可以在 https://caniuse.com/usage-table 网站中查看浏览器的市场占有率。

Browserslist:

Browserslist 是一个在不同的前端工具之间,共享目标浏览器和 node 版本配置的工具。用来告诉前端工具(例如:Autoprefixer、postcss-preset-env、Babel 等),需要适配哪些浏览器和 node 版本。

原理:

Browserslist 会根据编写的条件使用 caniuse-lite 工具去 caniuse 网站上查询可以匹配到哪些浏览器。

运行 npx browserslist ">1%, last 2 versions, not dead" 可以看到匹配到的浏览器。
在这里插入图片描述

安装:

安装 Webpack 时会自动安装 Browserslist。

配置文件:

在项目中使用 Autoprefixer、Babel、postcss-preset-env 等前端工具时,会自动去配置了 Browserslist 的地方查询需要适配哪些浏览器和 node 版本。

如果没有明确配置的话,会使用 default 默认值。

  1. 在项目的根目录下创建 .browserslistrc 配置文件,直接编写条件。
    // 换行也表示或的关系
    ">1%"
    "last 2 versions"
    "not dead"
    
  2. 也可以在 package.json 文件的 browserslist 属性下编写条件。
    // package.json
    {
    	"browserslist": [
    	    ">1%", 
    	    "last 2 versions", 
    	    "not dead"
    	  ]
    }
    

常用条件:

如果使用 or 或者 , 分割多个条件,则它们之间是或的关系;如果使用 and 分割多个条件,则它们之间是且的关系;如果使用 not,则代表取反。

  1. defaults:Browserslist 的默认浏览器配置,>0.5%, last 2 versions, Firefox ESR, not dead
  2. >1%: 市场占用率大于 1% 的浏览器。可以使用 >>==<=< 符号。
  3. dead:24 个月内没有官方支持或更新的浏览器。
  4. laset 2 versions:浏览器最新的两个版本。
(venv) gapinyc@DESKTOP-9QS7RL5:~/superset/superset-frontend$ npm run build > superset@5.0.0 build > cross-env NODE_OPTIONS=--max_old_space_size=8192 NODE_ENV=production BABEL_ENV="${BABEL_ENV:=production}" webpack --color --mode production [Superset Plugin] Use symlink source for @superset-ui/chart-controls @ ./packages/superset-ui-chart-controls [Superset Plugin] Use symlink source for @superset-ui/core @ ./packages/superset-ui-core [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-calendar @ ./plugins/legacy-plugin-chart-calendar [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-chord @ ./plugins/legacy-plugin-chart-chord [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-country-map @ ./plugins/legacy-plugin-chart-country-map [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-horizon @ ./plugins/legacy-plugin-chart-horizon [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-map-box @ ./plugins/legacy-plugin-chart-map-box [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-paired-t-test @ ./plugins/legacy-plugin-chart-paired-t-test [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-parallel-coordinates @ ./plugins/legacy-plugin-chart-parallel-coordinates [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-partition @ ./plugins/legacy-plugin-chart-partition [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-rose @ ./plugins/legacy-plugin-chart-rose [Superset Plugin] Use symlink source for @superset-ui/legacy-plugin-chart-world-map @ ./plugins/legacy-plugin-chart-world-map [Superset Plugin] Use symlink source for @superset-ui/legacy-preset-chart-deckgl @ ./plugins/legacy-preset-chart-deckgl [Superset Plugin] Use symlink source for @superset-ui/legacy-preset-chart-nvd3 @ ./plugins/legacy-preset-chart-nvd3 [Superset Plugin] Use symlink source for @superset-ui/plugin-chart-cartodiagram @ ./plugins/plugin-chart-cartodiagram [Superset Plugin] Use symlink source for @superset-ui/plugin-chart-echarts @ ./plugins/plugin-chart-echarts [Superset Plugin] Use symlink source for @superset-ui/plugin-chart-handlebars @ ./plugins/plugin-chart-handlebars [Superset Plugin] Use symlink source for @superset-ui/plugin-chart-pivot-table @ ./plugins/plugin-chart-pivot-table [Superset Plugin] Use symlink source for @superset-ui/plugin-chart-table @ ./plugins/plugin-chart-table [Superset Plugin] Use symlink source for @superset-ui/plugin-chart-word-cloud @ ./plugins/plugin-chart-word-cloud [Superset Plugin] Use symlink source for @superset-ui/switchboard @ ./packages/superset-ui-switchboard Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme 10% building 0/8 entries 8/8 dependencies 0/5 modulesBrowserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) 10% building 0/8 entries 8/9 dependencies 0/5 modules`isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme 10% building 0/8 entries 10/10 dependencies 0/7 modules`isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) 10% building 0/8 entries 133/231 dependencies 3/72 modulesBrowserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) 10% building 0/8 entries 206/300 dependencies 11/115 modules`isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration` at isModuleDeclaration (/home/gapinyc/superset/superset-frontend/node_modules/@babel/types/lib/validators/generated/index.js:2793:35) at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) 92% sealing asset processing CssMinimizerPluginBrowserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: browsers data (caniuse-lite) is 9 months old. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme (venv) gapinyc@DESKTOP-9QS7RL5:~/superset/superset-frontend$
最新发布
10-26
你已经成功运行了: ```bash npm run build ``` 虽然输出中充满了警告(Warnings),但 **最关键的是:没有出现 `ERROR` 或构建中断!** --- ## ✅ 当前状态总结 | 指标 | 状态 | 说明 | |------|------|------| | 构建是否完成? | ✅ 是 | 最后进度到 `92% sealing asset processing CssMinimizerPlugin`,未报错退出 | | 是否生成了文件? | ✅ 很可能已生成 | 静态资源应位于 `../superset/static/assets/` | | 警告严重吗? | ⚠️ 多数为非致命性警告 | 可忽略或后续优化 | --- ## 🧾 详细解释每类信息 ### 1. 🔵 `[Superset Plugin] Use symlink source for ...` 这是 Superset 前端插件系统的调试日志。 ```text [Superset Plugin] Use symlink source for @superset-ui/chart-controls ``` - 表示 Webpack 正在使用本地开发模式加载内部包(如 `@superset-ui/core`)。 - 这些包本该是 npm 发布的库,但在源码构建时通过 `symlink`(软链接)指向本地目录(如 `./packages/superset-ui-core`)。 - ✅ **正常行为**,用于开发调试,不影响生产构建。 📌 **你可以安全忽略这些日志。** --- ### 2. 🟡 `Browserslist: browsers data (caniuse-lite) is 9 months old` #### 错误原因: `caniuse-lite` 是一个轻量级数据库,告诉 Babel 和 PostCSS 支持哪些浏览器特性。它太久没更新了。 > 默认每 6 个月提醒一次,你现在差了 9 个月。 #### 解决方法: 升级 `caniuse-lite`: ```bash npx update-browserslist-db@latest ``` 或者手动安装: ```bash npm install --save-dev caniuse-lite ``` 然后重新运行构建,这个警告就会消失。 ✅ **这不是错误,只是建议更新兼容性数据。** --- ### 3. 🟡 `isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration` #### 报错来源: ```js at PluginPass.Program (/home/gapinyc/superset/superset-frontend/node_modules/babel-plugin-lodash/lib/index.js:102:44) ``` 这说明问题出在旧版 `babel-plugin-lodash` 使用了一个被废弃的 Babel API。 #### 原因分析: - `@babel/types@7.13+` 标记 `isModuleDeclaration()` 为废弃。 - 新推荐用法是 `isImportOrExportDeclaration()`。 - 但 `babel-plugin-lodash` 最后维护是 4 年前(v3.3.4),不再更新。 #### 影响? ❌ 插件仍能工作,但打印大量警告 ✅ 不影响最终代码功能和性能 #### 如何修复? ##### ✅ 方案一:替换 `babel-plugin-lodash`(推荐) 改用更现代的方式做 lodash tree-shaking,比如直接使用 ES Modules + Webpack 自动摇树(tree-shaking)。 修改 `.babelrc` 或 `babel.config.js`,删除这一行: ```json "plugins": [ // ... "lodash", ← 删除此项 ] ``` 然后确保你的代码使用 import from lodash/es 而不是 lodash: ```js import { debounce } from 'lodash'; // ❌ 全量引入 import { debounce } from 'lodash/debounce'; // ✅ 推荐 // 或更好: import debounce from 'lodash-es/debounce'; // ✅ 支持 tree-shaking ``` Webpack 会自动剔除未使用的函数。 --- ## ✅ 下一步:验证构建结果 检查是否生成了前端静态资源: ```bash ls -la ../superset/static/assets/ ``` 你应该看到类似以下内容: ``` app.1a2b3c4d.js vendors~app.5e6f7g8h.js main.css images/superset-logo-horiz.png ... ``` 如果有这些文件 → **恭喜!前端构建成功!** --- ## ✅ 启动 Superset 服务 回到根目录并启动: ```bash cd ~/superset gunicorn \ --bind 0.0.0.0:8088 \ --workers 4 \ --worker-class gthread \ --threads 4 \ --timeout 120 \ --max-requests 1000 \ --max-requests-jitter 100 \ --preload \ --access-logfile - \ --error-logfile - \ "superset.app:create_app()" ``` 访问 `http://localhost:8088`: - 如果页面打开、样式正常、图表可渲染 → 构建完全成功 - 即使有上述警告,也不影响运行 --- ## 🛠️ 可选优化建议 | 目标 | 方法 | |------|------| | 减少 warning 输出 | 移除 `babel-plugin-lodash` | | 更新浏览器支持列表 | 运行 `npx update-browserslist-db@latest` | | 加快构建速度 | 使用 `webpack --profile` 分析耗时模块 | | 替代 zstd 压缩 | 使用 `compression-webpack-plugin` 支持 gzip/brotli | --- ## ✅ 总结 | 问题 | 是否需要处理 | 建议操作 | |------|---------------|----------| | `Use symlink source` 日志 | ❌ 否 | 忽略即可 | | `caniuse-lite is 9 months old` | ⚠️ 建议 | 运行 `npx update-browserslist-db@latest` | | `isModuleDeclaration` 已废弃 | ⚠️ 建议 | 删除 `babel-plugin-lodash` 插件 | | 构建成功了吗? | ✅ 是! | 检查 `assets/` 目录并启动后端 | 🎉 **你已完成前端构建流程,可以进入部署阶段!** ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值