Giter Club home page Giter Club logo

postcss-filter-rules's Introduction

PostCSS Filter Rules

npm version License Build

PostCSS plugin that filters rules with a callback function on each selector. Can be used to filter out individual rules or remove all rules besides those you wish to keep.

Installation

This plugin supports PostCSS 8 as of version 0.7.0. If you use older versions of PostCSS, please use version 0.6.x of this package.

npm install --save-dev postcss postcss-filter-rules

Usage

postcss([
    require('postcss-filter-rules')(options)
])

See PostCSS docs for examples for your environment.

Options

filter
Type: Function

Function used to filter selectors. Called for each selector in a rule.

  • Selectors are kept if the function returns a truthy value, otherwise they are removed.
  • If all of the selectors for a rule are removed, the rule is also removed.
  • If all rules within an at-rule are removed, the block itself is also removed.

The function receives two arguments, selector and parts:

  • selector (String): The selector, as authored in CSS
  • parts (Array): An array of elements, classes, IDs and pseudo-classes in the selector. Can be used to quickly search for the existence of classes or IDs.
    i.e. for the selector .a + strong.b, the argument will be: [".a", "strong.b"]

For example, to keep only the selectors with the .styleguide class:

{
	filter: (selector, parts) => parts.includes('.styleguide')
}

With the input CSS:

.styleguide span,
.button span {
	color: red;
}
.button {
	color: blue;
}

Will output:

.styleguide span {
	color: red;
}

splitFunction
Type: Function

A custom callback that can be used to generate the parts argument of the callback. If one is not provided, the default callback is used. This callback builds an AST-tree using postcss-selector-parser, but there is no restriction on what this function can return.

keepAtRules
Type: Array (Optional)
Default: ['charset', 'import', 'keyframes']

By default, @font-face and any empty at-rules (after filtering) are removed. To keep specific at-rules, provide an array of names to this option. For example:

{
	keepAtRules: ['font-face', 'import']
}
  • To keep all at-rules, use the value true
  • To discard all at-rules, use an empty array []

postcss-filter-rules's People

Contributors

lachlanmcdonald avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.