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-vars-process

v1.0.0

Published

PostCSS plugin extract and replace variables in the css file like sass/less, support legal custom format variables

Downloads

3

Readme

PostCSS Vars Process Build Status

PostCSS plugin to extract and replace variables in the css file, support legal custom format variables. Extract variables array for secondary development or other use, and replace variables based on the given value.

input

.foo {
    font-size: $(size);
    color: $(color);
}
// variable.js
module.exports = {
    size: '12px',
    color: '#fff'
}
postcss([ require('postcss-vars-process')({
    values: './variable.js'
})])

output

.foo {
    font-size: 12px;
    color: #fff;
}
// extract variables
['size', 'color']

Usage

postcss([ require('postcss-vars-process')(options) ])

See PostCSS docs for examples for your environment.

Installation

npm install postcss-vars-process

options

.text {
    font-size: $(fontSize);
    color: $(color);
    line-height: 1;
    background: $(color) url('$(logo)');
}
.error {
    color: $(errorColor|#f00);
}

Note: without special instructions the following cases all uses this CSS template .

values

  • Type: String/Object
  • Default: null
  • Required: false
  • Description: The Object corresponding to the variables in css, used to replace the variable in css,like scss

When the value is String, only the file address of type .js or .json is accepted, otherwise Object is accepted as the value.

pattern

  • Type: RegExp/String
  • Default: /\$\(([^()]+)\)/g
  • Required: false
  • Description: Regular rules for matching variable templates

Used to match custom template expressions in CSS. If there is a capture group, the expression corresponding to $1 by default, otherwise the whole matching expression is extracted. If there is no capture group, splitKey is ignored by default。

// with capture group
postcss([ require('postcss-vars-process')({
    values: {
        'fontSize': '14px',
    	'color': '#333',
    	'logo': './img/logo.png',
        'errorColor': '#f10'
    },
    extract(variables){
        // variables: ['fontSize', 'color', 'logo', 'errorColor|#f00']
        // do something with variables
    }
})])

// without capture group
postcss([ require('postcss-vars-process')({
    pattern: //\$\([^()]+\)/g/,
    values: {
        '$(fontSize)': '14px',
    	'$(color)': '#333',
    	'$(logo)': './logo.png',
    	'$(errorColor|#f00)': '#999'
    },
    extract(variables){
        // variables: ['$(fontSize)', '$(color)', '$(logo)', '$(errorColor|#f00)']
        // do something with variables
    }
})])

logType

  • Type: String:<warn|error>
  • Default: warn
  • Required: false
  • Description: Message prompt when variables cannot be resolved

When a variable cannot be resolved, this specifies whether to throw an error or log a warning. In the case of a warning, the unknow variables will be replaced with an empty string.

splitKey

  • Type: String
  • Default: |
  • Required: false

All parameters are added directly after the variable via splitKey. The number of parameters is not limited. It is suggested that this attribute be used in conjunction with extract, and it is meaningless to use it alone.

For example:

.test{
     border-color: $(color|2|#f00);
}
// extract 'color|2|#f00', then split it with splitKey |
variable: color
valueType: 2 
defaultValue: #f00

The meaning of the parameters is completely defined by you.

extract

  • Type: Function(Array[])
  • Default: null
  • Required: false

If you need to process the variables, extract must be filled in and must be Function.

// with capture group
postcss([ require('postcss-vars-process')({
    extract(variables){
        // variables: ['fontSize', 'color', 'logo', 'errorColor|#f00']
        let vars = {};
        variables.forEach(item => {
			item = item.split('|');
            vars[item[0]] = item[1] : '';
        });
        // export variables
        fs.writeFile(path.join(__dirname, 'vars.json'), JSON.stringify(vars));
    }
})])

// vars.json
{
    "fontSize": "",
    "color": "",
    "logo": "",
    "errorColor": "#f00"
}