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

grunt-unclassify

v0.0.5

Published

Remove unused css classes in your html.

Downloads

81

Readme

grunt-unclassify

NPM Version Circle CI

Remove unused css classes in your html.

Getting Started

This plugin requires Grunt.

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-unclassify --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-unclassify');

The "unclassify" task

Overview

In your project's Gruntfile, add a section named unclassify to the data object passed into grunt.initConfig().

grunt.initConfig({
  unclassify: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
})

NOTE: This plugin uses Cheerio to parse your HTML. Besides removing classes, it may also attempt to fix unclosed/stray tags and other formatting issues.

Options

options.stylesheets

Type: Array/Path

An array of paths to stylesheets. You can use file globbing patterns. Valid examples:
['path/to/styles.css'] OR ['path/to/**/*.css']

options.customClasses

Type: Array/String
Default value: []

Array or space-separated list of additional classes to protect (not remove from html).

options.jsClasses

Type: Array/Boolean
Default value: true

Array of prefixes to protect (not remove). Default of true will result in ['js-']. Setting to false will disable it. Use this to preserve classes that are only used by your javascript (and are not in the css).

options.bootstrapClasses

Type: Array/Boolean
Default value: false

Array of bootstrap javascript classes to protect (not remove). Setting to true will result in a default list maintained by this plugin. Last updated for Bootstrap v3.3.1.

options.foundationClasses

Type: Array/Boolean
Default value: false

Array of foundation javascript classes to protect (not remove). Setting to true will result in a default list maintained by this plugin. Last updated for Foundation 5.

options.html5bpClasses

Type: Array/Boolean
Default value: false

Array of HTML5 Boilerplate classes to protect (not remove). Setting to true will result in a default list maintained by this plugin. Last updated for HTML5BP v4.3.0.

options.filter

Type: Function

Function to protect classes. Classes that were not found in the css will run through this function. Return true to keep the class, false to remove the class. Parameters provided (className, classes).

options.knockout

Type: Boolean
Default value: false

Determines whether to search <script type="text/html"> templates. Does not affect classes in css: bindings, yet.

options.dry

Type: Boolean
Default value: false

Will not write any files. Simulated results will be printed in the terminal. Highly recommended to run like this first to identify non-css classes. Classes that are used by javascript but are not in your css will be removed if not accounted for using the options above.

options.overwrite

Type: Boolean
Default value: false

Files will be overwritten instead of written to dest. Disabled when options.dry is true.

Usage Examples

Single File

grunt.initConfig({
  unclassify: {
    options: {
      stylesheets: ['assets/css/**/*.css']
    },
    files: {
      'dist/index.html': 'dev/index.html'
    }
  },
})

Multiple Files

grunt.initConfig({
  unclassify: {
    options: {
      stylesheets: ['assets/css/**/*.css']
    },
    files: {
      'dist/templates/': ['dev/templates/*.html']
    }
  },
})

Multiple Files (Expand)

grunt.initConfig({
  unclassify: {
    options: {
      stylesheets: ['assets/css/**/*.css']
    },
    files: [{
      expand: true,
      cwd: 'dev/templates',
      src: ['*.html'],
      ext: '.html',
      dest: 'dist/templates/'
    }]
  },
})

Overwrite Existing Files

grunt.initConfig({
  unclassify: {
    options: {
      stylesheets: ['assets/css/**/*.css'],
      overwrite: true
    },
    src: ['dev/templates/**/*.html']
  },
})

All options

grunt.initConfig({
  unclassify: {
    options: {
      stylesheets: ['assets/css/**/*.css'],
      customClasses: ['apples', 'bananas'], // array syntax
      jsClasses: ['js-', 'is-'],
      bootstrapClasses: true,
      foundationClasses: true,
      html5bpClasses: 'apples bananas', // string syntax
      filter: function(className, classes) {
        return className === 'keep-me' ? true : false;
      },
      knockout: true,
      dry: true,
      overwrite: true,
    },
    files: [{
      expand: true,
      cwd: 'dev/templates',
      src: ['**/*.html'],
      ext: '.html',
      dest: 'dist/templates/'
    }]
  },
})

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2014-12-11 v0.0.5 Do not output file names unless classes were removed.
  • 2014-12-11 v0.0.4 Make console output less verbose.
  • 2014-12-03 v0.0.3 Update readme.
  • 2014-12-02 v0.0.2 Fix reading classes in media queries.
  • 2014-12-02 v0.0.1 Initial release.

License

Copyright (c) 2014 Marius Craciunoiu. Licensed under the MIT license.