Convert between json, scss, and plain javascript objects. Forks the parseJson
function from node-sass-import-once.
If options.unquoteColors
is set to true, the color values in the sass variable will be unquoted so they can be used properly in your stylesheets.
Caution: this module has not been properly tested yet. Use at your own risks.
- parses Yaml buffers/strings
- parses JSON buffers/strings
- parses plain javascript literals and hashes
- unquote color values (hex, rgb, rgba, hsl, hsla, named CSS colors)
- unquote length values (%, em, ex, ch, em, vh, vw, in, ax, px, q, mm, cm, in, pt, pc)
- unquote angle values (deg, grad, rad, turn)
- unquote time values (s, ms)
- read files asyncrhoneously (returns a promise)
- option to write to a file directly
- indenting resulting sass code (more readable)
- option to get sass syntax output (instead of scss)
- resolving commonjs modules to json before converting
if options.unquoteLengths
is set to true, the color values in the sass variable will be unquoted so they can be used properly in your stylesheets.
var sassVars = require('sass-vars');
// values to convert
var obj = { foo: 'bar', bar: 10, baz: '#f49842' }
var json = JSON.stringify(obj);
var ile = 'path/to/js/file'
var ymlFile = '/path/to/yaml/file.yml'
sassVars(obj, { name: 'my-obj' });
sassVars(json, { name: 'my-obj', type: 'json' });
// => '$my-obj: ("foo": "bar", "bar": 10, "baz": #f429842)';
// can convert from a .yml, .js or .json file
// - reads file syncroneously and uses filename by default
sassVars(file, { isFile: true });
sassVars(ymlFile, { isFile: true });
// => '$file: ( ... )';
// also accepts a buffer
sassVars(fs.readFileSync(file));
sassVars(fs.readFileSync(ymlFile));
// => '$file: ( ... )';
-
name
(string) the name to give to the sass variable. ifisFile
is set totrue
, this will default to the file's name. -
default
(bool) whether to end the variable declaration with ' !default;' instead of ';'. -
type
: (string) the type of the data that needs to be converted. ifisFile
is set to true, sassVars will guess it based on the given path. In case a json string is given, setting this option to'json'
avoids an extra call toJSON.stringify()
-
isFile
: (bool) whether a file path was given. If true, sassVars will load the file synchroneously and convert its contents. Defaults tofalse
. -
encoding
: (string) what encoding to use to read file's content. Defaults to 'utf8'. -
unquoteColors
(bool) whether to unquote color values in the sass variable declaration. Defaults totrue
. -
unquoteNumbers
(bool) whether to unquote number values (length, angle & time) in the sass variable declaration. Deaults totrue
.