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

sass-variable-parser

v1.2.2

Published

Sass variable parser and webpack loader

Downloads

55

Readme

Sass variable parser and loader for webpack

Works as a Webpack loader or can be used as parser in Node.js

Parses variables from sass, evaluates their values with node-sass and returns the result as a Javascript object.

It was initially a fork of sass-variable-loader, but is completely rewritten: major bugs fixed, added maps support, elabortate tests with real world Sass included. Thanks for the directions anyway!

Features

  • Returns only top-level variables (obviously).
  • Emits both "plain" variables and maps. Maps are represented as nested objects.
  • By default "camelizes" variable names. Can be changed through options.
  • Returns only variables from the imported file itself, but follows @imports to evaluate dependent values.
  • Supports both scss and indented syntax.
  • Written in strict Typescript.
  • Reliable. Thoroughly tested.

Table Of Contents

Result example

{
  tagColor: "#409EFF",
  tagBorder: "rgba(64, 158, 255, 0.2)",
  tagBorderRadius: "4px",
  someMap: {
    key1: "value1",
    key2: "value2"
  }
}

Installation

npm i sass-variable-parser -D

Usage as a Webpack loader

No need to touch webpack config. Loaders can be used inline. Just install devDependency and go ahead. Two exclamation marks disable for this import all loaders and preloaders specified in the webpack configuration.

import variables from '!!sass-variable-parser!./_variables.scss';
// => returns all the variables in _variables.scss as an object with each variable name camelCased

Without camel-casing:

import variables from '!!sass-variable-parser?-camelCase!./_variables.scss';

Usage as a parser

const path = require('path');
const { parse } = require('sass-variable-parser');

const options = {
  // defaults to true
  camelCase: false,
  // optional, only if there are @imports with relative paths
  cwd: path.resolve(__dirname, 'node_modules/bulma/sass/utilities'),
  // true means indented sass syntax, defaults to false ('scss' syntax)
  indented: true,
};

const variables = parse(
  `
@import "initial-variables.sass"

$primary: $turquoise !default
$info: $cyan !default

$family-primary: $family-monospace`,
  options
);

variables would be:

{
  "primary": "#00d1b2",
  "info": "#209cee",
  "family-primary": "monospace"
}

Check out src/spec folder for more exmaples

Options

When using as a loader pass through query string (see how).

When using as a parser pass options object as the second parameter to parse method.

| Option | Default | Description | | --------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | | camelCase | true | Whether to camelize variable names | | cwd | Webpack's context when used as loader | Current working directory from which @import paths are calculated. Typically not needed when used as loader | | indented | false | Whether the loaded sass is in indented syntax or not. When used as loader is auto-calculated from file extension |

Contributing

The project is created with typescript-starter. Check out it's README for more info.

License

MIT (http://www.opensource.org/licenses/mit-license.php)