UNPKG

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