projectwallace / css-analyzer

Analytics for CSS
https://www.projectwallace.com
MIT License
314 stars 14 forks source link

Report media query features #261

Open bartveneman opened 1 year ago

bartveneman commented 1 year ago

The 2022 Web Almanac made an excellent point about media feature usage. See https://almanac.httparchive.org/en/2022/css#responsive-design List out all features, like screen, min-width, prefers-reduced-motion

romainmenke commented 1 year ago

A media query parser that might help with this : https://github.com/csstools/postcss-plugins/tree/main/packages/media-query-list-parser

import { parse } from '@csstools/media-query-list-parser';

const mediaQueryList = parse('screen and (min-width: 300px), (50px < height < 30vw)');

mediaQueryList.forEach((mediaQuery) => {
    mediaQuery.walk((entry, index) => {
        // Type of `node`
        if (isMediaFeature(entry.node)){
            console.log(entry.node.getName());
        }
    });
});

Also interesting to assign a complexity score to media queries :


edit : but I now see that css-tree is used. So it will likely make more sense to wait until they improve their support for newer media query features.

bartveneman commented 1 year ago

Thanks for chiming in! Adding another dependency is indeed not high on my list for a feature like this, so I'll see if/when CSSTree can give me a similar setup.

Also interesting to assign a complexity score to media queries :

Definitely! You're already familiar with my thoughts on CSS Complexity?

CSSTools is coming along nicely (I've been keeping an eye, mostly via Twitter), didn't realise the parser etc. were in the postcss-plugins repo. Starred and making sure I'm not missing updates 😉