Giter Club home page Giter Club logo

purgecss-docs's Introduction

Introduction

Purgecss is a tool to remove unused CSS. It can be used as part of your development workflow. Purgecss comes with a JavaScript API, a CLI, and plugins for popular build tools.

Here are a couple of ways to use Purgecss:

CLI

You can install the CLI in two ways. By installing Purgecss globally or using npx.

Install globally

npm i -g purgecss

Run Purgecss from the terminal:

purgecss --css <css> --content <content> [option]

Use npx

npx allows you to run the CLI locally without installing the package globally.

Install Purgecss as a dev dependency:

npm i -D purgecss

Run Purgecss from the terminal:

npx purgecss --css <css> --content <content> [option]

JavaScript API

Install Purgecss as a dev dependency:

npm i -D purgecss

ES6 with import

import Purgecss from 'purgecss'
const purgecss = new Purgecss({
  content: ['**/*.html'],
  css: ['**/*.css']
})
const purgecssResult = purgecss.purge()

ES5 with require

var Purgecss = require('purgecss')
var purgecss = new Purgecss({
  content: ['**/*.html'],
  css: ['**/*.css']
})
var purgecssResult = purgecss.purge()

Webpack

Install the Webpack plugin as a dev dependency:

npm i -D purgecss-webpack-plugin

Use the plugin in your Webpack config:

const path = require('path')
const glob = require('glob')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const PurgecssPlugin = require('purgecss-webpack-plugin')

const PATHS = {
  src: path.join(__dirname, 'src')
}

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader?sourceMap'
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('[name].css?[hash]'),
    new PurgecssPlugin({
      paths: glob.sync(`${PATHS.src}/*`)
    })
  ]
}

PostCSS

Install the PostCSS plugin as a dev dependency:

npm i -D @fullhuman/postcss-purgecss

Use the plugin in your PostCSS config:

module.exports = {
  plugins: [
    purgecss({
      content: ['./**/*.html']
    })
  ]
}

Gulp

Install the Gulp plugin as a dev dependency:

npm i -D gulp-purgecss

Use the plugin in your Gulpfile:

const gulp = require('gulp')
const purgecss = require('gulp-purgecss')

gulp.task('purgecss', () => {
  return gulp
    .src('src/**/*.css')
    .pipe(
      purgecss({
        content: ['src/**/*.html']
      })
    )
    .pipe(gulp.dest('build/css'))
})

Grunt

Install the Grunt plugin as a dev dependency:

npm i -D grunt-purgecss

Use the plugin in your Gruntfile:

module.exports = grunt => {

  grunt.initConfig({
    purgecss: {
      options: {
        content: ['./src/**/*.html']
      },
      my_target: {
        files: {
          './dist/app.purged.css': './src/app.css'
        }
      }
    }
  })

  grunt.loadNpmTasks('grunt-purgecss')
  grunt.registerTask('default', ['purgecss'])
}

Rollup

Install the Rollup plugin as a dev dependency:

npm i -D rollup-plugin-purgecss

Use the plugin in your Rollup config:

import { rollup } from 'rollup'
import purgecss from 'rollup-plugin-purgecss'

rollup({
  entry: 'main.js',
  plugins: [
    purgecss({
      content: ['index.html']
    })
  ]
})

purgecss-docs's People

Contributors

ahus1 avatar aryehraber avatar calinou avatar coliff avatar cristijora avatar ffloriel avatar gitbook-bot avatar jesseljohn avatar jsnanigans avatar lucaperret avatar lucleray avatar mannil avatar maoberlehner avatar mblancodev avatar mrkuzio avatar niksmac avatar sarahdayan avatar taylorbryant avatar tbjgolden avatar tehpsalmist avatar than avatar valantonini 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.