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

postcss-comments

v1.0.0

Published

Postcss plugin to prepend or append comments to CSS rules

Downloads

31

Readme

PostCSS Comments

PostCSS plugin to prepend and append comments to CSS rules.

Deployment Status   Coverage Status   npm version

Description

Some PostCSS plugins require to add comments to the CSS code to be able to perform their work (e.g. RTLCSS or PostCSS RTLCSS). But if the CSS code is coming from a third-party library or a CSS-in-JS framework it is impossible to modify the CSS source to add comments. In these cases, postcss-comments could be helpful to prepend or append comments to CSS rules selecting them using strings or regular expressions.

Install

npm

npm install postcss-comments --save-dev

yarn

yarn add postcss-comments -D

Usage with commonJS

const postcss = require('postcss');
const postcssComments = require('postcss-comments');

const result = postcss([
    postcssComments({
        rulesMatchers: [
            /* rulesMatchers */
        ]
    })
]).process(cssInput);

const commentedCSS = result.css;

Usage with ES6 modules

import postcss from 'postcss';
import postcssComments from 'postcss-comments';

const result = postcss([
    postcssComments({
        rulesMatchers: [
            /* rulesMatchers */
        ]
    })
]).process(cssInput);

const commentedCSS = result.css;

Usage in Webpack with postcss-loader

rules: [
    {
        test: /\.css$/,
        use: [
            { loader: 'style-loader' },
            { loader: 'css-loader' },
            {
                loader: 'postcss-loader',
                options: {
                    postcssOptions: {
                        plugins: [
                            postcssComments({
                                rulesMatchers: [
                                    /* rulesMatchers */
                                ]
                            })
                        ]
                    }
                }
            }
        ]
    }
]

Rules Matchers

rulesMatchers consist on an array of objects, each one describing one matcher.

{
    matcher: string | RegExp | (string | RegExp)[];
    prepend?: string;
    append?: string;
}

Examples

Input

.test1, .test2 {
    color: #666;
    padding-right: 20px;
    width: 100%;
}

.link {
    color: red;
}

.link:hover {
    color: red;
}

.link:visited {
    color: red;
}

.test-class {
    text-align: left;
    height: 100px;
}

Using string rule matchers

String matchers will match a rule if the entire selector of the rule matches exactly with the string.

postcssComments({
    rulesMatchers: [
       {
            matcher: ['.link', '.test-class'],
            prepend: 'Using an array of string matchers'
       },
       {
            matcher: '.link:visited',
            append: 'Using a unique string matcher'
       }
    ]
})

Output

.test1, .test2 {
    color: #666;
    padding-right: 20px;
    width: 100%;
}

/* Using an array of string matchers */
.link {
    color: red;
}

.link:hover {
    color: red;
}

.link:visited {
    color: red;
}
/* Using a unique string matcher */

/* Using an array of string matchers */
.test-class {
    text-align: left;
    height: 100px;
}

Using RegExp rule matchers

Regular Expressions matchers are more flexible. They allow one to match rules without specifing exactly the string of their selectors using a Regular Expression pattern instead.

postcssComments({
    rulesMatchers: [
       {
            matcher: [/^\.test\d+/, /^\.link:\w+$/],
            prepend: 'Using an array of RegExp matchers'
       },
       {
            matcher: /\.test-\w+$/,
            append: 'Using a single regular expression'
       }
    ]
})

Output

/* Using an array of RegExp matchers */
.test1, .test2 {
    color: #666;
    padding-right: 20px;
    width: 100%;
}

.link {
    color: red;
}

/* Using an array of RegExp matchers */
.link:hover {
    color: red;
}

/* Using an array of RegExp matchers */
.link:visited {
    color: red;
}

.test-class {
    text-align: left;
    height: 100px;
}
/* Using a single regular expression */

Notes

  1. String matchers and Regular Expression matchers can be mixed in the same macther array.
  2. Only the first matcher is used. If a rule matches a matcher, the append or prepend comments are inserted and it doesn‘t continue checking the next matchers on the array.
  3. Regular Expressions matchers cannot have flags, if you set flags, they will be ignored.
  4. If you do not use PostCSS, add it according to official docs and set this plugin in settings.