25743b1a4ec052934e1c35c84fd5eaca43e94bb9e709a22972d20fd33b7eb02d08aeb68befbcbf823d0e868b8db239faf08eeb30820423050c12ff51f6b283 15 KB

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