![Logo](/AS-Devs/json2scss-map/raw/master/images/JSON2SCSS.png)
It's an utility package that converts any JSON stream into Sass maps with additional color convertion.
View Changelog
·
View Demo
·
Report Bug
·
Request Feature
Table of Contents
There are lots of Json-sass package available on npm; however, I didn't find one that really suited my needs. So, I thought of creating one. Altough, i find one that's good enough & made by Andrew Clark. But that wasn't maintained from last 6+ years.
This is an Utility module that converts a JSON stream into sass maps. It can be used as whitelabeling themes, fonts etc.
Here's why it's best:
- firstly, you can share values between your scripts and stylesheets without having to use something like SassyJSON, which doesn't work with libsass.
- json2scss-map converts JSON objects into Sass maps, which are supported in Ruby Sass 3.3 and libsass 2.0. & latest Dart Sass.
- Also, New Version includes color convertions to
HEX, HSL, RGB
😎
This is a very small package without any major dependency. However, we use some packages for giving support for browser-compatible Javascript, Unit testing etc.
It's very easy to use this plugin, instructions are mentioned below or you can View Demo.
There no such basic requirements only node.js
should be installed. also update npm or yarn
if you can. Although, there are some issue in node 17.
There are many ways to install this plugin mentioned below.
- using npm:
npm i -D json2scss-map
- using yarn:
yarn add json2scss-map -D
Example source file theme.json
:
From the command-line:
$ json2scss-map -i theme.json -o theme.scss -p "\$variable: " -t hex
-i
,--infile
: (Default: standard inputstdin
) Specify the input file.-o
,--outfile
: (Default: standard outputstdout
) Designate the output SCSS file.-p
,--prefix
: Set a prefix for all SCSS variables.-s
,--suffix
: Set a suffix for all SCSS variables.-c
,--colorConversion
: (Default: true) Enable or disable color conversion.-t
,--convertTo
: (Default: 'hsl') Specify the color format (e.g.,hsl
,hex
,rgb
).-l
,--cl4Syntax
: (Default: false) Use cl4 syntax if applicable.
Output theme.scss
with new color convertion to hsl
by default:
Or you can use the Node API: (recommended)
var fs = require('fs');
var json2scss = require('json2scss-map');
fs.createReadStream('theme.json')
.pipe(json2scss({
prefix: '$variable: ',
cl4Syntax: true
}))
.pipe(fs.createWriteStream('theme.scss'));
Then, you can get the values from scss:map
$color-primary: map-get(map-get($variable, "theme"), "primary"); // hsl(211, 100%, 50%)
$font-main: map-get($variable, "fs-main"); // "Mulish"
You can also transform normal JavaScript values using the exposed utility function:
json2scss.convertJs([1, 2, 3]); // (1, 2, 3)
For more, please refer to the View Demo
Alternatively, If you're using custom Webpack Configuration, you can use this tool to easily import JSON files and get the correct values. Json2scss-map-webpack-importer
Returns a through stream. Available options:
prefix
: Add some text to the beginningsuffix
: Add some text to the end. Defaults to';'
.colorConversion
: New Feature to convert only colors value to different color scheme. Defaults totrue
. (from v1.5.0)convertTo
: Right now support convertion forHSL, RGB, HEX
colors. Defaults tohsl
.cl4Syntax
: color lavel 4 new syntax with space separator for the output. Defaults tofalse
.
Convert a normal JavaScript value to its string representation in Sass. Ignores undefined
and functions. Calls .toString()
on non-plain object instances.
- Add Changelog
- Add new Color label 4 syntax support
- Add Color Convertion to (hsl, rgb, hex)
- Add support for Color Lavel 4 Syntax in input stream as well.
- Add Support for LCH color scheme convertion.
- Add Support for
%
value in RGB colors. - Add Support for
turn, deg, rad
etc unit support in hsl & rgb colors.
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this plugin better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star ⭐️! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/{feature-name}
) - Commit your Changes (
git commit -m 'Add some {feature}'
) - Push to the Branch (
git push origin feature/{feature-name}
) - Open a Pull Request
- You're awesome.
Distributed under the MIT License. See LICENSE
for more information.
AS Developers
Susanta Chakraborty - @susantaChak
Project Link: https://github.com/AS-Devs/json2scss-map
There are lots of people to acknowledge. I've included few of them to help be build this plugin.
If you found this project helpful or you learned something from the source code and want to thank me, consider buying me a cup of 🍵