![Introducing Dashboard Analytics](https://cdn.sanity.io/images/cgdhsj6q/production/af327c5bd94708db501516a75da6870aef894c64-2286x1522.png?w=400&fit=max&auto=format)
Product
Introducing Dashboard Analytics
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
The doiuse npm package is a tool for checking your CSS against browser support. It helps developers ensure that their CSS is compatible with the browsers they intend to support by identifying unsupported or partially supported CSS features.
Check CSS for browser support
This feature allows you to check your CSS code against a list of browsers to see if any features are not supported. The code sample demonstrates how to use doiuse with PostCSS to process a CSS string and log any warnings about unsupported features.
const doiuse = require('doiuse');
const postcss = require('postcss');
const css = 'a { display: flex; }';
postcss([doiuse({ browsers: ['> 1%', 'last 2 versions'] })])
.process(css)
.then(result => {
result.warnings().forEach(warn => {
console.warn(warn.toString());
});
});
Custom browser support configuration
This feature allows you to specify custom browser support configurations. The code sample shows how to configure doiuse to check CSS against Internet Explorer 10 and the last two versions of all other browsers.
const doiuse = require('doiuse');
const postcss = require('postcss');
const css = 'a { display: grid; }';
postcss([doiuse({ browsers: ['ie >= 10', 'last 2 versions'] })])
.process(css)
.then(result => {
result.warnings().forEach(warn => {
console.warn(warn.toString());
});
});
caniuse-lite is a smaller, more compact version of the caniuse database that provides data on browser support for various web technologies. Unlike doiuse, caniuse-lite does not directly check your CSS but provides the data that can be used by other tools to perform such checks.
Autoprefixer is a PostCSS plugin that parses your CSS and adds vendor prefixes to CSS rules using values from the Can I Use database. While doiuse checks for unsupported features, Autoprefixer automatically adds necessary prefixes to ensure compatibility with specified browsers.
Stylelint is a powerful CSS linter that helps you avoid errors and enforce consistent conventions in your stylesheets. It can be extended with plugins to check for browser compatibility issues, similar to doiuse, but offers a broader range of linting capabilities.
Lint CSS for browser support against caniuse database.
npm install -g doiuse
git clone git@github.com:anandthakker/doiuse.git
cd doiuse
npm install
npm run babel
That last step transpiles the ES6 from src/ to ES5 in lib/. Already happens as a pretest
step for npm test
.
doiuse --browsers "ie >= 9, > 1%, last 2 versions" main.css
# or
cat main.css | doiuse --browsers "ie >= 9, > 1%, last 2 versions"
Sample output:
/projects/website/main.css:5:3: CSS3 Box-sizing not supported by: IE (8,9,10,11), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10,11)
/projects/website/main.css:6:3: CSS3 Box-sizing not supported by: IE (8,9,10,11), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10,11)
/projects/website/main.css:8:3: CSS user-select: none not supported by: IE (8,9)
/projects/website/main.css:9:3: CSS user-select: none not supported by: IE (8,9)
/projects/website/main.css:10:3: CSS user-select: none not supported by: IE (8,9)
/projects/website/main.css:11:3: CSS user-select: none not supported by: IE (8,9)
/projects/website/main.css:12:3: CSS user-select: none not supported by: IE (8,9)
/projects/website/main.css:13:3: Pointer events not supported by: IE (8,9,10), Firefox (32,33), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10)
/projects/website/main.css:14:3: Pointer events not supported by: IE (8,9,10), Firefox (32,33), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10)
/projects/website/main.css:32:3: CSS3 Transforms not supported by: IE (8)
Use --json
to get output as (newline-delimited) JSON objects.
var postcss = require('postcss');
var doiuse = require('doiuse');
postcss(doiuse({
browsers:['ie >= 6', '> 1%'],
ignore: ['rem'], // an optional array of features to ignore
ignoreFiles: ['**/normalize.css'], // an optional array of file globs to match against original source file path, to ignore
onFeatureUsage: function(usageInfo) {
console.log(usageInfo.message);
}
})).process("a { background-size: cover; }")
var gulp = require('gulp')
var postcss = require('postcss')
var doiuse = require('doiuse')
gulp.src(src, { cwd: process.cwd() })
.pipe(gulp.postcss([
doiuse({
browsers: [
'ie >= 8',
'> 1%'
],
ignore: ['rem'], // an optional array of features to ignore
ignoreFiles: ['**/normalize.css'], // an optional array of file globs to match against original source file path, to ignore
onFeatureUsage: function (usageInfo) {
console.log(usageInfo.message)
}
})
]))
In particular, the approach to detecting features usage is currently quite naive.
Refer to the data in /src/data/features.js.
properties
, we just use those
properties for regex/substring matches against the properties used in the input CSS.values
, then we also require that the associated
value matches one of those values.var doiuse = require('doiuse/stream');
process.stdin
.pipe(doiuse({ browsers: ['ie >= 8', '> 1%'], ignore: ['rem'] }))
.on('data', function (usageInfo) {
console.log(JSON.stringify(usageInfo))
})
Yields UsageInfo
objects as described below.
postcss(doiuse(opts)).process(css)
, where opts
is:
{
browsers: ['ie >= 8', '> 1%'] // an autoprefixer-like array of browsers.
ignore: ['rem'], // an optional array of features to ignore
ignoreFiles: ['**/normalize.css'], // an optional array of file globs to match against original source file path, to ignore
onFeatureUsage: function(usageInfo) { } // a callback for usages of features not supported by the selected browsers
}
And usageInfo
looks like this:
{
message: '<input source>: line <l>, col <c> - CSS3 Gradients not supported by: IE (8)'
feature: 'css-gradients', //slug identifying a caniuse-db feature
featureData:{
title: 'CSS Gradients',
missing: "IE (8)" // string of browsers missing support for this feature.
missingData: {
// map of browser -> version -> (lack of)support code
ie: { '8': 'n' }
},
caniuseData: { // data from caniuse-db/features-json/[feature].json }
},
usage: {} //the postcss node where that feature is being used.
}
Called once for each usage of each css feature not supported by the selected
browsers.
doiuse is an OPEN Open Source Project. This means that:
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
MIT
NOTE: Many of the files in test/cases are from autoprefixer-core, Copyright 2013 Andrey Sitnik andrey@sitnik.ru. Please see https://github.com/postcss/autoprefixer-core.
FAQs
Lint CSS for browser support against caniuse database
The npm package doiuse receives a total of 263,276 weekly downloads. As such, doiuse popularity was classified as popular.
We found that doiuse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
Security News
A new OpenSSF report uncovers critical gaps in secure software training, with 75% of new developers unfamiliar with secure practices, highlighting urgent educational needs.
Security News
The 2023 Python Developers Survey reveals key trends in packaging, web frameworks, and developer demographics, highlighting a shift toward innovative tools as the Python community diversifies and grows among less experienced developers.