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

palettier

v2.0.0

Published

Generate css variables and json from js

Downloads

36

Readme

Palettier

npm version License: MIT

Palettier is a tool designed to generate various output formats from a single source of design tokens.

Basically it transforms this:

/* tokens.js */
const color = "rgb(255, 255, 255)";

const background = {
  100: tinycolor(color).toRgbString(),
  50: tinycolor(color).setAlpha(0.5).toRgbString(),
  25: tinycolor(color).setAlpha(0.25).toRgbString(),
};

const color = {
  background,
};

const tokens = {
  color,
};

export default tokens;

Into this:

/* palette.json */
{
  "color": {
    "background": {
      "25": "rgba(255, 255, 255, 0.25)",
      "50": "rgba(255, 255, 255, 0.5)",
      "100": "rgb(255, 255, 255)"
    }
  }
}
/* palette.css */
:root {
  --color-background-25: rgba(255, 255, 255, 0.25);
  --color-background-50: rgba(255, 255, 255, 0.5);
  --color-background-100: rgb(255, 255, 255);
}

And more!

Table of Contents

Usage

Installation

Run Palettier without installation via npx:

npx palettier

Install Palettier globally using npm:

npm install -g palettier

Or add it to your project as a dev dependency:

npm install --save-dev palettier

Configuration

There is two ways to pass the config.

Using a Config file:

  • --config: Path to a JSON or JavaScript configuration file.
palettier --config path/to/config.json

Example configuration file:

{
  "src": "path/to/tokens.js",
  "dist": "path/to/output",
  "transform": [
    ["json", "palette.json"],
    ["cssVariables", "palette.module.css"]
  ],
  "verbose": true
}

Or using command-line arguments:

  • --src: Source file containing the token definitions.
  • --dist: Output directory for the generated files.
  • --transform: Transformation definitions in the format type:filename:options.
  • --verbose: Enable verbose logging.
palettier --src path/to/tokens.js --dist path/to/output --transform json:palette.json --transform cssVariables:palette.module.css --verbose

Also, if you run Palettier without or with minimal arguments, it uses and extends the default configuration:

{
  "src": "./index.js",
  "dist": "./",
  "transform": [
    ["json", "palette.json"],
    ["cssVariables", "palette.module.css"]
  ],
  "verbose": false
}

Transforms

Palettier supports several built-in transforms:

  • json: Converts tokens to a JSON file.
  • cssVariables: Converts tokens to CSS variables.
    • Option className
      • Wraps variables with a specified class (default is :root).
  • scss: Converts tokens to SCSS variables.
    • Option className
      • Wraps variables with a specified class (default is no class, prints without a wrap).
  • jsObject: Converts tokens to a JavaScript object.
    • Option jsExportType:
      • default: Exports as export default.
      • named: Exports as export { palette } (default, preferred and recommended way).
      • commonjs: Exports as module.exports.

You can also implement custom transforms by creating a function that accepts tokens and returns transformed content:

// config.js
function customJsonTransformer(tokens, rootObjectKey, childObjectKey) {
  return JSON.stringify(
    {
      [rootObjectKey]: {
        [childObjectKey]: tokens,
      },
    },
    null,
    4,
  );
}

export default {
  src: "./tokens/index.js",
  dist: "./out/",
  transform: [[customJsonTransformer, "palette-custom.json", "root", "child"]],
  verbose: true,
};

Examples

Example token file

const tokens = {
  colorPrimary: "#3498db",
  fontSizeBase: "16px",
  spacing: {
    small: "8px",
    medium: "16px",
    large: "24px"
  }
};

export default tokens;

Example configuration file

{
  "src": "tokens.js",
  "dist": "dist",
  "transform": [
    ["json", "palette.json"],
    ["cssVariables", "palette.module.css"],
    ["scss", "palette.scss"],
    ["jsObject", "palette.js:commonjs"]
  ],
  "verbose": true
}

More examples

For more examples, please see the examples/ folder of the repo.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/my-new-feature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin feature/my-new-feature).
  5. Create a new pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.