Export Sass as JSON data
npm install @theme-tools/sass-export-dataUses custom functions in node-sass to export JSON files from Sass maps and other variables.
``bash`
npm install --save @theme-tools/sass-export-data
`js`
const config = {
name: 'export_data', // Name of Sass function
path: 'path/to/export/folder/' // Folder where to place JSON files
};
const sassExportData = require('@theme-tools/sass-export-data')(config);
Then pass sassExportData to the functions option in node-sass. It'll be the object like it wants, so you could merge it with other functions if needed. This can work with many ways to compile sass: gulp, webpack, basic cli, or anything that utilizes node-sass.
In your sass declare this mixin to make it easier:
`scsspath/to/export/folder/
/// Export Sass Data to JSON in foldermystuff.json
/// @param {String} $filename - ie export_data
/// @param $var - What to turn into JSON
/// @example scss
/// @include export-data-to-lib('filename.json', $sass-map);
@mixin export-data($filename, $var) {
// The function is a custom function added to Sass.$data
// The var is weird, but needed.`
$data: export_data($filename, $var);
};
Then you can do this wherever you'd like:
`scss
$x: (
a: 'Apple',
b: 'Beer',
);
@include export-data('example.json', $x);
`
This will create the file example.json in path/to/export/folder/ with:
`json`
{
"a": "Apple",
"b": "Beer"
}
- If the file hasn't changed, it won't output. That helps watchers go less crazy.
Special thanks to node-sass-export` for inspiration and much of the original code.