What is browserslist?
The browserslist npm package is used to share target browsers and Node.js versions between different front-end tools, like Autoprefixer, Babel, and others. It utilizes a configuration file or package.json entries to specify the range of browser versions that your project supports. It helps in making decisions for transpiling JavaScript, adding vendor prefixes to CSS, and even defining which browser versions to test during the development process.
What are browserslist's main functionalities?
Defining target browsers
This feature allows you to define a range of browsers your project supports by specifying queries like market share, last versions, or excluding certain browsers. The code sample would typically be placed in a package.json file.
"browserslist": ["> 1%", "last 2 versions", "not dead"]
Integration with build tools
Browserslist can be integrated with build tools like Webpack, Babel, or PostCSS to automatically apply browser-specific transformations based on the defined browser support criteria.
const browserslist = require('browserslist');
const supportedBrowsers = browserslist('> 0.5%, last 2 versions, Firefox ESR, not dead');
CLI Usage
Browserslist provides a command-line interface (CLI) that can be used to check which browsers and versions are selected by your queries.
npx browserslist "> 1%, last 2 versions"
Configuring environments
You can define different sets of browsers for various environments, like production or development, within a .browserslistrc file.
[production]
> 1% in US
[development]
last 1 chrome version
Other packages similar to browserslist
caniuse-api
The caniuse-api provides raw browser support data from Can I Use, which is similar to the data that browserslist uses. However, it focuses more on querying specific features for browser support rather than defining a list of supported browsers for a project.
autoprefixer
Autoprefixer is a CSS post-processor that uses browserslist to add vendor prefixes to CSS rules. It is a consumer of browserslist data rather than a direct alternative, but it serves a similar purpose in terms of browser compatibility.
@babel/preset-env
Babel's preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms are needed by your target environment. It uses browserslist to determine which features need to be transformed or polyfilled. While not a direct alternative, it shares the goal of adapting code to be compatible with different environments.
Browserslist
Get browser versions that match given criteria.
Useful for tools like Autoprefixer.
You can select browsers by passing a string. This library will use
Can I Use data to return list of all matching versions.
For example, query to select all browser versions that are the last version
of each major browser, or have a usage of over 10% in global usage statistics:
browserslist('last 1 version, > 10%');
Queries
Browserslist will use browsers criterias from:
- First argument.
BROWSERSLIST
environment variable.browserslist
config file in current or parent directories.- If all methods will not give a result, Browserslist will use defaults:
> 1%, last 2 versions, Firefox ESR
.
Multiple criteria are combined as a boolean OR
. A browser version must match
at least one of the criteria to be selected.
You can specify the versions by queries (case insensitive):
last 2 versions
: the last 2 versions for each major browser.last 2 Chrome versions
: the last 2 versions of Chrome browser.> 5%
: versions selected by global usage statistics.> 5% in US
: uses USA usage statistics. It accepts two-letter country code.> 5% in my stats
: uses custom usage data.ie 6-8
: selects an inclusive range of versions.Firefox > 20
: versions of Firefox newer than 20.Firefox >= 20
: versions of Firefox newer than or equal to 20.Firefox < 20
: versions of Firefox less than 20.Firefox <= 20
: versions of Firefox less than or equal to 20.Firefox ESR
: the latest [Firefox ESR] version.iOS 7
: the iOS browser version 7 directly.not ie <= 8
: exclude browsers selected before by previous queries.
You can add not
to any query.
Browserslist works with separated versions of browsers.
You should avoid queries like Firefox > 0
.
Browsers
Names are case insensitive:
Major Browsers
Chrome
for Google Chrome.Firefox
or ff
for Mozilla Firefox.Explorer
or ie
for Internet Explorer.Edge
for Microsoft Edge.iOS
or ios_saf
for iOS Safari.Opera
for Opera.Safari
for desktop Safari.ExplorerMobile
or ie_mob
for Internet Explorer Mobile.
Other
Android
for Android WebView.BlackBerry
or bb
for Blackberry browser.ChromeAndroid
or and_chr
for Chrome for Android
(in Other section, because mostly same as common Chrome
).FirefoxAndroid
or and_ff
for Firefox for Android.OperaMobile
or op_mob
for Opera Mobile.OperaMini
or op_mini
for Opera Mini.
Config File
Browserslist’s config should be named browserslist
and have browsers queries
split by a new line. Comments starts with #
symbol:
> 1%
Last 2 versions
IE 8
Browserslist will check config in every directory in path
.
So, if tool process app/styles/main.css
, you can put config to root,
app/
or app/styles
.
You can specify direct path to config by config
option
or BROWSERSLIST_CONFIG
environment variables.
Environment Variables
If some tool use Browserslist inside, you can change browsers settings
by environment variables:
-
BROWSERSLIST
with browsers queries.
BROWSERSLIST="> 5%" gulp css
-
BROWSERSLIST_CONFIG
with path to config file.
BROWSERSLIST_CONFIG=./config/browserslist gulp css
-
BROWSERSLIST_STATS
with path to the custom usage data.
BROWSERSLIST_STATS=./config/usage_data.json gulp css
Custom Usage Data
If you have a website, you can query against the usage statistics of your site:
-
Import your Google Analytics data into Can I Use.
Press Import…
button in Settings page.
-
Open browser DevTools on [caniuse.com] add paste this snippet into Console:
var e=document.createElement('a');e.setAttribute('href', 'data:text/plain;charset=utf-8,'+encodeURIComponent(JSON.stringify(JSON.parse(localStorage['usage-data-by-id'])[localStorage['config-primary_usage']])));e.setAttribute('download','stats.json');document.body.appendChild(e);e.click();document.body.removeChild(e);}
-
Save data to file in your project.
-
Give it to Browserslist by stats
option
or BROWSERSLIST_STATS
environment variable:
browserslist('> 5% in my stats', { stats: 'path/to/the/stats.json' });
Of course, you can generate usage statistics file by any other method.
Option stats
accepts path to file or data itself:
var custom = {
ie: {
6: 0.01,
7: 0.4,
8: 1.5
},
chrome: {
…
},
…
};
browserslist('> 5% in my stats', { stats: custom });
Note that you can query against your custom usage data while also querying
against global or regional data. For example, the query
> 5% in my stats, > 1%, > 10% in US
is permitted.
Usage
var browserslist = require('browserslist');
var process = function (css, opts) {
var browsers = browserslist(opts.browsers, { path: opts.file });
}
Queries can be a string "> 5%, last 1 version"
or an array ['> 5%', 'last 1 version']
.
If a query is missing, Browserslist will look for a config file.
You can provide a path
option (that can be a file) to find the config file
relatively to it.
For non-JS environment and debug purpose you can use CLI tool:
browserslist "> 1%, last 2 version"