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

cogs-transformer-local-css

v4.1.1

Published

A local CSS transformer for Cogs.

Downloads

12

Readme

cogs-transformer-local-css

A local CSS transformer for Cogs inspired by this blog post and works similarly to this webpack loader.

If you haven't read this blog post, you'll want to do that first. It's a quick read and it explains the benefits of freeing yourself from the grips of global, collision-prone CSS class names.

This transformer takes CSS files in, inspects the contained class/id references, renames them uniquely and stores a map of your human-readable names in a simple JSON file.

Here's what you'll want to add to your Cogs config...

cogs.js

module.exports = {
  ...
  pipe: [
    ...
    {
      name: 'local-css',
      only: 'src/my/css/**/*.css',

      // All of the following options are...optional.
      options: {

        // The base directory to store CSS file paths relative to. Default: '.'
        base: "src/my/css",

        // In debug mode, classes will be prepended with their path and
        // human-readable class name. This is highly recommended for development
        // but should be disabled in production. Default: false
        debug: true,

        // The location to store the JSON class name map.
        // Default: class-names.json
        target: "build/my-class-names-map.json",

        // Should not be necessary, but in the case of multiple projects with
        // identical relative CSS file paths being used on the same page, simply
        // insert a unique salt here to break the hash collision. Default: ''
        salt: 'my-unique-salt',

        // The length of the unique class identifier. The higher the number, the
        // lower the chance for a hash collision. Default: 5
        uidLength: 12,

        // How long to wait (in milliseconds) after a change is made before
        // saving the JSON class name map. Default: 500
        debounce: 1000
      }
    }
    ...
  ]
  ...
}

So after this is set up and I run cogs, and assuming I have I'll have a build/my-class-names-map.json file that looks something like...

{
  "one/of/my/css/files": {
    "one-of-my-classes": "abcdef123456",
    "another-class": "zyx123abc456"
  }
}

Now we can load/parse this file with anything that supports JSON (everything) and access the local-css-ified names while references the human-readable class names we want, all without having to sweat global collisions.

Build Status