UNPKG

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