Giter Site home page Giter Site logo

purifycss's Introduction

PurifyCSS

  • Detects which CSS selectors your app is using and creates a file without the unused CSS.

Able to also detect dynamically-loaded CSS selectors in your JavaScript.

PurifyCSS has been designed from the beginning with single-page apps in mind.

This is in addition to working with multi-page apps. PurifyCSS is voodoo magic.

Potential reduction

  • Bootstrap file: ~140k characters.
  • Average Bootstrap usage: ~40% (at most)
  • Minified Bootstrap: ~117k characters.
  • Purified + Minified Bootstrap: ~27k characters

Install

npm install purify-css

#Able to detect

  • Anytime your class name is intact in your code.
Example for the class button-active
  <!-- html -->
  <!-- class directly on element -->
  <div class="button-active">click</div>
  // javascript
  // this example is jquery, but anytime your class name 
  // is together in your javascript, it will work
  $(button).addClass('button-active');
  • Dynamically created classes
Example for the class button-active
  // can detect even if class is split
  var half = 'button-';
  $(button).addClass(half + 'active');
  
  // can detect even if class is joined
  var dynamicClass = ['button', 'active'].join('-');
  $(button).addClass(dynamicClass);
  • All JavaScript frameworks
Example for the class angular-button
  <!-- angular template -->
  <div ng-class="'angular' + '-button'"></div>
Example for the class commentBox
  // react component
  var CommentBox = React.createClass({
    render: function() {
      return (
        <div className="commentBox">
          Hello, world! I am a CommentBox.
        </div>
      );
    }
  });
  React.render(
    <CommentBox />,
    document.getElementById('content')
  );

PurifyCSS detects all JS frameworks. It is voodoo magic.

API

var purify = require('purify-css');

purify(content, css, options, callback);

content

Type: Array or String

Array of filepaths to the files you want to search through for used classes (HTML, JavaScript, Templates, anything that relates to CSS classes)

String of content you want us to look for used classes.

css

Type: Array or String

Array of filepaths to the CSS files you want us to filter.

String of CSS you want us to filter.

##options (optional)

Type: Object
Properties of options object:
  • minify: Set to true to minify. Default: false.

  • output: Filepath to write purified CSS to. Returns raw string if false. Default: false.

  • info: Logs info on how much CSS was removed if true. Default: false.

  • rejected: Logs the CSS rules that were removed if true. Default: false.

##callback (optional)

Type: Function
Example
purify(content, css, options, function(output){
  console.log(output, ' is the result of purify');
});
Example without options
purify(content, css, function(output){
  console.log('callback without options');
});

Command Line Tool

$ npm install -g purify-css
$ purifycss
usage: purifycss <css> <content> [option ...]

options:
 --min                Minify CSS
 --out [filepath]     Filepath to write purified CSS to
 --info               Logs info on how much CSS was removed
 --rejected           Logs the CSS rules that were removed

 -h, --help           Prints help (this message) and exits

At build time

Grunt

Gulp

purifycss's People

Contributors

gheoan avatar henryng24 avatar idlewinn avatar illyism avatar kennyt avatar kuzcoo avatar nokane avatar unwitting avatar

Watchers

 avatar

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.