postcss-class-name-shortener
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

PostCSS Class Name Shortener

Build Status Coverage Status npm npm GitHub issues

PostCSS plugin that shortens CSS class names to optimize website performance.

The plugin will produce an object with all mapped class names and return it via the callback.

// INPUT
.long-class-name-that-just-sets-the-text-color-to-black {
  color: black;
}
// OUTPUT
.a {
  color: black;
}

This plugin uses css-shortener under the hood.

Usage

npm install --save postcss-class-name-shortener
const classNameShortener = require('postcss-class-name-shortener');
const fs = require('fs');

postcss([
  [
    'postcss-class-name-shortener',
    {
      outputMapCallback: map => {
        console.log(JSON.stringify(map));
        // You can return a promise
        return new Promise((resolve, reject) => {
          fs.writeFile('cssMap.json', JSON.stringify(map), err => {
            if(err) reject(err);
            else resolve();
          });
        })
      }
      // Optionally disable shorting of class names in dev environment
      disable: process.env.NODE_ENV === 'development'
    }
  ]
])

The map object will look like this:

{
  "long-class-name-that-just-sets-the-text-color-to-black": "a"
}

Options

See PostCSS docs for examples for your environment.

Package Sidebar

Install

npm i postcss-class-name-shortener

Weekly Downloads

68

Version

2.0.0

License

MIT

Unpacked Size

20.5 kB

Total Files

12

Last publish

Collaborators

  • mbrandau