I wasn't entirely sure if I could use JSON objects that already existed in my code without having to save them to a file first. Using the Importing *.js Files documentation I was able to do it, but I thought it might be worth adding documentation that makes that explicit.
I can create a PR for this if you agree. Here's the basic version:
- Provide an object of JSON values. You can do this directly like so:
const theme = {
"colors": {
"primary": "#900",
"secondary": "#060"
}
}
or you can use JSON.stringify to transform any objects you already have into JSON-formatted objects.
- If you have nested objects, it appears that you can't access them through Sass's
map-get() function. So with the above example data, you couldn't do color: map-get($theme.colors, 'primary'). Instead, you have to create objects of the nested objects, e.g.:
const colors = theme.colors
- Then, as in the "Importing *.js Files" example, export your objects:
module.exports = {
colors
}
- Finally, call the JS file you've just created and use the
map-get() function:
@import 'theme.js';
a {
color: map-get($colors, 'primary');
}
My particular use case was providing a way for people to define theme components in a YAML file, then using js-yaml to read them in. So it seemed really silly to then have to export the settings back to a JSON file just for consumption by the JSON importer! Doing it with the objects alone made me much happier.
I wasn't entirely sure if I could use JSON objects that already existed in my code without having to save them to a file first. Using the Importing *.js Files documentation I was able to do it, but I thought it might be worth adding documentation that makes that explicit.
I can create a PR for this if you agree. Here's the basic version:
or you can use
JSON.stringifyto transform any objects you already have into JSON-formatted objects.map-get()function. So with the above example data, you couldn't docolor: map-get($theme.colors, 'primary'). Instead, you have to create objects of the nested objects, e.g.:map-get()function:My particular use case was providing a way for people to define theme components in a YAML file, then using js-yaml to read them in. So it seemed really silly to then have to export the settings back to a JSON file just for consumption by the JSON importer! Doing it with the objects alone made me much happier.