Giter VIP home page Giter VIP logo

posthtml-minify-classnames's Introduction

posthtml-minify-classnames

NPM Deps Build Coverage

posthtml-minify-classnames is a PostHTML plugin that rewrites classnames and ids inside of html and css files to reduce file size.

Minifying classnames allows you to write verbose classnames in your source code, and distribute a smaller package to your users or application.

Use cases include:

posthtml-minify-classnames supports svg href attributes.

Before:

<html>
  <style>
    #some-id {
      text-transform: uppercase;
    }
    .header__intro {
      color: blue;
    }
    .card--profile {
      background: white;
    }
    .js-overlay {
      display: none;
    }
    #js-button {
      color: blue;
    }
    @media (min-width: 768px) {
      .header__intro {
        color: gray;
      }
    }
  </style>
  <body>
    <svg style="display:none">
      <symbol id="icon-location"><path d=""></path></symbol>
    </svg>
    <h1 id="some-id">Title</h1>
    <p class="header__intro">OMG</p>
    <div class="js-overlay"></div>
    <div id="js-button"></div>
    <div class="card--profile">
      card content
    </div>
    <svg>
      <use href="#icon-location"></use>
    </svg>
    <label for="username">Click me</label>
    <input type="text" id="username">
  </body>
</html>

After:

<html>
  <style>
    #a {
      text-transform: uppercase;
    }
    .a {
      color: blue;
    }
    .b {
      background: white;
    }
    .js-overlay {
      display: none;
    }
    #js-button {
      color: blue;
    }
    @media (min-width: 768px) {
      .a {
        color: gray;
      }
    }
  </style>
  <body>
    <svg style="display:none">
      <symbol id="b"><path d=""></path></symbol>
    </svg>
    <h1 id="a">Title</h1>
    <p class="a">OMG</p>
    <div class="js-overlay"></div>
    <div id="js-button"></div>
    <div class="b">
      card content
    </div>
    <svg>
      <use href="#b"></use>
    </svg>
    <label for="c">Click me</label>
    <input type="text" id="c">
  </body>
</html>

Installation

npm i -D posthtml-minify-classnames

Usage

Note: To use with external sheets, other plugins must be used, like posthtml-inline-assets and posthtml-style-to-file, or other build task plugins.

var posthtml = require('posthtml');
var minifyClassnames = require('posthtml-minify-classnames');

posthtml()
  .use(minifyClassnames({
    filter: /^.js-/,
    genNameClass: 'genNameEmoji',
    genNameId: 'genNameEmoji',
  }))
  .process(`
    <style>
      #foo { color: red }
      .bar { color: blue }
    </style>
    <div id="foo" class="bar">baz</div>
  `)
  .then(function(result) {
    console.log(result.html); //=> '<style>#a { color: red } .bar { color: blue }</style><div id="a" class="bar">baz</div>'
  });

Options

filter

Type: regex, Default: /^.js-/

genNameClass & genNameId

Type: string, Default: 'genName'

Available options: 'genName', 'genNameEmoji', 'genNameEmojiString', false

  • 'genName' Generates the smallest possible names
  • 'genNameEmoji' Generates small emoji based names
  • 'genNameEmojiString' Generates random emoji with 3 emojis in each
  • false Preserves names. Use this to ignore ids or classes

Example:

<html>
  <style>
    #๐Ÿšง๐Ÿ•ฅ๐Ÿ‰ {
      text-transform: uppercase;
    }
    .โ˜˜๐Ÿ‘™๐Ÿ“™ {
      color: blue;
    }
    .โฒ๐Ÿ“‚โš— {
      background: white;
    }
    .js-overlay {
      display: none;
    }
    #js-button {
      color: blue;
    }
    @media (min-width: 768px) {
      .โ˜˜๐Ÿ‘™๐Ÿ“™ {
        color: gray;
      }
    }
  </style>
  <body>
    <svg style="display:none">
      <symbol id="๐Ÿ‘‚๐Ÿ—จ๐ŸŒน"><path d=""></path></symbol>
    </svg>
    <h1 id="๐Ÿšง๐Ÿ•ฅ๐Ÿ‰">Title</h1>
    <p class="โ˜˜๐Ÿ‘™๐Ÿ“™">OMG</p>
    <div class="js-overlay"></div>
    <div id="js-button"></div>
    <div class="โฒ๐Ÿ“‚โš—">
      card content
    </div>
    <svg>
      <use href="#๐Ÿ‘‚๐Ÿ—จ๐ŸŒน"></use>
    </svg>
    <label for="๐Ÿป๐Ÿ”๐Ÿ™">Click me</label>
    <input type="text" id="๐Ÿป๐Ÿ”๐Ÿ™">
  </body>
</html>

Future: Option to define own generator function.

Contributing

See PostHTML Guidelines and contribution guide.

License MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.