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

postcss-alter-property-value

v1.1.3

Published

Alter your css declarations.

Downloads

760

Readme

postcss alter property value

Alter your css declarations.

about

A tool for changing css declarations from a rule based configuration.

  • Change property names
  • Change values
  • Clone a declaration with a new property name

Usage examples.

  • Simulate flexbox not working by removing display: flex
  • Remove all outline usage
  • Correct declaration mouse: pointer to cursor: pointer
  • Simplify background: #ddd to background-color: #ddd if the value is a hex color
  • For all properties, change value from 16px to 1rem

npm

https://www.npmjs.com/package/postcss-alter-property-value

configuration example

Check postcss.config.js for inspiration.

{
  /* optional */
  config: {
    /* add debug info */
    addInfo: true,
  },

  /* required */
  declarations: {      
    '*': {
      /* The *-property will evaluate all properties 
        The tasks for *-property is always executed first.
        This is a relative expensive task, I recommend to only use this if you must.
        */
      task: 'changeValue',
      to: 'translateY(2px)',
      whenValueEquals: 'translateY(10px)'
    },
    /* set all font-families to this value */
    'font-family': 'sans-serif',                  
    'mouse': {
      /* replace all mouse properties with cursor */
      task: 'changeProperty',
      to: 'cursor'
    },
    'transform': {
      /* clone a declaration and add before this declaration */
      task: 'cloneBefore',
      to: '-webkit-transform'
    },
    'display': {
      /* conditional change value */
      task: 'changeValue',
      to: 'flex',
      whenValueEquals: 'inline-flex'
    },
    'outline': {
      /* remove all outlines */
      task: 'remove'
    },
    'color': {
      /* replace all color names ending with blue */
      task: 'changeValue',
      to: 'orange',
      whenRegex: {
        value: 'blue$',
        flags: 'i' // ignore case sensitivity
      }
    },
    'font-size': {
      /* change to 2rem when value is a rem unit */
      task: 'changeValue',
      to: '2rem',
      whenRegex: {
        value: 'rem',
        flags: 'i'
      }
    },
    'border': [
      /* list of tasks for border property */
      {
        /* change border: 1px solid black
                to border: 1px solid #000 */
        task: 'changeValue',
        to: '#000',
        whenRegex: {
          mode: 'partial',
          value: 'black',
          flags: 'i'
        },
      }, {
        /* change border: 1px solid #000
                to border: 2px solid #000 */
        task: 'changeValue',
        to: '2px',
        whenRegex: {
          mode: 'partial',
          value: '1px',
          flags: 'i'
        }
      }
    ],
    'background': {
      /* simplify background to background-color 
      if value is a hex */
      task: 'changeProperty',
      to: 'background-color',
      whenRegex: {
        value: '^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$',
        flags: 'i'
      }
    }
  } // end declarations
}

A css file with these rules

body {
    font-family: Helvetica;
    background: #FFD700;
    mouse: pointer;
    max-width: 900px;
    margin: 0 auto;
    color: LightbluE;
}

p {    
    font-size: 1rem;
    font-family: Arial;
    background: #fff;
    padding: 1rem;    
    color: dodgerblue;    
    outline: 2px dashed red;
    border: 1px solid black;
    display: inline-flex;
    transform: translateY(10px);    
}

Is modified to

body {
    font-family: sans-serif /* papv - changeValue from [Helvetica] */;
    background-color: #FFD700 /* papv - changeProp from [background] */;
    cursor: pointer /* papv - changeProp from [mouse] */;
    max-width: 900px;
    margin: 0 auto;
    color: orange /* papv - changeValue from [LightbluE] */;
}

p {    
    font-size: 2rem /* papv - changeValue from [1rem] */;
    font-family: sans-serif /* papv - changeValue from [Arial] */;
    background-color: #fff /* papv - changeProp from [background] */;
    padding: 1rem;
    color: orange /* papv - changeValue from [dodgerblue] */;
    border: 2px solid #000 /* --papv - changeValue from [1px solid black] */;
    display: flex /* papv - changeValue from [inline-flex] */;
    -webkit-transform: translateY(2px) /* papv - changeValue from [translateY(10px)] */;
    transform: translateY(2px) /* papv - changeValue from [translateY(10px)] */;
}

development

  • Git clone the project or download it
  • npm install
  • npm start
  • Open a browser and go to http://localhost:3456/
  • Insert your styles in demo/index.css
  • Open and make changes to the configuration in postcss.config.js
  • Open dev tools in browser and inspect the elements

command line usage example

css-changes.js

const fs = require('fs');
const postcss = require('postcss');
const plugin = require('postcss-alter-property-value');
const configuration = {
    /* your configuration */
    declarations: {
        'background-color': {
                task: 'changeValue',
                to: '#fff',
                whenValueEquals: 'white'
            }
    }
};
fs.readFile('my.css', (err, css) => {
    postcss([plugin(configuration)])
        .process(css, { from: 'my.css', to: 'my-new.css' })
        .then(result => {
            fs.writeFile('my-new.css', result.css);
        });
});

Run in console/terminal where my-new.css is generated from my.css and given configuration.

node css-changes.js