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

sass3js

v1.0.9

Published

Convert SASS variables to JS or TS

Downloads

886

Readme

sass3js

npm version GitHub license

Convert SASS * variables to JavaScript (flow supported), JSON, and TypeScript.

It takes this:

$red: #ff0001;
$dark-red: darken($red, 20%);
$spacing-1: 5px;
$spacing-2: $spacing-1 * 2;

and outputs this:

export default {
  red: "#ff0001",
  darkRed: "#990001",
  spacing1: "5px",
  spacing2: "10px",
};

so you can do stuff like this:

import variables from "styles/variables";

function Logo() {
  return <Icon src={logo} fill={variables.darkRed} />;
}

* Only SCSS syntax is supported at the moment.

Getting started

Globally

You can install sass3js globally with npm:

$ npm install -g sass3js sass

and use it like this:

$ sass3js variables.scss variables.js

In your project

You can also add sass3js as a dev dependency to your own module:

$ npm i -D sass3js

Optionally, you can add a script to your package.json file. Example:

  "scripts": {
    "update-vars": "sass3js -f ts styles/variables.scss styles/variables.ts"
  }

Note: sass3js requires you to have sass installed

CLI

The CLI can take a source and a destination file:

$ sass3js source.scss destination.js

If those aren't provided, stdin and stdout are used instead:

$ cat source.scss | sass3js > destination.js

Options

-f, --format <format>

Output format. Takes: js, json, ts, or flow. Default: js.

-t, --tab <tab>

Number of spaces or string to use for indentation. Default: 2.

Using 4 spaces:

$ sass3js -t 4 source.scss

Using tabs instead of spaces:

$ sass3js -t $'\t' source.scss

API

The API is pretty small. It contains only two functions: getVariables and format.

Types:

type Variables = { [variable: string]: string };
type Format = "js" | "json" | "ts" | "flow";

getVariables(contents: string): Variables

This function takes the contents of your SCSS file and returns an object where the keys are the variable names and the values are the rendered (final) values.

format(variables: Variables, format: Format, tab: number | string): string

The format function serializes the variables and generates the output file. It takes the variables object returned by getVariables, a format option, and tab argument which can either be the number of spaces or the character to use (e.g. tab).

The Name

I know it's silly, but sass2js is already taken 😛

Motivation

sass > node-sass

There are already a number of npm packages that do something similar to this one. However, most of them use node-sass instead of sass. The latter is the official implementation and tools like parcel recommend using it over the former. I didn't want to have both in my project so I made this.

Type safety

In addition, there are some webpack loaders that let you import your SASS variables from JS. The problem with those is that even if you use TypeScript or Flow, there's no way (that I'm aware of) for the compiler to check the type of your variables object.

sass3js creates an actual .js/.ts file that you can import like you would any other module. This allows your type checker to infer the types of the variables object and alert you of typos and other type errors at compile time.

Inspiration

  1. https://github.com/nordnet/sass-variable-loader
  2. https://github.com/hankmccoy/sass-to-js-var-loader