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

scss-variable

v0.2.3

Published

generate css variable for different css preprocessors

Downloads

7

Readme

scss-variable

Generate css variables fro different css pre-processors. Mainly target for scss/sass.

Installation

npm i -D scss-variable
#or
yarn add -D scss-variable

Usage

Assume you have a varaible.js looks like this

module.exports = {
  ns: {
    property: {
      a: "#fff",
      b: "#ccc"
    }
  }
};

Create another scss-variable.js

const path = require("path");
const generateVariable = require("scss-variable");

generateVariable({
  src: path.resolve(__dirname, "path/to/variable.js"),
  dest: path.resolve(__dirname, "path/to/variable.scss")
});

Then run node scss-variable;

Output will look like this

// variable.scss
$ns-property-a: #fff;
$ns-property-b: #ccc;

Advanced usage

Scss map

// in your variable.js
module.exports = {
  _map_: {
    ns: {
      case: {
        "font-size": "12px"
      }
    }
  }
};

Run node scss-variable

Output will look like this

$ns: (
  case: (
    font-size: 12px
  )
);

To use !default

module.exports = {
  _map_: {
    ns: {
      default: true,
      case: {
        "font-size": "12px"
      }
    }
  }
};

Output will look like this

$ns: (
  case: (
    font-size: 12px
  )
) !default;

map will be emitted after variables

You can refer to your variables in your map

Merge

override is used to provide a new object to merge with src

const path = require("path");
const generateVariable = require("scss-variable");

generateVariable({
  src: path.resolve(__dirname, "path/to/variable.js"),
  dest: path.resolve(__dirname, "path/to/variable.scss"),
  override: {
    property: "value"
  }
});

You can merge variables as well as scss map

// in variable.js
module.exports = {
  a: {
    p: "black"
  },
  _map_: {
    ns: {
      case: {
        "font-size": "12px"
      }
    }
  }
};
// in your external override related js
module.exports = {
  a: {
    p: "red"
  },
  _map_: {
    ns: {
      case: {
        "font-size": "14px"
      }
    }
  }
};
// in scss-variable.js
generateVariable({
  src: path.resolve(__dirname, "path/to/variable.js"),
  dest: path.resolve(__dirname, "path/to/variable.scss"),
  override: require("path/to/override/related/js")
});

Output will be:

$a-p: red;
$ns: (
  case: (
    font-size: 12px
  )
);

Viola!

In case you do not like nesting, flatten property key or even flatten map expression are supported

The case above can be rewritten as following

// in your external override related js
module.exports = {
  "a-p": "red",
  "ns.case": {
    "font-size": "14px"
  }
};

Configuration

src

Source of your variable

dest

Where to emit your file. File extension is needed!

In other word, less or other syntax are possible and actually supported

[override]

Object to override your src

[prefix]

Default: '$'

String to use to prefix the variable (and map) assignment

Basically works like this

`${prefix}${propertyName}`;

'@' for less variables : )

[semi]

Default: true

Whether to add semicolon at the end of variable assignment

[assignToken]

Default: ':'

As the name suggested, used for variable assignment

[separator]

Default: '-'

String used to concat nested property names

`${property}${separator}${property}`;

[mapSeparator]

Default: '.'

When you write map structure as a single property in override (ns.a.b.c)

Only useful for override

// in your variable.js

module.exports = {
  _map_: {
    ns: {
      a: {
        b: {
          "font-size": "12px"
        }
      }
    }
  }
};

In your override, you can refer the above map structure as following

`ns${mapSeparator}a${mapSeparator}b`;

[mapKey]

Default: '_map_'

Key to identify map object

Affected to both src and override

[beforeBody]

Default: ''

String inserted before generated content

`${beforeBody}\n${generatedContent}`;

[afterBody]

Default: ''

String inserted after generated content

`${generatedContent}\n${afterBody}`;

[watch]

Default: false

Watch src to recompile.

[merge]

Default: lodash.merge

Dangerous area. Function used to merge src and override

Signature should look like Object.assign

TODO

  • [x] watch mode
  • [ ] cli
  • [ ] webpack-loader