SassyExport is a lightweight Compass extension that allows you to export an encoded Sass map into an external JSON file. Use it anyway you like.
gem install SassyExport
- Add
require "SassyExport"
to yourconfig.rb
- Import into your stylesheet with
@import "SassyExport";
Our file structure,
root
├── sass
│ ├── style.scss
├── json
└── config.rb
Sass,
// ./sass/style.scss
@import "SassyExport";
$map: (
hello: world,
);
// SassyExport : convert passed map to json and write to path/to/filename.json
// ----------------------------------------------------------------------------------------------------
// @param $path [string] : directory path and filename
// @param $map [map] : map to convert to json
// @param $pretty [bool] : pretty print json
// @param $debug [bool] : print debug string with path
// ----------------------------------------------------------------------------------------------------
// @return $string | write json to path
@include SassyExport("/json/hello.json", $map, true);
New JSON file is created at ./json/hello.json
. As you can see, $path
is relative to where your config.rb
is located. Simply calling @include SassyExport("/hello.json", $map)
will write to your directory root.
{
"hello": "world"
}
The SassyExport()
mixin takes a directory/filename.json $path
, and a Sass $map
as arguments. There is also an optional argument $pretty
which defaults to false
, but will print pretty JSON (nicely indented) if set to true
.
It converts the $map
into a JSON map through Ruby, and then creates a new file (or updates an existing file), and writes the contents of the JSON string to it. I'm no Ruby expert, so if you belive that you could improve the small amount of code here, feel free to.
Enjoy.