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

css-trimmer

v0.1.0

Published

`css-trimmer` identifies the unused properties in your CSS.

Downloads

2

Readme

css-trimmer

css-trimmer identifies the unused properties in your CSS.

Wait, doesn't Chrome DevTools do that already?

Yes, kinda. Here are two reasons for this tool:

The Coverage panel in Chrome DevTools will consider any rule (.some-rule { ... }) as "used" if any node on the page uses the selectors. It doesn't catch if a property within a rule is always superceded by some other rule. In other words, a rule is either all-covered or all-uncovered.

For example, the property in div {} should be considered uncovered, but Chrome DevTools doesn't know.

css-trimmer does:

=========
file:///Users/cjamcl/src/me/css-trimmer/test/fixtures/1/index.html (3 unused declarations)
=========
:5 div { color: red; /* unused! */ }
:13 .never-used { color: blue; /* unused! */ }
:14 .never-used { display: block; /* unused! */ }

-----
total unused declarations: 3

Additionally, this tool checks style coverage by considering many variants of a page, providing general strategies (try this viewport, try this color scheme ...), and also provides a way to configure your own variants.

Usage

Package is available on npm as css-trimmer.

css-trimmer [urlsOrFiles] <options>
css-trimmer path/to/page.html path/to/page2.html --viewports 500,500
css-trimmer https://www.example.com

Options:
  --help                    Show help                                  [boolean]
  --version                 Show version number                        [boolean]
  --color-scheme            Add a collection while emulating
                            `prefers-color-scheme` to the value given   [string]
  --config-path             Loads these options from disk. Supports .js and
                            .json                                       [string]
  --debug                   Generate debug data, such as which collections were
                            redundant                                  [boolean]
  --disable-default-config  Disable loading of the default configuration
                                                                       [boolean]
  --only-collections        Collections to process - all others are skipped
                                                                         [array]
  --output                  Format for the generated the report
                                     [choices: "json", "text"] [default: "text"]
  --quiet                   Suppress logging output to stderr
                                                      [boolean] [default: false]
  --skip-collections        Collections to skip - all others are processed
                                                                         [array]
  --viewports               viewports "width,height". Adds a collection while
                            emulate each viewport.                       [array]

*.js configs can define afterNavigation(context), which provides access to the puppeteer page handler. It runs before any collection for a URL.

Besides the CLI, there is the Node API.

const cssTrimmer('css-trimmer');
const context = await cssTrimmer.start(); // see src/css-trimmer.js for context type definition

await context.navigate('https://www.example.com');
await context.collect('https://www.example.com basic');

const report = context.finish(context);

// Or, use the same runner that the CLI uses:
const report = await cssTrimmer.run(urls, options);

How it works

Uses Puppeteer. Loads each URL and collects the active style declarations for every relevant node in the DOM. The range within the style sheet for each applied property is recorded. Every mass-reading of the DOM is called a collection. After all the URLs and each variant of collections runs, the recorded property sets are combined. Every property not in this set is flagged.

Every collection takes a few seconds, and with all the URLs and variants the program can take painfully long. Running with --debug will determine which of the collections are redundant, and --only-collection will allow you to skip the rest.

Future work

  • Flesh out afterNavigation, maybe provide more lifecycle methods / easy way to add more variants. Using against lighthouse report should help flesh this out
  • Variables
  • Upstream property-level coverage to Chrome DevTools
  • Output text format doesn't work with minified CSS