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-mangle-css-class

v0.0.7

Published

Minifies and obfuscates the class names without any modern css modules.

Downloads

20

Readme

Usage

The plugin will generate optimized class name. Configure minimally as follows:

postcss.config.js

import postCSSMangleCSSClass from "postcss-mangle-css-class";

export default {
  plugins: [
    postCSSMangleCSSClass({
      CSSinput: "./style.css",
      export: {
        JSONFile: "./.mangle-css-class/classes.json",
        JSFile: "./.mangle-css-class/classes.js"
      },
      classNameRename: "[name]_[hash]",
      getOutput(renamed, excluded) {
        console.log("output", { renamed, excluded });
      },
    }),
  ],
};

Options

const options = {
    enable: true,
    cwd: process.cwd(), 
    CSSinput: "",
    classNameRename: "[name]_[hash]", 
    hash: {
      type: "random",
      minLength: 5,
      salt: "",
    },
    exclude: {
      classNames: [],
      files: [],
    },
    CSSextensions: [".css", ".sass", ".scss", ".less", ".styl"],
    export: {
      JSONFile: "./.mangle-css-class/classes.json", JSFile: "./.mangle-css-class/classes.js",
      rewrite: false,
    },
    getOutput(renamed, excluded) {}
  };

enable Boolean

Enable/disable plugin. Default: true

cwd String

The current working directory. Default: process.cwd()

CSSinput String, String[]

Establishes which CSS files should be analyzed. e.g: './app.css'

classNameRename String

Set an interpolated string to rename CSS classes. Placeholders compatible:

  • [name] is the class name
  • [hash] is the hash generate. You can set the type to hash.type and the minimum length hash.minLength and optionally hash.salt

e.g.: prefix_[name]_[hash]

hash.type String

Sets the hash type. Possible values: none,random,salt. Default: random

  • none no hash will be applied.
  • random generates an unpredictable random hash.
  • salt generates a predictable random hash. If you need it to be unique; for example, in each project, you can configure: hash.salt

hash.minLength Int

Set the minimum hash length. Default: 5

hash.salt String

If you need to generate unique hashes for each project, you can set salt different in each one. e.g: myProject1. It is only applicable if you have defined hash.type:salt

exclude.classNames String, Regex, String[], Regex[]

CSS classes that will not be replaced. e.g: ["card",/text-/g]

exclude.files String, Glob, String[], Glob[]

CSS classes that are used or found in these files will be ignored. e.g: ["./css-ignore.css","./js/*.js"]

CSSextensions String, String[]

Establishes which files should be parsed as style sheets. Usually used if you set exclude.files. Default: [".css", ".sass", ".scss", ".less", ".styl"]

export.JSONFile String, Boolean

The path of the file where the replaced classes will be saved. Default: "./.mangle-css-class/classes.json". The file will have the following structure:

{
  "card":"ahJ25g",
  "card-body":"Awk1JO"
}

export.JSFile String, Boolean

The path of the file where the replaced classes will be saved in format ESM. Default: "./.mangle-css-class/classes.js". The file will have the following structure:

const card = "ahJ25g";
const card_body = "Awk1JO";
export default {card,card_body}

export.rewrite Boolean

Sets whether to rewrite previously generated files. Default: false.

getOutput(renamed, excluded) Function

A callback that passes the renamed and excluded CSS classes.

Utilities/Methods

init

Required to start; It should commonly be called before any script.

const options_ = {
  cwd: process.cwd(),
  CSSinput: "",
  postcss: null,
  context: {},
};
  • cwd the current working directory. Default process.cwd()
  • CSSinput CSS files that are allowed to be analyzed. e.g: './app.css'
  • postcss PostCSS config. Default: load postcss.config.js in the root project

e.g:

import initMangleCSSClass from "postcss-mangle-css-class/init";

initMangleCSSClass({
  CSSinput: "./style.css"
});

Examples

Each package has its own particularity, which is why we have prepared the following examples of how you could use this plugin. For more details:

If you are using vite ...

You can use the plugin vite-plugin-replace-mangle-css-class Documentation

Others ...

Recommendations

  • Use a single main style sheet as input. e.g:

Before:

// postcss.config.js
import postCSSMangleCSSClass from "postcss-mangle-css-class";

export default {
  plugins: [
    postCSSMangleCSSClass({
      CSSinput: ["./page-1.css","./page-2.css","./page-3.css"]
    }),
  ],
};

After:

// postcss.config.js
import postCSSMangleCSSClass from "postcss-mangle-css-class";

export default {
  plugins: [
    postCSSMangleCSSClass({
      CSSinput: "./all-pages.css"
    }),
  ],
};

MIT License

This project is under license from MIT. For more details, see the LICENSE file.