UNPKG

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