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

@greenpeace/dashdash

v1.2.1

Published

Script to run just after SCSS is compiled into CSS, to convert declarations in any selectors ending with --some-string-- into the same declarations but with a variable around the value. These declarations are added to the selector preceding the --some-str

Downloads

2,529

Readme

dashdash

Planet4

PostCSS plugin for shorthand CSS variables syntax.

Script to run just after SCSS is compiled into CSS, to convert declarations in any selectors ending with --some-string-- into the same declarations but with a variable around the value. These declarations are added to the selector preceding the --some-string-- part, which will be created if it doesn't exist yet.

.foobar {
  --some-string-- {
    color: green;
    background-color: white;
  }
}

gets transformed to

.foobar {
    color: var(--some-string--color: green);
    background-color: var(--some-string--background-color: white);
}

Just to get it out of the way: yes, it's a hack. It relies on the shorthand syntax being recognized as a valid custom HTML element. Still seems a smaller problem than having the same long word scattered in half of the declarations, as well as the echoing property names at the end.

I looked for an alternative way to implement this with "vanilla" SCSS, but alas, they all involved rewriting the declarations to something inconvenient. I specifically looked for a way to have this shorthand as a wrapper around lines of existing CSS without needing to change them. I came to the current solution pretty much be elimination. SCSS doesn't support modifying declarations this way. So instead I implemented it on the result of SCSS, so the script only sees the combined selectors. It strips the prefix from the selector, which gives it the "normal" selector to add the declarations to.

You can also include any element state pseudo selectors, and they will behave the same way as they would when used directly in the selector. In that case the state is included in the generated name. If there is more than one state, the first one is used for the name (but better to give them separate variables, which will allow them to be changed at a later point without needing a code change).

.foobar {
  --some-string-- {
    color: green;
    &:hover {
      background-color: white;
    }
    &:active, &:visited {
      background-color: yellow;
    }
  }
}

gets transformed to

.foobar {
    color: var(--some-string--color: green);
}
.foobar:hover {
    background-color: var(--some-string--hover--background-color: white);
}
.foobar:active, .foobar:visited {
    background-color: var(--some-string--active--background-color: white);
}

If the element's selector is sufficiently descriptive, you can use the _-- shorthand to have the prefix be generated from it. The name generation simply takes anything that is not alphanumeric or a dash, and turns it into maximum 2 dashes. This will throw an error when used on multiple selectors.

.foobar {
  _-- {
    color: green;
    &:hover {
      background-color: white;
    }
  }
}

#header .nav-item {
  _-- {
    color: red;
  }
}

gets transformed to

.foobar {
    color: var(--foobar--color: green);
}
.foobar:hover {
    background-color: var(--foobar--hover--background-color: white);
}
#header .nav-item {
    color: var(--header--nav-item--color: red);
}

Obviously this will lead to naming conflicts in some cases and is really only advisable on single class or element selectors.

Usage with webpack

Ideally you should run this plugin as early as possible in the build, so the other plugins don't need to work with the shorthand form. This could cause some issues with for example duplicate detection.

module.exports = {
  module: {
    // ...
    rules: [
      // ...
      {
        test: /\.(sass|scss)$/,
        use: [
            // ...
          {
            loader: 'postcss-loader',
            options: {
              ident: 'postcss',
              plugins: () => [
                dashDash(),
                // ...other plugins
              ],
              sourceMap: true
            }
          },
          {
            loader: 'sass-loader',
          }
        ]
      },
    ]
  },
}

If you have stylelint set up, you probably need to allow the format with the following rule.

{
    "selector-type-no-unknown": [true, {
	  "ignoreTypes": ["/^--[a-z]\\w*(--?[a-z0-9]+)*--$/", "_--"]
	}]
}

@media queries

If the declaration is within a media query, a prefix will be generated from the media query's conditions. This means queries with multiple conditions result in very long names, which might or might not be a problem. You can pass an alias map using mediaQueryAliases, where each key is the media query's conditions (e.g. (min-width: 1200px)), the value is the alias. By default, this it is added at the start of the variable name.

Nested media queries are not handled yet, in that case it will only use the innermost for the name generation.