npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mapbox/postcss-html-filter

v3.0.0

Published

Filter CSS through HTML, removing selectors that do not apply to the HTML

Downloads

162

Readme

@mapbox/postcss-html-filter

Build Status

Filter CSS through HTML, removing selectors that do not apply to that HTML.

Parses HTML with Cheerio — using its jQuery-like selector queries — to determine which selectors in the CSS correspond to actual elements on the page. Removes selectors that have no corresponding elements, rules that have no corresponding selectors, at-rules containing no corresponding rules, etc.

Also, for good measure, runs the CSS through postcss-discard-unused, which removes unused @counter-style, @keyframes, and @font-face at-rules.

Installation

npm install @mapbox/postcss-html-filter

Usage

Follow the instructions for your PostCSS runner.

This example uses PostCSS's Node API:

const postcss = require('postcss');
const postcssHtmlFilter = require('@mapbox/postcss-html-filter');
const fs = require('fs');

const myHtml = fs.readFileSync('path/to/some.html', 'utf8');
postcss()
  .use(postcssHtmlFilter({ html: myHtml })
  .then(result => { /* ... */ });

Options

html

Type: string. Required.

The HTML that you will use to filter your CSS.

Details

  • Before checking if a selector applies to the HTML pseudo-elements are stripped (e.g. ::before, ::first-line). If the selector applies without them, we can assume that it also applies with them, and the selector should be kept. Pseudo-classes (e.g. :first-child, :not(a), :nth-child(3)) are passed to Cheerio when Cheerio can interpret them (using css-select). If the pseudo-class cannot be interpreted by Cheerio, it is stripped before the selector is checked.

Caveats

  • This does not resolve nested selectors (e.g. for SCSS and Less). If you want to give that a shot, feel free to try a PR. Maybe try postcss-resolve-nested-selector.

Is this like UnCSS?

Kind of. This is essentially a simplified version of what UnCSS does. Instead of using PhantomJS or jsdom to load the page, size things, download resources, etc., this module only addresse the core problem of filtering out CSS that is not used in some HTML. This is a low-level module that could be used within other, higher-level projects (e.g. ones that download resources).

Similar projects

  • Another project that uses Cheerio to filter out unused CSS is css-razor.
  • Another PostCSS plugin with similar aims is usedcss.

Development

The tests are very simple. In fact, there's just one Jest snapshot test at the moment, which provides 100% code coverage. We can add more to the CSS and HTML fixtures, as needed, to test other scenarios and code changes; and Jest handles the comparison and offers a nice legible read-out of what went wrong.