You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

593 lines
18 KiB

4 years ago
  1. # Browserslist [![Cult Of Martians][cult-img]][cult]
  2. <img width="120" height="120" alt="Browserslist logo by Anton Lovchikov"
  3. src="https://browserslist.github.io/browserslist/logo.svg" align="right">
  4. The config to share target browsers and Node.js versions between different
  5. front-end tools. It is used in:
  6. * [Autoprefixer]
  7. * [Babel]
  8. * [postcss-preset-env]
  9. * [eslint-plugin-compat]
  10. * [stylelint-no-unsupported-browser-features]
  11. * [postcss-normalize]
  12. * [obsolete-webpack-plugin]
  13. All tools will find target browsers automatically,
  14. when you add the following to `package.json`:
  15. ```js
  16. "browserslist": [
  17. "defaults",
  18. "not IE 11",
  19. "maintained node versions",
  20. ]
  21. ```
  22. Or in `.browserslistrc` config:
  23. ```yaml
  24. # Browsers that we support
  25. defaults
  26. not IE 11
  27. maintained node versions
  28. ```
  29. Developers set versions list in queries like `last 2 version`
  30. to be free from updating versions manually.
  31. Browserslist will use [Can I Use] data for this queries.
  32. Browserslist will take queries from tool option,
  33. `browserslist` config, `.browserslistrc` config,
  34. `browserslist` section in `package.json` or environment variables.
  35. [Browserslist Example] shows how every tool uses Browserslist.
  36. [cult-img]: https://cultofmartians.com/assets/badges/badge.svg
  37. [cult]: https://cultofmartians.com/done.html
  38. <a href="https://evilmartians.com/?utm_source=browserslist">
  39. <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
  40. alt="Sponsored by Evil Martians" width="236" height="54">
  41. </a>
  42. [stylelint-no-unsupported-browser-features]: https://github.com/ismay/stylelint-no-unsupported-browser-features
  43. [eslint-plugin-compat]: https://github.com/amilajack/eslint-plugin-compat
  44. [Browserslist Example]: https://github.com/browserslist/browserslist-example
  45. [postcss-preset-env]: https://github.com/jonathantneal/postcss-preset-env
  46. [postcss-normalize]: https://github.com/jonathantneal/postcss-normalize
  47. [Autoprefixer]: https://github.com/postcss/autoprefixer
  48. [Can I Use]: https://caniuse.com/
  49. [Babel]: https://github.com/babel/babel/tree/master/packages/babel-preset-env
  50. [obsolete-webpack-plugin]: https://github.com/ElemeFE/obsolete-webpack-plugin
  51. ## Table of Contents
  52. 1. [Tools](#tools)
  53. 2. [Best Practices](#best-practices)
  54. 3. [Queries](#queries)
  55. 4. [Config File](#config-file)
  56. 5. [Shareable Configs](#shareable-configs)
  57. 6. [Configuring for Different Environments](#configuring-for-different-environments)
  58. 7. [Custom Usage Data](#custom-usage-data)
  59. 8. [JS API](#js-api)
  60. 9. [Environment Variables](#environment-variables)
  61. 10. [Cache](#cache)
  62. ## Tools
  63. * [`browserslist-ga`] and [`browserslist-ga-export`] download your website
  64. browsers statistics to use it in `> 0.5% in my stats` query.
  65. * [`browserslist-useragent-regexp`] compiles Browserslist query to a RegExp
  66. to test browser useragent.
  67. * [`browserslist-useragent-ruby`] is a Ruby library to checks browser
  68. by user agent string to match Browserslist.
  69. * [`browserslist-browserstack`] runs BrowserStack tests for all browsers
  70. in Browserslist config.
  71. * [`caniuse-api`] returns browsers which support some specific feature.
  72. * Run `npx browserslist` in your project directory to see project’s
  73. target browsers. This CLI tool is built-in and available in any project
  74. with Autoprefixer.
  75. [`browserslist-useragent-regexp`]: https://github.com/browserslist/browserslist-useragent-regexp
  76. [`browserslist-useragent-ruby`]: https://github.com/browserslist/browserslist-useragent-ruby
  77. [`browserslist-browserstack`]: https://github.com/xeroxinteractive/browserslist-browserstack
  78. [`browserslist-ga-export`]: https://github.com/browserslist/browserslist-ga-export
  79. [`browserslist-useragent`]: https://github.com/pastelsky/browserslist-useragent
  80. [`browserslist-ga`]: https://github.com/browserslist/browserslist-ga
  81. [`caniuse-api`]: https://github.com/Nyalab/caniuse-api
  82. ## Best Practices
  83. * There is a `defaults` query, which gives a reasonable configuration
  84. for most users:
  85. ```json
  86. "browserslist": [
  87. "defaults"
  88. ]
  89. ```
  90. * If you want to change the default set of browsers we recommend to combine
  91. `last 2 versions`, `not dead` with a usage number like `> 0.2%`. This is
  92. because `last n versions` on its own does not add popular old versions while
  93. only using a percentage above `0.2%` will in the long run make popular
  94. browsers even more popular. We might run into a monopoly and stagnation
  95. situation, as we had with Internet Explorer 6. Please use this setting
  96. with caution.
  97. * Select browsers directly (`last 2 Chrome versions`) only if you are making
  98. a web app for a kiosk with one browser. There are a lot of browsers
  99. on the market. If you are making general web app you should respect
  100. browsers diversity.
  101. * Don’t remove browsers just because you don’t know them. Opera Mini has
  102. 100 million users in Africa and it is more popular in the global market
  103. than Microsoft Edge. Chinese QQ Browsers has more market share than Firefox
  104. and desktop Safari combined.
  105. ## Queries
  106. Browserslist will use browsers and Node.js versions query
  107. from one of these sources:
  108. 1. `browserslist` key in `package.json` file in current or parent directories.
  109. **We recommend this way.**
  110. 2. `.browserslistrc` config file in current or parent directories.
  111. 3. `browserslist` config file in current or parent directories.
  112. 4. `BROWSERSLIST` environment variable.
  113. 5. If the above methods did not produce a valid result
  114. Browserslist will use defaults:
  115. `> 0.5%, last 2 versions, Firefox ESR, not dead`.
  116. ### Query Composition
  117. An `or` combiner can use the keyword `or` as well as `,`.
  118. `last 1 version or > 1%` is equal to `last 1 version, > 1%`.
  119. `and` query combinations are also supported to perform an
  120. intersection of the previous query: `last 1 version and > 1%`.
  121. There is 3 different ways to combine queries as depicted below. First you start
  122. with a single query and then we combine the queries to get our final list.
  123. Obviously you can *not* start with a `not` combiner, since there is no left-hand
  124. side query to combine it with.
  125. | Query combiner type | Illustration | Example |
  126. | ------------------- | :----------: | ------- |
  127. |`or`/`,` combiner <br> (union) | ![Union of queries](img/union.svg) | `> .5% or last 2 versions` <br> `> .5%, last 2 versions` |
  128. | `and` combiner <br> (intersection) | ![intersection of queries](img/intersection.svg) | `> .5% and last 2 versions` |
  129. | `not` combiner <br> (relative complement) | ![Relative complement of queries](img/complement.svg) | `> .5% and not last 2 versions` <br> `> .5% or not last 2 versions` <br> `> .5%, not last 2 versions` |
  130. _A quick way to test your query is to do `npx browserslist '> 0.5%, not IE 11'`
  131. in your terminal._
  132. ### Full List
  133. You can specify the browser and Node.js versions by queries (case insensitive):
  134. * `defaults`: Browserslist’s default browsers
  135. (`> 0.5%, last 2 versions, Firefox ESR, not dead`).
  136. * `> 5%`: browsers versions selected by global usage statistics.
  137. `>=`, `<` and `<=` work too.
  138. * `> 5% in US`: uses USA usage statistics. It accepts [two-letter country code].
  139. * `> 5% in alt-AS`: uses Asia region usage statistics. List of all region codes
  140. can be found at [`caniuse-lite/data/regions`].
  141. * `> 5% in my stats`: uses [custom usage data].
  142. * `> 5% in browserslist-config-mycompany stats`: uses [custom usage data]
  143. from `browserslist-config-mycompany/browserslist-stats.json`.
  144. * `cover 99.5%`: most popular browsers that provide coverage.
  145. * `cover 99.5% in US`: same as above, with [two-letter country code].
  146. * `cover 99.5% in my stats`: uses [custom usage data].
  147. * `maintained node versions`: all Node.js versions, which are [still maintained]
  148. by Node.js Foundation.
  149. * `node 10` and `node 10.4`: selects latest Node.js `10.x.x`
  150. or `10.4.x` release.
  151. * `current node`: Node.js version used by Browserslist right now.
  152. * `extends browserslist-config-mycompany`: take queries from
  153. `browserslist-config-mycompany` npm package.
  154. * `ie 6-8`: selects an inclusive range of versions.
  155. * `Firefox > 20`: versions of Firefox newer than 20.
  156. `>=`, `<` and `<=` work too. It also works with Node.js.
  157. * `iOS 7`: the iOS browser version 7 directly.
  158. * `Firefox ESR`: the latest [Firefox ESR] version.
  159. * `PhantomJS 2.1` and `PhantomJS 1.9`: selects Safari versions similar
  160. to PhantomJS runtime.
  161. * `unreleased versions` or `unreleased Chrome versions`:
  162. alpha and beta versions.
  163. * `last 2 major versions` or `last 2 iOS major versions`:
  164. all minor/patch releases of last 2 major versions.
  165. * `since 2015` or `last 2 years`: all versions released since year 2015
  166. (also `since 2015-03` and `since 2015-03-10`).
  167. * `dead`: browsers without official support or updates for 24 months.
  168. Right now it is `IE 10`, `IE_Mob 10`, `BlackBerry 10`, `BlackBerry 7`,
  169. `Samsung 4` and `OperaMobile 12.1`.
  170. * `last 2 versions`: the last 2 versions for *each* browser.
  171. * `last 2 Chrome versions`: the last 2 versions of Chrome browser.
  172. * `not ie <= 8`: exclude browsers selected by previous queries.
  173. You can add `not ` to any query.
  174. [`caniuse-lite/data/regions`]: https://github.com/ben-eb/caniuse-lite/tree/master/data/regions
  175. [two-letter country code]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements
  176. [custom usage data]: #custom-usage-data
  177. [still maintained]: https://github.com/nodejs/Release
  178. [Can I Use]: https://caniuse.com/
  179. ### Debug
  180. Run `npx browserslist` in project directory to see what browsers was selected
  181. by your queries.
  182. ```sh
  183. $ npx browserslist
  184. and_chr 61
  185. and_ff 56
  186. and_qq 1.2
  187. and_uc 11.4
  188. android 56
  189. baidu 7.12
  190. bb 10
  191. chrome 62
  192. edge 16
  193. firefox 56
  194. ios_saf 11
  195. opera 48
  196. safari 11
  197. samsung 5
  198. ```
  199. ### Browsers
  200. Names are case insensitive:
  201. * `Android` for Android WebView.
  202. * `Baidu` for Baidu Browser.
  203. * `BlackBerry` or `bb` for Blackberry browser.
  204. * `Chrome` for Google Chrome.
  205. * `ChromeAndroid` or `and_chr` for Chrome for Android
  206. * `Edge` for Microsoft Edge.
  207. * `Electron` for Electron framework. It will be converted to Chrome version.
  208. * `Explorer` or `ie` for Internet Explorer.
  209. * `ExplorerMobile` or `ie_mob` for Internet Explorer Mobile.
  210. * `Firefox` or `ff` for Mozilla Firefox.
  211. * `FirefoxAndroid` or `and_ff` for Firefox for Android.
  212. * `iOS` or `ios_saf` for iOS Safari.
  213. * `Node` for Node.js.
  214. * `Opera` for Opera.
  215. * `OperaMini` or `op_mini` for Opera Mini.
  216. * `OperaMobile` or `op_mob` for Opera Mobile.
  217. * `QQAndroid` or `and_qq` for QQ Browser for Android.
  218. * `Safari` for desktop Safari.
  219. * `Samsung` for Samsung Internet.
  220. * `UCAndroid` or `and_uc` for UC Browser for Android.
  221. * `kaios` for KaiOS Browser.
  222. ## Config File
  223. ### `package.json`
  224. If you want to reduce config files in project root, you can specify
  225. browsers in `package.json` with `browserslist` key:
  226. ```json
  227. {
  228. "private": true,
  229. "dependencies": {
  230. "autoprefixer": "^6.5.4"
  231. },
  232. "browserslist": [
  233. "last 1 version",
  234. "> 1%",
  235. "IE 10"
  236. ]
  237. }
  238. ```
  239. ### `.browserslistrc`
  240. Separated Browserslist config should be named `.browserslistrc`
  241. and have browsers queries split by a new line. Comments starts with `#` symbol:
  242. ```yaml
  243. # Browsers that we support
  244. last 1 version
  245. > 1%
  246. IE 10 # sorry
  247. ```
  248. Browserslist will check config in every directory in `path`.
  249. So, if tool process `app/styles/main.css`, you can put config to root,
  250. `app/` or `app/styles`.
  251. You can specify direct path in `BROWSERSLIST_CONFIG` environment variables.
  252. ## Shareable Configs
  253. You can use the following query to reference an exported Browserslist config
  254. from another package:
  255. ```json
  256. "browserslist": [
  257. "extends browserslist-config-mycompany"
  258. ]
  259. ```
  260. For security reasons, external configuration only supports packages that have
  261. the `browserslist-config-` prefix. npm scoped packages are also supported, by
  262. naming or prefixing the module with `@scope/browserslist-config`, such as
  263. `@scope/browserslist-config` or `@scope/browserslist-config-mycompany`.
  264. If you don’t accept Browserslist queries from users, you can disable the
  265. validation by using the `dangerousExtend` option:
  266. ```js
  267. browserslist(queries, { path, dangerousExtend: true })
  268. ```
  269. Because this uses `npm`'s resolution, you can also reference specific files
  270. in a package:
  271. ```json
  272. "browserslist": [
  273. "extends browserslist-config-mycompany/desktop",
  274. "extends browserslist-config-mycompany/mobile"
  275. ]
  276. ```
  277. When writing a shared Browserslist package, just export an array.
  278. `browserslist-config-mycompany/index.js`:
  279. ```js
  280. module.exports = [
  281. 'last 1 version',
  282. '> 1%',
  283. 'ie 10'
  284. ]
  285. ```
  286. You can also include a `browserslist-stats.json` file as part of your shareable
  287. config at the root and query it using `> 5% in browserslist-config-mycompany stats`.
  288. It uses the same format as `extends` and the `dangerousExtend` property as above.
  289. ## Configuring for Different Environments
  290. You can also specify different browser queries for various environments.
  291. Browserslist will choose query according to `BROWSERSLIST_ENV` or `NODE_ENV`
  292. variables. If none of them is declared, Browserslist will firstly look
  293. for `production` queries and then use defaults.
  294. In `package.json`:
  295. ```js
  296. "browserslist": {
  297. "production": [
  298. "> 1%",
  299. "ie 10"
  300. ],
  301. "modern": [
  302. "last 1 chrome version",
  303. "last 1 firefox version"
  304. ],
  305. "ssr": [
  306. "node 12"
  307. ]
  308. }
  309. ```
  310. In `.browserslistrc` config:
  311. ```ini
  312. [production]
  313. > 1%
  314. ie 10
  315. [modern]
  316. last 1 chrome version
  317. last 1 firefox version
  318. [ssr]
  319. node 12
  320. ```
  321. ## Custom Usage Data
  322. If you have a website, you can query against the usage statistics of your site.
  323. [`browserslist-ga`] will ask access to Google Analytics and then generate
  324. `browserslist-stats.json`:
  325. ```
  326. npx browserslist-ga
  327. ```
  328. Or you can use [`browserslist-ga-export`] to convert Google Analytics data without giving a password for Google account.
  329. You can generate usage statistics file by any other method. File format should
  330. be like:
  331. ```js
  332. {
  333. "ie": {
  334. "6": 0.01,
  335. "7": 0.4,
  336. "8": 1.5
  337. },
  338. "chrome": {
  339. },
  340. }
  341. ```
  342. Note that you can query against your custom usage data while also querying
  343. against global or regional data. For example, the query
  344. `> 1% in my stats, > 5% in US, 10%` is permitted.
  345. [`browserslist-ga-export`]: https://github.com/browserslist/browserslist-ga-export
  346. [`browserslist-ga`]: https://github.com/browserslist/browserslist-ga
  347. [Can I Use]: https://caniuse.com/
  348. ## JS API
  349. ```js
  350. const browserslist = require('browserslist')
  351. // Your CSS/JS build tool code
  352. function process (source, opts) {
  353. const browsers = browserslist(opts.overrideBrowserslist, {
  354. stats: opts.stats,
  355. path: opts.file,
  356. env: opts.env
  357. })
  358. // Your code to add features for selected browsers
  359. }
  360. ```
  361. Queries can be a string `"> 1%, IE 10"`
  362. or an array `['> 1%', 'IE 10']`.
  363. If a query is missing, Browserslist will look for a config file.
  364. You can provide a `path` option (that can be a file) to find the config file
  365. relatively to it.
  366. Options:
  367. * `path`: file or a directory path to look for config file. Default is `.`.
  368. * `env`: what environment section use from config. Default is `production`.
  369. * `stats`: custom usage statistics data.
  370. * `config`: path to config if you want to set it manually.
  371. * `ignoreUnknownVersions`: do not throw on direct query (like `ie 12`).
  372. Default is `false.`
  373. * `dangerousExtend`: Disable security checks for `extend` query.
  374. Default is `false.`
  375. * `mobileToDesktop`: Use desktop browsers if Can I Use doesn’t have data
  376. about this mobile version. For instance, Browserslist will return
  377. `chrome 20` on `and_chr 20` query (Can I Use has only data only about
  378. latest versions of mobile browsers). Default is `false`.
  379. For non-JS environment and debug purpose you can use CLI tool:
  380. ```sh
  381. browserslist "> 1%, IE 10"
  382. ```
  383. You can get total users coverage for selected browsers by JS API:
  384. ```js
  385. browserslist.coverage(browserslist('> 1%'))
  386. //=> 81.4
  387. ```
  388. ```js
  389. browserslist.coverage(browserslist('> 1% in US'), 'US')
  390. //=> 83.1
  391. ```
  392. ```js
  393. browserslist.coverage(browserslist('> 1% in my stats'), 'my stats')
  394. //=> 83.1
  395. ```
  396. ```js
  397. browserslist.coverage(browserslist('> 1% in my stats', { stats }), stats)
  398. //=> 82.2
  399. ```
  400. Or by CLI:
  401. ```sh
  402. $ browserslist --coverage "> 1%"
  403. These browsers account for 81.4% of all users globally
  404. ```
  405. ```sh
  406. $ browserslist --coverage=US "> 1% in US"
  407. These browsers account for 83.1% of all users in the US
  408. ```
  409. ```sh
  410. $ browserslist --coverage "> 1% in my stats"
  411. These browsers account for 83.1% of all users in custom statistics
  412. ```
  413. ```sh
  414. $ browserslist --coverage "> 1% in my stats" --stats=./stats.json
  415. These browsers account for 83.1% of all users in custom statistics
  416. ```
  417. ## Environment Variables
  418. If some tool use Browserslist inside, you can change browsers settings
  419. by [environment variables]:
  420. * `BROWSERSLIST` with browsers queries.
  421. ```sh
  422. BROWSERSLIST="> 5%" gulp css
  423. ```
  424. * `BROWSERSLIST_CONFIG` with path to config file.
  425. ```sh
  426. BROWSERSLIST_CONFIG=./config/browserslist gulp css
  427. ```
  428. * `BROWSERSLIST_ENV` with environments string.
  429. ```sh
  430. BROWSERSLIST_ENV="development" gulp css
  431. ```
  432. * `BROWSERSLIST_STATS` with path to the custom usage data
  433. for `> 1% in my stats` query.
  434. ```sh
  435. BROWSERSLIST_STATS=./config/usage_data.json gulp css
  436. ```
  437. * `BROWSERSLIST_DISABLE_CACHE` if you want to disable config reading cache.
  438. ```sh
  439. BROWSERSLIST_DISABLE_CACHE=1 gulp css
  440. ```
  441. [environment variables]: https://en.wikipedia.org/wiki/Environment_variable
  442. ## Cache
  443. Browserslist caches the configuration it reads from `package.json` and
  444. `browserslist` files, as well as knowledge about the existence of files,
  445. for the duration of the hosting process.
  446. To clear these caches, use:
  447. ```js
  448. browserslist.clearCaches()
  449. ```
  450. To disable the caching altogether, set the `BROWSERSLIST_DISABLE_CACHE`
  451. environment variable.
  452. ## Security Contact
  453. To report a security vulnerability, please use the [Tidelift security contact].
  454. Tidelift will coordinate the fix and disclosure.
  455. [Tidelift security contact]: https://tidelift.com/security