Created
June 11, 2017 12:48
-
-
Save adamgruber/3af1de1d41c6e552b79588c4c0e07936 to your computer and use it in GitHub Desktop.
Use sass-extract to get JS object from sass variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable import/no-webpack-loader-syntax */ | |
import mapValues from 'lodash/mapValues'; | |
const style = require('sass-extract-loader!./colors.scss'); | |
function getSassValue(sassVar) { | |
const {type,value} = sassVar; | |
switch (type) { | |
case 'SassString': | |
case 'SassBoolean': | |
case 'SassNull': | |
return value; | |
case 'SassNumber': | |
return sassVar.unit ? `${value}${sassVar.unit}` : value; | |
case 'SassColor': | |
const {r,g,b,a} = value; | |
return `rgba(${r}, ${g}, ${b}${a !== 1 ? `, ${a}` : ''})`; | |
case 'SassList': | |
return value.map(getSassValue).join(' '); | |
case 'SassMap': | |
return mapValues(value, getSassValue); | |
default: | |
return value; | |
} | |
} | |
const globalStyles = style.global || {}; | |
export default mapValues(globalStyles, getSassValue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment