UNPKG

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