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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sass-importer-json

v0.2.0

Published

Dart Sass importer for reading values in JSON files into Sass/SCSS variables

Readme

sass-importer-json

Dart Sass importer for reading values in JSON files (or most other key-value-ish files) into Sass/SCSS variables.

Usage

npm install -D --production sass-importer-json

The default export is a function which takes an options object and returns an importer. Add the returned importer to the importers array when you call sass.compile or sass.compileAsync from the Dart Sass JS API.

Options

  • extensions: Defaults to ['.json']. List of file extensions the importer will recognize and attempt to load.
  • parse: Defaults to JSON.parse, but can be any function which receives file contents as a string and returns a value. Its return value will be read as a dict whose entries will become the file's declared Sass variables.
  • encoding: Defaults to 'utf-8' which is almost certainly what you want, but you can pass any encoding Node supports

Examples

Basic Usage

build.js:

import sass from 'sass';
import jsonImporter from 'sass-importer-json';

const output = sass.compile('style.scss', {
	importers: [jsonImporter()],
});

// log it, write it, stick it in a stew
console.log(output.css);

data.json:

{
	"foo": "i am a string",
	"bar": 42
}

style.scss:

@use "data";

.something::after {
	content: data.$foo;
	font-size: daa.$bar * 1px;
}

Then, in your terminal:

$ node build.js
.something::after {
  content: "i am a string";
  font-size: 42px;
}

Custom formats

With the parse and extensions options, you can add support for importing custom syntaxes like JSONC or JSON5, or even completely different formats like YAML. Basically any format consisting of key-value pairs can probably be used with this importer if you set an appropriate parse function:

import {compile} from 'sass';
import jsonImporter from 'sass-importer-json';

import JSON5 from 'json5';
import JSONC from 'jsonc-parser';
import YAML from 'yaml';

const out = compile('style.scss', {
	importers: [
		jsonImporter({
			extensions: ['.json', '.jsonc'],
			parse: JSONC.parse,
		}),
		jsonImporter({
			extensions: ['.json5'],
			parse: JSON5.parse,
		}),
		jsonImporter({
			extensions: ['.yml', '.yaml'],
			parse: YAML.parse,
		}),
	],
});

You don't have to pass a library function directly to parse - you can also write a custom function which takes a string and returns a file's contents. This can be useful to pass custom options to other parsers, or to pass a reviver to standard JSON.parse:

import {compile} from 'sass';
import jsonImporter from 'sass-importer-json';
import YAML from 'yaml';

const out = compile('style.scss', {
	importers: [jsonImporter({
		extensions: ['.yaml', '.yml'],
		parse: value => YAML.parse(value, undefined, {stringKeys: true});
	})],
});

Development

  • npm run fmt to format code with dprint
  • npm run build to build the typescript thingy

todos

  • alternate version that doesn't use readFileSync (is this important? what does the actual dart sass file loader do for normal sass files?)
  • maybe handle color values somehow (all values are put in sass as quoted strings; converting from a string to a color from sass is impossible; people might want to be able to import a color value from sass and then modify it using color.scale() or whatever? unsure how important this is since you can also just pass colors as number tuples or some other consistent value and manually create the color values from those numbers from sass)

License

WTFPL