Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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