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

tokens-to

v0.0.5

Published

Converter for css and js tokens

Downloads

9

Readme

It is somewhat cumbersome to prepare style tokens across design systems. Design and Style tokens can be defined various ways. However, once defining style tokens in Javascript(or Typescript), it is quite tedius and inefficient to write that again in other format such as css and json to be delivered or reused. tokens-to addresses that problem to ease the pain.

[Notice]

It was commonjs module before 0.0.5, but I decided to move on to es module. The commonjs version will be archived on branch. As the most of tokens work with UI frameworks/libaries which utilizes es module.

Table of Contents

Installation

Install the library using npm or yarn:

npm install --save-dev tokens-to

Usage

Example

Let's say there is a js token like following.

export const colors = {
  whites: [
    '#fffff0',
    '#fffff1'
    '#fffff2'
  ]
}

And this is what you get after the conversion.

:root {
  --colors-whites-0: #fffff0;
  --colors-whites-1: #fffff1;
  --colors-whites-2: #fffff2;
}

When there are multiple tokens like following,

export const colors = {
  whites: [
    '#fffff0',
    '#fffff1'
    '#fffff2'
  ]
}

export const curves = {
  card: [
    10,
    20,
    30,
  ]
}

And then this is what you get after the conversion.

:root {
  --colors-whites-0: #fffff0;
  --colors-whites-1: #fffff1;
  --colors-whites-2: #fffff2;
  --curves-card-0: 10;
  --curves-card-1: 20;
  --curves-card-2: 30;
}

To Do Before converting

Module Exports

To be converted into css, every source js module should export tokens with esm "export" as following. The "export default" will be converted into variables with prefix "default".

export { colorTokens, typographyTokens };

Please keep this form(⬆️) of exporting object to get expected output files.

Configurations


To configure converting to keep consistency and reusability, you can create tokens-to.config.json

tokens-to.config.json

{
  "css": {
    "sources": ["./tokens/*.js"],
    "outFileName": "cssvariables",
    "outDir": "./dist/css",
    "bundled": true,
    "selector": ":root",
    "prefix": ""
  },
  "json": {
    "sources": ["./tokens/*.js"],
    "outFileName": "cssvariables",
    "outDir": "./dist/json",
    "bundled": true
  }
}

Configuration types and descriptions

  • sources: string[]; //Sources to be converted. Patterns are allowed.
  • outFileName: string; //Output file name, in case of converting into single css file(bundled).
  • outDir: string; //Directory for output files.
  • bundled: boolean; //Whether output file is bundled into single file or not. If false, each sources file is converted into single css file. and outfile option is ignored.
  • selector: string; //Css selector that wraps css variables.
  • prefix: string; //Prefix to be prepended in front of each css variables

If configurations are missing, Default configurations are used instead of.

DefaultConfiguration

{
  "css": {
    "sources": [],
    "outFileName": "tokens",
    "outDir": "./dist/css",
    "bundled": true,
    "selector": ":root",
    "prefix": "",
  },
  "json": {
    "sources": [],
    "outFileName": "tokens",
    "outDir": "./dist/json",
    "bundled": true,
  }
}

When manual configurations are provided, configurations from tokens-to.config.json is overrided.

Available manual configurations(command line interface)

arguments

files: optional argument for source files, patterns allowed. If not specified, sources from configurations are used.

options

-o, --outFileName:  output filename.
-d, --outDir:  output directory.
-b, --bundled:  whether output is bundled.
-s, --selector: what css selector is used to define css variables.

Add npm scripts

"tokens-to": "tokens-to",
"js2css": "tokens-to js2css",
"js2json": "tokens-to js2json"

Usages

Run with configurations

npm run tokens-to js2css
npm run js2css
npm run tokens-to js2json
npm run js2json

Help

npm run tokens-to help
npm run tokens-to help js2css
npm run tokens-to help js2json

Run with command line argument

js2css

npm run js2css ./tokens/colors.js
npm run js2css --outDir=./dist/css ./tokens/colors.js
npm run js2css --outFileName=tokens ./tokens/colors.js
npm run js2css --bundled=true ./tokens/colors.js
npm run js2css --selector=html ./tokens/colors.js

js2json

npm run js2json ./tokens/*.js
npm run js2json --outDir=./dist/json ./tokens/colors.js
npm run js2json --outFileName=tokens ./tokens/colors.js
npm run js2json --bundled=true ./tokens/colors.js

Contributing

We welcome contributions to this library! Here's how you can get involved:

  • Bug reports: If you find a bug, please report it on the GitHub issue tracker.
  • Feature requests: If you have a feature request, please submit an issue on the GitHub issue tracker.
  • Pull requests: We encourage you to submit pull requests for bug fixes and new features. Please make sure to follow the coding style and guidelines outlined in the project's contribution guide (if applicable).

License

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

Authors

  • HoJoon Eum