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

node-sass-json-importer

v4.3.0

Published

Allows importing json in sass files parsed by node-sass.

Downloads

111,698

Readme

node-sass-json-importer

JSON importer for node-sass. Allows @importing .json or .json5 files in Sass files parsed by node-sass.

npm build status

Usage

node-sass

This module hooks into node-sass's importer api.

var sass = require('node-sass');
var jsonImporter = require('node-sass-json-importer');

// Example 1
sass.render({
  file: scss_filename,
  importer: jsonImporter(),
  [, options..]
}, function(err, result) { /*...*/ });

// Example 2
var result = sass.renderSync({
  data: scss_content
  importer: [jsonImporter(), someOtherImporter]
  [, options..]
});

node-sass command-line interface

To run this using node-sass CLI, point --importer to your installed json importer, for example:

./node_modules/.bin/node-sass --importer node_modules/node-sass-json-importer/dist/cli.js --recursive ./src --output ./dist

Webpack / sass-loader

Webpack v1

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

// Webpack config
export default {
  module: {
    loaders: [{
      test: /\.scss$/,
      loaders: ["style", "css", "sass"]
    }],
  },
  // Apply the JSON importer via sass-loader's options.
  sassLoader: {
    importer: jsonImporter()
  }
};

Webpack v2

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

// Webpack config
export default {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1
            },
          },
          {
            loader: 'sass-loader',
            // Apply the JSON importer via sass-loader's options.
            options: {
              importer: jsonImporter(),
            },
          },
        },
      ],
    ],
  },
};

Importing

Importing strings

Since JSON doesn't map directly to SASS's data types, a common source of confusion is how to handle strings. While SASS allows strings to be both quoted and unquoted, strings containing spaces, commas and/or other special characters have to be wrapped in quotes. In terms of JSON, this means the string has to be double quoted:

Incorrect
{
  "description": "A sentence with spaces."
}
Correct
{
  "description": "'A sentence with spaces.'"
}

See discussion here for more:

https://github.com/Updater/node-sass-json-importer/pull/5

Importing *.js Files

You can also import *.js Files. This way you can use javascript to compose and export json structure for node-sass-json-importer.

const xl = require('./variables.json')
const md = require('./variables-md.json')
const xs = require('./variables-xs.json')

module.exports = {
    xl,
    md,
    xs,
}

Custom resolver

Should you care to resolve paths, say, starting with ~/ relative to project root or some other arbitrary directory, you can do it as follows:

1.sass:

@import '~/1.json'
body
    margin: $body-margin

json/1.json:

{"body-margin": 0}
var path = require('path');
var sass = require('node-sass');
var jsonImporter = require('../dist/node-sass-json-importer');

sass.render({
  file: './1.sass',
  importer: jsonImporter({
    resolver: function(dir, url) {
      return url.startsWith('~/')
        ? path.resolve(dir, 'json', url.substr(2))
        : path.resolve(dir, url);
    },
  }),
}, function(err, result) { console.log(err || result.css.toString()) });

camelCase to kebab-case

If you want to convert standard JavaScript caseCase keys into CSS/SCSS compliant kebab-case keys, for example:

variables.json:

{
  "bgBackgroundColor": 'red'
}

For usage like this:

style.scss:

@import "variables.json";

div {
  background: $bg-background-color;
}

You can pass set the convertCase option to true as an argument to jsonImporter like so:

sass.render({
  file: './1.sass',
  importer: jsonImporter({
    convertCase: true,
  }),
}, function(err, result) { console.log(err || result.css.toString()) });

Thanks to

This module is based on the sass-json-vars gem, which unfortunately isn't compatible with node-sass.