UNPKG

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