Disallow features that are unsupported by the browsers that you are targeting
npx @tessl/cli install tessl/npm-stylelint-no-unsupported-browser-features@8.0.0A Stylelint plugin that disallows CSS features that are unsupported by your target browser audience. It uses doiuse for browser compatibility checking against the Can I Use database and integrates with browserslist for flexible browser targeting.
npm install stylelint-no-unsupported-browser-featuresimport plugin from "stylelint-no-unsupported-browser-features";For CommonJS environments:
const plugin = require("stylelint-no-unsupported-browser-features");Note: The plugin is an ES module ("type": "module" in package.json), but can be imported in CommonJS environments that support ES module imports.
Add the plugin to your Stylelint configuration:
{
"plugins": ["stylelint-no-unsupported-browser-features"],
"rules": {
"plugin/no-unsupported-browser-features": true
}
}With configuration options:
{
"plugins": ["stylelint-no-unsupported-browser-features"],
"rules": {
"plugin/no-unsupported-browser-features": [
true,
{
"browsers": ["> 1%", "Last 2 versions"],
"ignore": ["rem", "css-table"],
"ignorePartialSupport": true
}
]
}
}Example CSS that would trigger warnings:
/* Triggers warning if targeting IE 8 */
div {
display: table;
}
/* Triggers warning if targeting older browsers */
.element {
border-radius: 10px;
}The plugin integrates with Stylelint's standard architecture:
stylelint.createPlugin() for proper integrationThe main plugin export that integrates with Stylelint. This is a function with additional properties attached.
/**
* Main Stylelint plugin export - a function created by stylelint.createPlugin()
* @param on - Whether the rule is enabled
* @param options - Optional configuration for the rule
* @returns Stylelint rule function
*/
declare function plugin(on: boolean, options?: PluginOptions): StylelintRule;
/**
* Plugin properties attached to the main function
*/
declare namespace plugin {
/** The unique identifier for this Stylelint rule */
const ruleName: "plugin/no-unsupported-browser-features";
/** Message templates for reporting violations */
const messages: {
rejected: (details: string) => string;
};
}
/**
* Stylelint rule function signature
*/
interface StylelintRule {
(root: PostCSSRoot, result: StylelintResult): void;
}
/**
* PostCSS AST root node
*/
interface PostCSSRoot {
// PostCSS AST root containing CSS nodes
}
/**
* PostCSS AST node
*/
interface PostCSSNode {
// PostCSS AST node representing a CSS declaration, rule, etc.
}
/**
* Stylelint result object for reporting violations
*/
interface StylelintResult {
// Stylelint result object for rule reporting
}Configuration options for customizing browser compatibility checks.
interface PluginOptions {
/** Array of browserslist-compatible browser selectors */
browsers?: string[];
/** Array of CSS feature names to ignore */
ignore?: string[];
/** Whether to ignore features with partial browser support */
ignorePartialSupport?: boolean;
}browsers - Array of browserslist-compatible strings:
["> 1%", "Last 2 versions"] - Modern browser support["IE 8", "Chrome >= 60"] - Specific browser versions["defaults"] - Uses browserslist defaultsignore - Array of feature names from Can I Use database:
["rem", "css-table"] - Ignore specific CSS featuresignorePartialSupport - Boolean flag for partial support handling:
true - Ignore features that are only partially supportedfalse (default) - Report all unsupported features including partialThe plugin uses browserslist for determining target browsers. Browser lists can be specified in multiple ways:
Via plugin options:
{
"plugin/no-unsupported-browser-features": [
true,
{ "browsers": ["> 1%", "Last 2 versions"] }
]
}Via browserslist config file (.browserslistrc):
> 1%
Last 2 versionsVia package.json:
{
"browserslist": ["> 1%", "Last 2 versions"]
}The plugin reports violations using Stylelint's standard error format. Based on the source code, error messages are formatted by the cleanWarningText() internal function and follow this pattern:
interface StylelintViolation {
ruleName: "plugin/no-unsupported-browser-features";
message: string; // Format: 'Unexpected browser feature "feature-id" is [support description] (plugin/no-unsupported-browser-features)'
node: PostCSSNode; // The CSS node causing the violation
line: number;
column: number;
}Actual Message Format Examples from Tests:
Unexpected browser feature "css-table" is not supported by IE 6 (plugin/no-unsupported-browser-features)Unexpected browser feature "rem" is only partially supported by IE 9 (plugin/no-unsupported-browser-features)Unexpected browser feature "css-table" is not supported by IE 6,7 (plugin/no-unsupported-browser-features) (multiple browsers)Unexpected browser feature "rem" is not supported by IE 7 (plugin/no-unsupported-browser-features) (when ignorePartialSupport removes partial warnings)The plugin leverages the doiuse library for browser compatibility detection. While this integration is internal, it's important for understanding the plugin's behavior:
// Internal integration details (not directly exposed in public API)
interface DoiuseIntegration {
/** Options passed to doiuse processor */
doiuseOptions: {
browsers?: string[];
ignore?: string[];
ignorePartialSupport?: boolean;
onFeatureUsage: (info: FeatureUsageInfo) => void;
};
}
interface FeatureUsageInfo {
usage: PostCSSNode;
featureData: {
missing: boolean; // True if feature is completely unsupported
partial: boolean; // True if feature has partial support
};
}Processing Flow:
onFeatureUsage callback tracks feature data for each CSS nodeignorePartialSupport settingThe plugin validates configuration options using Stylelint's built-in validation:
// Internal validation schema (not directly exposed)
interface OptionsValidation {
/** Validation schema for plugin options */
optionsSchema: {
browsers: [(value: any) => boolean]; // Array of functions that check if value is string
ignore: [(value: any) => boolean]; // Array of functions that check if value is string
ignorePartialSupport: (value: any) => boolean; // Function that checks if value is boolean
};
}Validation Behavior:
browsers and ignore options must be arrays of stringsignorePartialSupport must be a boolean valueRecommended usage with warning-level severity:
{
"plugins": ["stylelint-no-unsupported-browser-features"],
"rules": {
"plugin/no-unsupported-browser-features": [
true,
{
"severity": "warning"
}
]
}
}This allows builds to continue while highlighting compatibility issues that may need fallbacks.
For build-breaking compatibility checks:
{
"plugins": ["stylelint-no-unsupported-browser-features"],
"rules": {
"plugin/no-unsupported-browser-features": [
true,
{
"browsers": ["> 1%"],
"severity": "error"
}
]
}
}To ignore specific features with handled fallbacks:
{
"plugin/no-unsupported-browser-features": [
true,
{
"ignore": ["rem", "viewport-units", "css-grid"],
"ignorePartialSupport": true
}
]
}// Required peer dependency
"stylelint": "^16.0.2"// Browser compatibility checking
"doiuse": "^6.0.5"
// CSS parsing and processing
"postcss": "^8.4.32"The plugin handles various error conditions gracefully:
validateOptions for option validationignorePartialSupport setting"type": "module")./lib/index.js