UNPKG

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